Analysis of memory allocation in C language

Source: Internet
Author: User

Example 1:

void Myfun (int x);    // declaration may also be written as: void myfun (int);  int Main () {myfun (+);  0;} void myfun (int x) {printf ("myfun:%d\n", x);}       
We start with a functional or mathematical understanding of the Myfun function, knowing that myfun function name represents a feature (or a piece of code). What is the name of the function? function pointer variableThe memory address of a data variable can be stored in the corresponding pointer variable, and the first address of the function is stored in a function pointer variable.     In this way, I can invoke the function pointed to by this function pointer variable. In a C-series language, any variable is always declared before it can be used.     function pointer variables should also be declared first.   Declaration of a function pointer variable: void (*FUNP) (int);    Declares a function pointer variable that points to the same parameter and return value. (The declaration format of the entire function pointer variable is just like the declaration of the function myfun, except--we change the myfun to (*FUNP), so we have a pointer to the Myfun function.) Of course, this FUNP pointer variable can also point to all other functions that have the same parameters and return values. ) Example 2:
#include <stdio.h>#include <stdlib.h>void (*FUNP) (int);//Declarations can also be written as void (*FUNP) (int x), but generally not.void (*funa) (Int);void Myfun (int x);//Declarations can also be written as: void myfun (int);IntMain () {//The General function call Myfun (100);//The type relationship between Myfun and FUNP is similar to the relationship between int and int *. funp=&myfun;//Assign the address of the Myfun function to the FUNP variable (*FUNP) (200);//To invoke a function by a function pointer variable//myfun and Funa is similar to the relationship between int and int. Funa=myfun; Funa (300// Three seemingly deranged calls FUNP (400< Span style= "color: #000000;" >); (*funa) (6001000return 0; void myfun (int X) { printf ( "myfun:%d\n,x)}                 

Output:

Summary:1, in fact, Myfun function name and FUNP, Funa function pointers are the same, that is, the function pointer. The Myfun function name is a function pointer constant, and FUNP, Funa is the function number pointer variable, which is their relationship. 2, but if the function name is called (*myfun) (10), then it is inconvenient and unaccustomed to write and read. So the designers of C are designed to allow Myfun (10) to be called in this form (much more convenient, as in the form of functions in mathematics). 3, in order to unify the call way, the FUNP function pointer variable can also FUNP (10) the form to call. 4, when assigned, can be written in funp=&myfun form, can also be written as Funp=myfun. 5. However, when declaring, void myfun (int) cannot be written as void (*myfun) (int). void (*FUNP) (int) cannot be written as void funp (int). 6, Function pointer variable can also be stored in an array. Method of declaring an array: Int (*farray[10]) (int); Example 3
#include <stdio.h>#include <stdlib.h>void (*FUNP) (Int);void (*funa) (Int);void Myfun (Intx);IntMain () {funp=&Myfun;//In-depth understanding of printf ("sizeof (Myfun) =%d\n",sizeof(Myfun)); printf"sizeof (FUNP) =%d\n",sizeof(FUNP)); printf "myfun\t 0x%p=0x%p\n" , &myfun,myfun); printf ( "funp\t 0x%p=0x%p\n FUNP,FUNP); printf ( "funa\t 0x%p=0x%p\n funa,funa); return 0; void myfun (int X) { printf ( "myfun:%d\n,x)}                 

Output:

Summary:1, the function pointer variable is the same as the normal pointer in the 32-bit system size is 4. However, the size of the function pointer constant is 1.2, the function pointer variable, and the function pointer constant are stored in different locations in memory. 3. The value of the function pointer variable (global) that is negative is 0. function pointers as arguments to a functionSince a function pointer variable is a variable, it can also be used as a parameter of a function. Example:
#include <stdio.h>#include <stdlib.h>typedefvoid (*funtype) (Int);//Before adding a typedef keyword, this defines a pointer type named Funtype function instead of a funtype variable.//form with typedef int* PINT;void Myfun (Intx);void Hisfun (Intx);void Herfun (Intx);void Callfun (Funtype FP,Intx);IntMain () {Callfun (myfun,100);//Pass in the function pointer constant, as the callback function Callfun (Hisfun,200); Callfun (Herfun,300);Return0;}void Callfun (Funtype FP,Intx) {fp (x);//The function passed in by the FP pointer is executed, note that the function referred to in the FP has a parameter}void myfun (int x) {printf ( "myfun:%d\n  ",x);} void hisfun (int X) { printf ( "hisfun:%d\n" ,x);} void herfun (int X) { printf ( "herfun:%d\n" ,x);             

Output:

Analysis of memory allocation in C language

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.