Some minor issues with C ++ (1)

Source: Internet
Author: User

Char A [] = "liuj"; defines a string ending with '\ 0', so sizeof (A) = 5

Char B [] = {'l', 'I', 'U', 'J'}; defines a character array, so sizeof (B) = 4

InC LanguageA [0], a [1]… To call an element. In Perl, the string scalar $ A = "liuj" cannot be accessed by subscript. If you want to access it, you can use split (", $ A) to separate strings one by one.

The application of the cstring class starts because the C ++ has not yet determined the standard. If there is a standard, the string class is used. To use the string class, remember to add the header file <string> and using namespcae STD; to assign values to the string class, you can use = to assign values directly, or assign values to STR ("liuj") using constructors ").

Code
# Include < Iostream >
# Include < String >
Using   Namespace STD;

Char STR [] =   " Liujian " ;
Char   * P = STR;
Int N =   10 ;
Int A = 0 , B = 0 , C = 0 , D = 0 , E = 0 ;
Int Main ( Char St [ 100 ])
{
A = Sizeof (STR );
B = Sizeof (P );
C = Sizeof (N );
D = Sizeof (St );
Void   * P = Malloc ( 100 );
E = Sizeof (P );
Return   0 ;
}

Obtain a = 8, B, c, d, e = 4.

What is the purpose of const? (Please specify at least two types)

A: (1) const constants can be defined.

(2) const can modify function parameters, return values, and even function definitions. All things modified by const are protected by force, which can prevent unexpected changes and improveProgramRobustness.

In the C ++ program, why should I add the extern "C" to call the function compiled by the C compiler "?
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.

The most common C ++ exam

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.

Note the followingCodeProblems

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

Answer: 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.

Please write out the code output

Code
# Include < Iostream >
Using   Namespace STD;

Class A
{
Public :
Virtual   Void Func1 ( Void ) {Cout<"This is a: func1 \ n";} ;
} ;

Class B: Public A
{
Public :
Virtual   Void Func1 ( Void ) {Cout<"This is B: func1 \ n";} ;
} ;

Class C: Public A
{
Public :
Virtual   Void Func1 ( Void ) {Cout<"This is C: func1 \ n";} ;
} ;

Void Test ( * A)
{
A->Func1 ();
}

Int Main ()
{
A;
B;
C;
Test (&A );
Test (&B );
Test (&C );
Return 0;
}

This is a: func1
This is B: func1
This is C: func1
Because of the use of virtual functions, each call is to determine the type of instance to which it is directed at runtime.

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.