C ++ memory problems (many companies' interview questions are worth reading. Don't forget to tell me if you understand them)

Source: Internet
Author: User
C ++ memory problems (many companies' interview questions are worth reading. Don't forget to tell me if you understand them) Void getmemory (char * P)
{
P = (char *) malloc (100 );
}

Void test (void)
{
Char * STR = NULL;
Getmemory (STR );
Strcpy (STR, "helloworld ");
Printf (STR );
}
What are the results of running the test function?
A:ProgramCrash. Because getmemory cannot pass dynamic memory, STR in the test function is always null. Strcpy (STR, "helloworld"); will crash the program.

Char * getmemory (void)
{
Char P [] = "helloworld ";
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 a pointer to "stack memory", the address of this pointer is not null, but its original 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?
Answer: (1) Hello output (2) Memory leakage

The above are all about memory. I would like to ask the first reason why getmemory cannot pass dynamic memory, but the third getmemory2 (char ** P, int num) can; the second one said that getmemory returned a pointer pointing to the "stack memory". The address of the pointer is not null. In short, I don't understand it. I 'd better understand what people can explain. ========================================================== ================================== They are all simple questions.
Void getmemory (char * P)
{
P = (char *) malloc (100 );
}
The p Parameter copies the value of the original pointer instead of the original pointer. Therefore, even if P points to the new address again, the original Pointer Points to the address unchanged.
Char * getmemory (void)
{
Char P [] = "helloworld ";
Return P;
}
P is a local variable. After leaving the scope, the stack space is recycled and the result is unpredictable.
Void getmemory (char ** P, int num)
{
* P = (char *) malloc (Num );
}
P is the pointer to the passed pointer. * P is the value assigned to the passed pointer. Therefore, the value of the passed pointer can be changed as follows:
Void getmemory (char * & P)
{
P = (char *) malloc (100 );
}
Same effect
========================================================== ================================== During VC debugging, press Alt + 8, TC or BC to use TD for debugging. Open the Assembly window and check whether the Assembly corresponding to each sentence C is understood.
To understand the C pointer in essence, you must learn assembly and the correspondence between C and assembly.
From the perspective of compilation, understanding and learning the pointers of C language makes it very simple to look at something complicated! From: http://topic.csdn.net/u/20100318/21/ae111d42-27e4-413a-9fd9-7132367d9f91.html

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.