C Memory Management
A four-zone memory
Code area stack area static variable area
Scope
The scope of a C-language variable can be either a code block scope function scope or a file scope.
The code block is a piece of code between {}.
Auto Automatic variable
In general, variables defined inside a code block are automatic variables. Of course can also be displayed using the AOTU keyword
Register variable of register
Usually variables in memory if you can put the variables in the CPU registers, the code execution efficiency will be higher
register int I;
Static variables for code block scopes
A static variable is a variable that the memory location does not change during program execution. A static variable inside a block of code can only be accessed within this block of code.
Static variables outside the scope of the code block
A static variable that is outside the code block persists during program execution but can only be accessed by a file that defines the variable
Global Variables
Global variables are stored in the same way as static variables but can be accessed by multiple files
external variables and extern keywords
extern int I;
global functions and static functions
In the C language, the function defaults to global use of the keyword static to declare the function as static
four areas of memory
Code Area
Code area codes program is loaded into memory by the operating system, all executable code is loaded into the code area also called code snippet this block of memory can not be modified during the run.
Static Zone
All global variables and static variables in the program are stored in the static area, comparing the following two code differences
int a = 0;
int main ()
{
static int b = 0;
printf ("%p,%p\n", &a, &b);
return 0;
}int a = 0;
static int b = 0;
int main ()
{
printf ("%p,%p\n", &a, &b);
return 0;
}
Stack Area
Stack stack is an advanced memory structure all the parameters of the automatic variable function are automatically released by the compiler when an automatic variable goes out of its scope and automatically pops out of the stack.
Heap Area
Heap heap is also a stack of memory areas that can be modified at any time while the program is running, but not as advanced as the stack.
A heap is a large container whose capacity is much larger than the stack, but the application and release of heap memory space in C language needs to be done manually through code.
Allocation and release of 14.3 heap
14.3.1malloc
14.3.2Free
Parameter scope
#include <stdio.h>int main () { int *p = null; //is actually an automatic variable auto int *p { int i =100; p = &i; } //int arr[9] = {65535}; //This statement may overwrite the memory area of the I just released printf ("%d \n", *p); //an unreliable operation because the point of *p after the code scope is released is likely to be occupied by other variables return 0;} [email protected]:~/pointer$ gcc -std=c99 main.c && ./a.out100 # Include <stdio.h>int main () { int *p = null; //is actually an automatic variable auto int *p { int i =100; p = &i; } int arr[9] = {65535}; //This statement may overwrite the memory area of the I just released printf ("%d \n", *p); //an unreliable operation because *p points are likely to be occupied by other variables after the code scope is released above return 0;} [email protected]:~/pointer$ gcc -std=c99 main.c && ./a.out65535
A static variable is a variable that is not changed by the memory location during program execution. A static variable inside a block of code can only be accessed within this block of code.
#include <stdio.h>void fun () {static int i = 0; printf ("%d \ n", i++);} int main () {fun (); Fun (); Fun (); return 0;} [Email protected]:~/pointer$ gcc-std=c99 main.c &&./a.out0 1 2
Stack size
My system
[Email protected]:~/pointer$ Lsb_release-ano LSB modules is available. Distributor ID:UbuntuDescription:Ubuntu 14.04.3 ltsrelease:14.04codename:trusty[email protected]:~/pointer$ uname- Rm3.19.0-25-generic x86_#include <stdio.h>int Main () {char buf[8185 * 1024] = {};} Can't get any bigger than this.
Application and release of heap memory
#include <stdio.h> #include <stdlib.h>int main () {//int *p = (int *) malloc (sizeof (int)); int *p = (int *) malloc (sizeof (char) *1024); *p = 200; printf ("%d \ n", *p); printf ("%p \ n", p); Free (P);//Please remember to release the memory}[email protected]:~/pointer$ gcc-std=c99 main.c &&./a.out200 0x7f158cec7010
Heap pointer Change direction
#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main () {while (1) { int *p = malloc (1024*1024*100); printf ("%p \ n", p); Sleep (1); }}[email protected]:~/pointer$ gcc-std=c99 main.c &&/a.out0x7fd62f15d010 0x7fd628d5c010 0x7fd62295b010 0x7fd61c55a010 0x7fd616159010 0x7fd60fd58010 0x7fd609957010
function return string problem
#include <stdio.h> #include <stdlib.h>char *test () {char str[] = {"Hello wold!"}; return str;} int main () {char *p = Test (); printf ("%s \ n", p);} Main.c:6:2:warning:function returns address of the local variable [-wreturn-local-addr] return str; ^¤@ does not get the expected string
So why is it possible to return it because the string is a constant
#include <stdio.h> #include <stdlib.h>char *test () {return "Hello world!"; Because this is a constant}int main () {char *p = Test (); printf ("%s \ n", p);}
Heap memory and Function call resolution
Because if the request space inside the function will be processed after the string returned in the storage heap memory is not released
#include <stdio.h> #include <stdlib.h> #include <string.h>char test (char **p) { *p = malloc (char) *20); Change the original level pointer to the address printf ("%p p \n", p); printf ("%p *p \n", *p);} Int main () { char *s = NULL; test (&s); strcpy ( S, "hello world!"); printf ("%s \n", s); printf ("%p s \n", s); free (s);} [email protected]:~/pointer$ gcc -std=c99 main.c && ./ a.out0x7fffcc6b0dc8 p 0x76b010 *p hello world! 0x76b010 s
This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1775750
C Memory Management