C ++ minor issues

Source: Internet
Author: User

1. In C ++ProgramTo call the function compiled by the C compiler. Why should we add the extern "C "?
A: function Overloading is supported in C ++ and function Overloading is not supported in C. The name of the function in the library after being compiled by C ++ is different from that in C language. Assume that the prototype of a function is void Foo (int x, int y). After the function is compiled by the C compiler, its name in the library is _ Foo, the C ++ compiler generates names such as _ foo_int_int. C ++ programs cannot directly call C functions because their compiled names are different. Therefore, C ++ provides a C connection exchange with the specified symbol extern "C" to solve the name matching problem.

 

2. Test memory

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.All function parameters are local variables. Changing the values of these parameters does not affect the values in the called function. Local variables are stored in the stack. When the function returns, the stack is automatically cleared. The memory allocated by malloc will not be automatically released. P points to the memory block in the free storage zone. When the pointer is out of scope, the memory block will not be automatically returned to the free storage zone.Because getmemory cannot pass dynamic memory, STR in the test function is always null. Strcpy (STR, "Hello World"); will crash the program.

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 a pointer to the "stack memory", the pointer address is not null, but its original content has been cleared, and the new content is unknown. P [] array is the partial automatic variable in the function. After the function returns, the memory has been released. This is a common mistake made by many programmers. Its root cause is that they do not understand the survival of variables.

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) the hello (2) test function can be output and the memory of malloc is not released. (3) getmemory avoids Question 1. The input parameter of getmemory is a pointer to the string pointer, but the request memory and value assignment statement P = (char *) is executed in getmemory *) malloc (Num); if the memory application is successful, add: If (* P = NULL ){... // handle Memory Request failure}

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 unpredictable and dangerous. Because free (STR); then, STR becomes a wild pointer, if (STR! = NULL) the statement does not work.

 

3. Point out the followingCodeProblems

Int Main ()
{
Char A;
Char   * Str = & A;
Strcpy (STR, " Hello " );
Printf (STR );
Return   0 ;
}

A: If no memory space is allocated for STR, an exception will occur. The problem is that a string is copied into the address indicated by a character variable pointer. Although the results can be correctly output, the program crashes because internal read/write is performed out of the border.

 

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.