Storage Class C language, links and memory management

This is my C Primer Plus a summary of this classic C language is subject to Chapter 12 does not welcome that

int   a  =     3; 

This thing called a identifier created from the hardware point of view is that there is a call identifier takes up a certain physical memory  

There are two nature identifier called a scope (scope) there is a link called (linkage)

A scope

  

         ① block scope

         ② file scope

         ③ Function prototype scope (also called a function scope do not need him)

① block scope           

 

int MAX (int a , int b, int c){        我们可以看到在这个函数里面创建了一个叫t的变量 他只在 这个叫MAX的函数里面起作用  这就叫块作用域(在主函数里面的也是的)
    int t;
 
     if (a>b)
         t=a;
     else
         t=b;
     if (c>t)
         t=c;
     printf ( "%d\n" ,t);
     return  0;
}
② file scope
#include<stdio.h>;
int a = 0; Here we see a is defined outside of all functions, he can direct any function, including a main function directly called file scope;
int main(void)
{
~~~~
}
 
③ function prototype scope
#include<stdio.h>
int main(void)
{
~~~
}
int MAX (~~~) This is called a function prototype scope, do not know how to explain their brain make it ,,,,
 
 

Second connector

① no link

Function prototype scope variables are connectionless, variable that is a function of the connectionless belong to the function itself;

② internal links     

③ external connection

The difference between internal links and external links is to look at the definition of variables plus the time did not increase static;

 

Three storage period

It is said that static storage period is divided into four kinds of thread storage shelf life (I do not understand this matter) are automatically stored in dynamically allocated storage period

 

Static and automatic

#include <stdio.h>

void fn (void) believe that we can tell the difference, right?

{N is an automatic storage period is when fn the end of this function runs he would be released, but because he m static external links so that he can become static storage period, that is, when the end of the program will free up memory

int n = 10;

printf("n=%d\n", n);
n++;
printf("n++=%d\n", n);
}

void fm_static(void)
{
static int m = 10;

printf("static m=%d\n", m);
m++;
printf("m++=%d\n", nm;
}

int main(void)
{
fn();

fm_static();

fn();

fm_static();

return 0;
}

Here are the results

n=10 n++=11 -------------------- static m=10 m++=11 -------------------- n=10 n++=11 -------------------- static m=11 m++=12

 

Dynamic allocation of the storage period

int *a;

a = (int *) malloc (30 * sizeof (int)) mean a pointer to an integer is then distributed to the pointer 30 memory words corresponding to the size of int int a [30];

free (a); meaning that the release of a;

                Dynamically allocated storage period would mean a much longer to decide this variable;

 

Finally, talk about and define const

#const A10 const A think A is a constant;

#define B 10 B think this is variable, but this variable is immutable;

Guess you like

Origin www.cnblogs.com/Bruce-Xie/p/11223549.html