C memory allocation 2

Source: Internet
Author: User

There are three memory allocation methods:
(1) distribution from the static storage area. It is allocated when the program is compiled.
It exists throughout the running period. For example, global variables and static variables.
(2) create a stack. When executing a function, all the storage units of local variables in the function can be created on the stack.
These storage units are automatically released when execution ends. Stack memory allocation operations are embedded in the processor's Instruction Set
Medium, high efficiency, but the allocated memory capacity is limited.
(3) allocate from the stack, also known as dynamic memory allocation. When the program is running, use malloc or new to apply for any number
With less memory, the programmer is responsible for releasing the memory with free or delete. Lifetime of dynamic memory
It is up to us to decide that it is very flexible to use, but there are also the most problems.

Void getmemory (char * P)
{
P = (char *) malloc (100 );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (STR );
Strcpy (STR, "Hello World ");
Printf (STR );
}
What are the results of running the test function?
A: The program crashes.
Because getmemory cannot transmit dynamic memory,
STR in the test function is always null.
Strcpy (STR, "Hello World"); program crash
.
Char * getmemory (void)
{
Char P [] = "Hello World ";
Return P;
}
Void test (void)
{
Char * STR = NULL;
STR = getmemory ();
Printf (STR );
}
What are the results of running the test function?
A: It may be garbled.
Because getmemory returns "stack memory"
Pointer, the pointer address is not null, but its original
The current content has been cleared, and the new content is unknown.
Void getmemory2 (char ** P, int num)
{
* P = (char *) malloc (Num );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (& STR, 100 );
Strcpy (STR, "hello ");
Printf (STR );
}
What are the results of running the test function?
A:
(1) Output hello
(2) Memory leakage
Void test (void)
{
Char * STR = (char *) malloc (100 );
Strcpy (STR, "hello ");
Free (STR );
If (STR! = NULL)
{
Strcpy (STR, "World ");
Printf (STR );
}
}
What are the results of running the test function?
A: Tampering with content in the dynamic memory area is difficult to pre-empt.
Material, very dangerous.
Because free (STR); after that, STR becomes a wild pointer,
If (STR! = NULL) the statement does not work.
**************************************** **************************************** **************

Void getmemory2 (char ** P, int num)
{
* P = (char *) malloc (sizeof (char) * num );
}
Void Test2 (void)
{
Char * STR = NULL;
Getmemory2 (& STR, 100); // note that the parameter is & STR, not Str
Strcpy (STR, "hello ");
Cout <STR <Endl;
Free (STR );
}
If you have to use the pointer parameter to request memory, you should use "pointer to Pointer" instead ". because the concept of "pointer to Pointer" is not easy to understand, we can use function return values to transmit dynamic memory.
Char * getmemory3 (INT num)
{
Char * P = (char *) malloc (sizeof (char) * num );
Return P;
}
Void test3 (void)
{
Char * STR = NULL;
STR = getmemory3 (100 );
Strcpy (STR, "hello ");
Cout <STR <Endl;
Free (STR );
}
**************************************** **************************************** **************

Although it is easy to use the function return value to pass dynamic memory, some people often use the return statement in an error.
. It is emphasized that the return statement should not be used to return the pointer pointing to the "stack memory", because there is a function end time in it.
Automatic extinction

Char * getstring (void)
{
Char P [] = "Hello World ";
Return P; // the compiler will give a warning
}
Void test4 (void)
{
Char * STR = NULL;
STR = getstring (); // STR content is junk
Cout <STR <Endl;
}
Use the debugger to track test4 step by step. After executing the STR = getstring statement, STR is no longer a null pointer,
However, STR content is not "Hello World" but garbage.

Char * getstring2 (void)
{
Char * P = "Hello World ";
Return P;
}
Void test5 (void)
{
Char * STR = NULL;
STR = getstring2 ();
Cout <STR <Endl;
}
Although the function test5 runs without errors, the design concept of the function getstring2 is incorrect. Because
The "Hello World" in getstring2 is a constant string located in the static storage zone. It is within the lifetime of the program.
Constant. No matter when getstring2 is called, it returns the same read-only memory block.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.