Memory Allocation in C:
1.
# Include <stdio. h>
# Include <stdlib. h>
# Include <String. H>
# Include <assert. h>
VoidGetmemory (Char** P)
{
* P = (Char*) Malloc (100 );
}
VoidTest ()
{
Char** STR;
Getmemory (STR );
Strcpy (* STR,"Hello World");
Printf ("STR is % s", * Str );
}
IntMain ()
{
Test ();
System ("Pause");
Return0;
}
Result: A running error occurs.
Getmemory does not properly allocate memory. Str starts to be null. After the input parameter is passed, P is null. * P will certainly encounter an error when referencing it.
Correct correction:
# Include <stdio. h>
# Include <stdlib. h>
# Include <String. H>
# Include <assert. h>
VoidGetmemory (Char** P)
{
Assert (P! = NULL );
* P = (Char*) Malloc (100 );
}
VoidTest ()
{
Char* STR = NULL;
Getmemory (& Str );
Strcpy (STR,"Hello World");
Printf (STR );
}
IntMain ()
{
Test ();
System ("Pause");
Return0;
}
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------
2.
# Include <stdio. h>
# Include <stdlib. h>
# Include <String. H>
# Include <assert. h>
Char* Getmemory ()
{
Return "Hello World";
}
VoidTest ()
{
Char* STR = NULL;
STR = getmemory ();
Printf (STR );
}
IntMain ()
{
Test ();
System ("Pause");
Return0;
}
Print: Hello World
The pointer returned by getmemory points to the read-only zone, but the test function does not perform write operations, so the print is correct.
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------
3.
# Include <stdio. h>
# Include <stdlib. h>
# Include <String. H>
# Include <assert. h>
StructPara
{
CharSTR [11];
CharLen;
};
VoidGetmemory (CharP [11],Char* Len)
{
* Len = strlen ("Hello World");
Strcpy (p,"Hello World");
}
VoidTest ()
{
StructPara;
IntI;
Getmemory (A. STR, & A. Len );
Printf ("% D", A. Len );
For(I = 0; I <A. Len; ++ I)
{
Printf ("% C", A. Str [I]);
}
}
IntMain ()
{
Test ();
System ("Pause");
Return0;
}
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------
4.
# Include <stdio. h>
# Include <stdlib. h>
# Include <String. H>
# Include <assert. h>
VoidTest ()
{
Char* STR = (Char*) Malloc (100 );
Strcpy (STR,"Hello");
STR + = 6;
Free (STR );
If(STR! = NULL)
{
Strcpy (STR,"World");
Printf (STR );
}
}
IntMain ()
{
Test ();
System ("Pause");
Return0;
}
GCC print: World
VC: assertion failed, error during running
STR has been removed by 6 digits, and then free. I don't understand what this means. It should not be allowed !!! (VC does Assert failure, running error, but GCC is useless)
But STR must not be null. The memory to which it points should be uncertain. However, it is a pity that errors are not reported during compilation and running.
Last question:
Design a calculator. It is known that all input is stored in char buff [].
Last question:
Knapsack problems, recursion and non-recursionAlgorithm