C ++ pointer differs from the array

C ++ / C program, the array is either created (such as global array) in static memory, or be created on the stack. Array name corresponds to (instead of pointing to) a memory, its address and the capacity during the life remain the same, only the contents of the array can be changed. Pointer can point to any type of memory block at any time, it is characterized by "variable", so we used a pointer to dynamic memory operation. Flexible than an array of pointers, but also more dangerous.
The following characteristics of Comparative Example string of pointers and arrays.

First, modify the content

. 1  char A [] = "Hello"; // "Hello" is located in the constant memory area, a is global (static) initialization region
 2 A [ 0 ] = 'X-';
 . 3 COUT A << << endl;
 . 4  char * = p "World"; // Note that constant string pointed to by p p is in the global (static) zone initialization 
. 5 p [ 0 ] = 'X-'; // compiler can not find the contents of the error constant string can not be modified 
. 6 COUT P << << endl;

 

Second, copy the contents of the comparison

1  // Array ... 
2  char A [] = " Hello " ; // A global (static) initialize the memory area "hello" in the constant store
 . 3  char B [ 10 ]; // B uninitialized global (static) store
 . 4 strcpy (B, A); // not with A = B; 
. 5  IF (strcmp (B, A) == 0 ) // not with IF (A == B) 
. 6  ...
 . 7  // pointer ... 
8  int len = strlen (A);
 . 9  char * P = ( char *) the malloc ( the sizeof ( char ) * (len + . 1)); // p applications must give a capacity of a memory
 10 strcpy (p, A); // do not use p = a; // Assignment 
. 11  IF (strcmp (p, A) == 0 ) // do not use if (p == a) // If the latter is the address comparator 
12 ...

 

Array name can not be copied directly and compared. If you want to copy the contents of the array to array a b, the statement can not be used  b = a , otherwise it will produce a compilation error. You should use standard library functions strcpyfor replication. Similarly, comparing the content of a and b are the same, can not be used if(b==a) , should be determined in a standard library function strcmpcompared.
  Statement  p = a is not able to copy the contents of a pointer p, but to assign the address of a p. To copy a content, you can first use the library functions mallocto apply for a capacity of p strlen(a)+1-character memory, and then strcpyString copy. Similarly, the statement if(p==a) is not the content but the address should be used library function compared strcmpto compare.

Third, the calculated memory capacity

Using operators sizeofcan calculate the capacity of the array (the number of bytes). In the following example, sizeof(a)the value is 12 (note Remember ''). Pointer p pointing to a, but the sizeof(p)value is 4. This is because sizeof(p)to obtain the number of bytes of a pointer variable, equivalent to sizeof(char*), but not within the meaning of p memory. C ++ / C language has no way of knowing pointer memory capacity, unless remember it at the time of application memory.

 

1 char a[] = "hello world";
2 char *p = a;
3 cout<< sizeof(a) << endl; // 12字节
4 cout<< sizeof(p) << endl; // 4字节

 

 

 

Note that when the array is passed as a parameter when the array is automatically reduced to the same type of pointer. In the following example, regardless of how much the capacity of the array a, sizeof(a)always equal sizeof(char *).

. 1  void Func ( char A [ 100 ]) {
 2      COUT << the sizeof (A) << endl; // . 4 bytes instead of 100 bytes 
3 }

 

Fourth, it is how to pass the pointer argument

If the parameter is a pointer to a function, do not expect to use the pointer to apply dynamic memory. In the following example, the statement Test function GetMemory(str, 200)does not make strmemory to obtain the desired, strstill NULL, why?

. 1  void GetMemory ( char * p, int NUM) {
 2      p = ( char *) the malloc ( the sizeof ( char ) * NUM); // first p_ assigned as a temporary copy of the stack of p, pointing p_ p when p = (char *) malloc ( size (char) * num), to modify the actual memory pointed p_, 
                           // now p_ i.e. directed by malloc newly allocated heap memory. P but had not changed. So the function GetMemory not output anything, and each will perform a GetMemory a memory leak, because there is no free fall
                           @ p_ memory heap opened
. 3 } . 4 void the Test ( void ) { . 5 char * STR = NULL ; . 6 GetMemory (STR, 100 ); //str still NULL 7 strcpy (str, " the Hello " ); // run error 8 }

 

If you have to use a memory pointer parameter to apply, you should use the "pointer to a pointer", see Example:

. 1  void GetMemory2 ( char ** P, int NUM) {
 2      * P = ( char *) the malloc ( the sizeof ( char ) * NUM); // if incoming pointer is a pointer, i.e., a memory area, stores a value that is a pointer, it points to another area of memory. This is for p allocated on the heap a new memory
 . 3  }
 . 4  void (Test2 void ) {
 . 5      char * STR = NULL;
 . 6      GetMemory2 (& str, 100 ); // Note that the argument is & str, instead str str is located in a pointer on the stack, & str is a pointer and allocate on the stack, the pointer to str. 
. 7      strcpy (STR, " Hello " );
 8     cout<< str << endl;
 9     
10     free(str);
11 }

 

Guess you like

Origin www.cnblogs.com/ccpang/p/11307415.html