In the analysis of the Code of the pointer, the mind must have a memory allocation diagram, used to analyze the various variables stored.
1. Use variable A to give the following definition
A) An integer number (an integer)
b) A pointer to the integer number (a pointer to an integer)
c) A pointer to a pointer to a pointer that points to a number of integers (a pointer to a pointer to an intege) r
d) An array of 10 integers (an arrays of ten integers)
e) An array of 10 pointers that point to an integer number. (An array of ten pointers to integers)
f) A pointer to the array of 10 integer numbers (a pointer to an array of ten integers)
g) A pointer to the function that has an integer parameter and returns an integer number (a pointer to a function this takes an integer as an argument and returns an integer)
h) An array of 10 pointers that point to a function that has an integer parameter and returns an integer number (an array of ten pointers to functions that taking an integer argument and ret Urn an integer)
The answer is:
a) int A; An integer
b) int *a; A Pointer to an integer
c) int **a; A pointer to a pointer to an integer
d) int a[10]; An array of ten integers
e) int *a[10]; An array of ten pointers to integers
f) Int (*a) [10]; A pointer to an array of ten integers
g) Int (*a) (int); A pointer to a function A, takes an integer argument and returns an integer
h) int (*a[10]) (int); An array of ten pointers to functions A, a integer argument and return an integer
Some problems when the C-language function returns a pointer type
1.
#include <stdio.h>char *returnstr () { char *p = "Tigerjibo"; return p;} int Main () { char*str; =returnstr (); // str[0]= ' T '; Will cause an error, cannot modify the contents of the read-only data segment printf ("%s\n", str); Return0;}
To analyze the program.
(1) Char *p = "Tigerjibo". The system allocates four bytes of space on the stack to hold the value of P. "Tigerjibo" is a character constant that is stored in a read-only data segment. After pointing, the system assigns the address of "Tigerjibo" to P.
(2) The function returns the value of P with return. The value points to the read-only data segment (the data in that segment is static and does not change). After exiting the sub-function, the system destroys the value of P. But the value of P has been returned by return. and the content in the read-only data segment is not modified and recycled (it is lost in the static region)
(3) The address is given to STR in the main program. So str points to "Tigerjbo".
(4) Although the program can run, bear another disadvantage, is that in the program can not modify the number of characters in the constant value. If the modification causes a segment error.
2.
#include <stdio.h>Char *returnstr () { char p[]="Tigerjibo"; return p;} int Main () { char *str; =returstr (); printf ("%s\n", str);}
After compiling the program, the system will prompt the following warning:
function returns address of local variable
(function returns a variable address)
Parse the error:
1> "Tigerjibo" is a character constant that is stored in a read-only data segment and cannot be modified.
2>char p[], is a local variable that, when called, opens up a space on the stack to hold the contents of the array p.
3>char p[]= "Tigerjibo", the statement is to assign the value of "Tigerjibo" to the number p, stored at the array p address. Instead of assigning the address "Tigerjibo" to the array p. Therefore, "Tigerjibo" has a backup in the system at this time, one in the read-only data segment (cannot be modified, the content will not be recycled), one on the stack (can modify the content, but after the function exits, the contents of its stack will be recycled).
4> therefore, when return p returns the first address of the array, but when the function exits, the contents of its stack are discarded, and the memory of the local variable is emptied, so the content at the first address of the array is a variable value.
3.
#include <stdio.h>Char *returnstr () { static char p[]=" Tigerjibo "; return p;} int Main () { char *str; =returnstr (); str[0]=' T '; printf ("%s\n", str);}
This program is running correctly.
The analysis is as follows:
1> "Tigerjibo" is a character constant that is stored in a read-only data segment and cannot be modified.
2>static Char p[], is a static local variable that opens up a space in the read-write data segment for p to hold its value.
3>static Char p[]= "Tigerjibo", which assigns the value of "Tigerjibo" to the number p, which is stored at the array p address. Instead of assigning the address "Tigerjibo" to the array p. Therefore, "Tigerjibo" has a backup in the system at this time, one in the read-only data segment (cannot be modified, the content will not be recycled), one in the read-write data segment (can modify its contents, when the function exits, because it is stored in the read and write data segment, the content will not be discarded).
4> therefore, when return p returns the first address of the array, but when the function exits, the contents of the stack are cleared, but the P address is the address in the read-write data segment, and the content on it is not recycled.
The following knowledge is added here:
Char day[15] = "ABCDEFGHIJKLMN"; When this statement executes, the system allocates a length of 15 memory, and the memory is named Day with the value "ABCDEFGHIJKLMN" as shown:
For char* strtmp = "opqrstuvwxyz", after this statement executes, the memory of the string pointer strtmp is shown below:
4.
#include <stdio.h>#include<string.h>#include<strdlib.h>void getmemory (char *p) { = (char *)malloc(+);} int Main () { char *str=NULL; GetMemory (str); strcpy (str, "HelloWorld"); printf ("%s\n", str);}
Post-Compilation error: Segment error
Analysis: In the main program, the STR address is empty. In the function pass, the address of the STR is passed to the pointer P (which is a copy) of the child function, and then in the Word function, p is applied for a 100-byte space on the heap, and the first address is assigned to P. However, in a function pass, the P-value change does not affect the value of STR in the main function. Therefore, the address of STR is still empty. A segment error occurs when referencing a null pointer in strcpy.
If STR is not initialized to NULL, instead of a char string, the P = (char *) malloc (100) is called, before the value of P is as follows:
When p = (char *) malloc (100) is called, then the value of P is as follows:
Visible, Str still points to the original address
5.
voidGetMemory2 (Char**p,intnum) { *p = (Char*)malloc(sizeof(Char) *num); } voidTest2 (void){ Char*str =NULL; GetMemory2 (&STR, -);//Note that the parameter is &STR, not strstrcpy (str,"Hello"); cout<< Str <<Endl; Free(str); }
----------------------------------&str is the address of the pointer, passing the address of the pointer to the formal parameter p, p also points to STR,
So *p = (char *) malloc (sizeof (char) * num); that is, the STR that P points to is allocated memory, so it is correct. (Personal opinion)
If STR is not initialized to NULL, instead of a char string, the P = (char *) malloc (100) is called, before the value of P is as follows:
When p = (char *) malloc (100) is called, then the value of P is as follows:
6.
Char*getmemory3 (intnum) { Char*p = (Char*)malloc(sizeof(Char) *num); returnp;} voidTEST3 (void) { Char*str =NULL; STR= GetMemory3 ( -); strcpy (str,"Hello"); cout<< Str <<Endl; Free(str); }
----------------------------correct
Before "Return P":
After calling GetMemory:
7.
char *getstring (void char p[] = hello World " return p; // void NULL; str = GetString (); // cout<< str << Endl;}
Do not return a pointer to "stack memory" with a return statement because the memory automatically dies at the end of the function;
Before "Return P":
After "return P", the stack has been destroyed by following the stop of the child function, so:
8.
Char *getstring2 (void) { char'helloworld' ; return p;} void TEST5 (void) { char *str = NULL; = GetString2 (); cout<< str <<
The function Test5 runs without errors, but the design concept of the function GetString2 is wrong. Because the "Hello World" in GetString2 is a constant string, it is located in a static store, which is constant throughout the lifetime of the program. Whenever GetString2 is called, it always returns the same "read-only" block of memory.
Before "Return P":
After "Return P":
9.
void Test (void) { char *p = (charmalloc() ; strcpy (P, "Hello"); Free (p); // The memory referred to by P is freed, but the address referred to by P remains unchanged ..... if (P! = NULL) // does not play a role in error prevention { strcpy (p, "World"); // Error } }
In "Free (p);" Before:
In "Free (p);" After:
That is, although the "Hello" is located in the heap of 100 space has been eliminated, but the value of P is still 0x10006000, is a wild pointer, can not be used. It is recommended to add the following code after free:
P=null;
C Language pointer related questions