Dynamic memory can be used across functions. Examples and static variables cannot be used across functions.

Source: Internet
Author: User

 

Examples of dynamic memory cross-function usage

 

# Include <stdio. h> # include <malloc. h> void F (INT ** q) {* q = (int *) malloc (sizeof (INT); // sizeof (data type) the returned value is the number of bytes occupied by the data type. // equivalent to P = (int *) malloc (sizeof (INT); // q = 5; // error // * q = 5; // P = 5; ** q = 5; // * P = 5;} int main (void) {int * P; F (& P); printf ("% d \ n", * P); Return 0 ;}

 

A problem occurs (static variables cannot be used across functions. If a function is executed, its space will be released ):

1 # include <stdio. h> 2 3 void F (INT ** q) // Q is a pointer variable. No matter what type of pointer variable q is, only 4 bytes are occupied. 4 {5 Int I = 5; 6 // * q is equivalent to p q and ** Q is not equivalent to p 7 // * q = I; // error because * q = I; is equivalent to P = I; in this case, 8 * q = & I; // P = & I; 9} 10 11 int main (void) 12 {13 int * P; // 13 rows 14 15 F (& P); 16 printf ("% d \ n ", * P); // the syntax of the 16-line statement is correct, but the logic is incorrect. The space of I has been released 17 18 return 0; 19}

 

 

 

 

 

3In the program, you can call the function funTo make mainPointer Variable P in the functionWhich of the following points to a valid integer unit?

A) Main ()

{Int * P;

Fun (P );

...

}

Int fun (int * P)

{Int S;

P = & S;

}

B) Main ()

{Int * P;

Fun (& P );

...

}

Int fun (INT ** q)

{Int S;

* Q = & S;

}

C) # include <stdlib. h>

Main ()

{Int * P;

Fun (& P );

...

}

Int fun (INT ** q)

{* Q = (int *) malloc (4 );

}

D)

# Include <stdlib. h>

Main ()

{Int * P;

Fun (P );

...

}

Int fun (int * P)

{P = (int *) malloc (sizeof (INT ));

}

December 5

Note: This question is very interesting. Tested:

1) pointer usage 2) dynamic memory allocation and automatic variable memory allocation.

The free () function must be called for the dynamically allocated memory to be released. Once the automatic variable jumps out of the scope of its code, it will be automatically released by the compiler.

Let's take a look:

A) no matter how the value of P changes in fun (), the option does not affect the value of P in the main function, because it is a value transfer.

B) The option is to pass the P address & P to the fun () function, but unfortunately, since S is an automatic variable, when the fun () function is introduced, the memory units occupied by the S variable will be released. In this case, the P in the main function still cannot point to a valid int type unit.

C) the parameter int ** P of option fun () indicates that p is a pointer variable pointing to a pointer variable, that is, a pointer. The int * P in the main function indicates that P is only a pointer variable, but & P points to P, and & P is also a pointer variable pointing to the pointer Variable P, the real parameters and the form parameters are of the same type. Fun () enables the real parameter P to point to an int type variable, and because the int type variable is dynamically allocated by malloc (), fun () is introduced () the function does not affect the point of the real parameter P, So C is

D) The option has made the same mistake as. I can't think of second C, but I will still get this knowledge. Haha!

 

 

 

 

 

 

 

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.