Detail the memory four region model and structure's use of memory in C language

  • 2020-05-09 18:56:50
  • OfStack

Memory 4 areas
1. Code area
Code area code, when the program is loaded into memory by the operating system, all executable code is loaded into the code area, also known as code segment, which can not be modified during the run.
2. Static area
All global variables and static variables in the program are stored in the static area.
3, the stack area
Stack stack is an advanced memory structure, all automatic variables, function parameters are automatically out of the stack by the compiler, when an automatic variable out of its scope, automatically pop out of the stack. For automatic variables, when to push and when to push is not programmatically controlled by the C language compiler. The implementation stack is not very large, 1 is usually in K units.
When the stack is full, but still pushing variables into the stack, this is called a stack. Overflow for one 32-bit operating system, the maximum management is 4G memory, of which 1G is for the operating system for its own use, and the remaining 3G is for the user program. One user program can theoretically use the memory space of 3G.
Note: the order in which function arguments are pushed in C is from right to left.
4, heap area
Heap heap, like stack 1, is also a memory area that can be modified at any time in the process of program running, but there is no stack in that order. The heap is a large container with a much larger capacity than the stack, but in C, the request and release of heap memory space is done manually by code.
Code example:


#include <stdio.h> 
 
int c = 0; //  Static area  
 
void test(int a, int b) //  parameter a . b In the stack  
{ 
  printf("%d, %d\n", &a, &b); 
} 
 
int *geta() //  The return value of the function is 1 A pointer to the  
{ 
  int a = 100; //  The stack area  
  return &a; 
} // int a The scope of theta is this {} 
 
int main() 
{ 
  int *p = geta(); //  To get here 1 The address of the temporary stack variable in the function geta The call is invalid after completion  
  *p = 100; 
  printf("%d\n", *p); 
  static int d = 0; //  Static area  
  int a = 0; //  The stack area  
  int b = 0; 
 
  printf("%d, %d, %d, %d, %d\n", &a, &b, &c, &d, main); 
  test(a, b); 
  return 0; 
} 
 
/* 
 The output  
100 
2619740, 2619728, 9404720, 9404724, 9376059 
2619512, 2619516 
*/ 

Heap usage precautions:


#include <stdio.h> 
#include <stdlib.h> 
 
int *geta() //  Error, will not 1 The addresses of the stack variables are returned by the return value of the function  
{ 
  int a = 0; 
  return &a; 
} 
 
int *geta1() //  It can be returned by the return value of the function 1 Heap addresses, but remember, 1 Need to free 
{ 
  int *p = (int *)malloc(sizeof(int)); //  To apply for the 1 A heap  
  return p; 
} 
 
int *geta2() //  Legal, but remember you can't use it here free 
{ 
  static int a = 0; //  The variable is in the static region, while the program is running 1 Straight there  
  return &a; 
} 
 
void getHeap(int *p) 
{ 
  printf("p = %p\n", &p); 
  p = (int *)malloc(sizeof(int) * 10); 
} // getHeap And when you're done, p It disappears, causing the address number of the specific heap space he points to to disappear  
//  A memory leak has occurred here  
 
void getHeap1(int **p) 
{ 
  *p = (int *)malloc(sizeof(int) * 10); 
} //  This is the right thing to do  
 
int main() 
{ 
  int *p = NULL; 
  printf("p = %p\n", &p); 
  getHeap(p); //  The argument has not changed at all  
  getHeap1(&p); //  Gets the address of the heap memory  
  printf("p = %d\n", p); 
 
  p[0] = 1; 
  p[1] = 2; 
  printf("p[0] = %d, p[1] = %d\n", p[0], p[1]); 
  free(p); 
 
  return 0; 
} 


The alignment pattern exists within the structure

Structure in the body of the alignment mode in detail


#include <stdio.h> 
 
struct A 
{ 
  int a; //  Now the structure is occupied 4 bytes  
  char b; //  Now the structure is occupied 8 bytes  
  char c; //  or 8 bytes  
  char d; //  or 8 bytes  
  char e; //  or 8 bytes  
  char f; //  Now it is 12 bytes   
}; 
 
struct B 
{ 
  char a; // 1 bytes  
  char b; // 2 bytes  
  char c; // 3 bytes  
}; 
 
struct c 
{ 
  char name[10]; // 10 bytes  
  char a; // 11 bytes  
  //  for char For type array, each element of the array is treated as 1 a char type  
}; 
 
struct d 
{ 
  int name[10]; // 40 bytes  
  char a; // 44 bytes  
  char b; // 44 bytes  
}; 
 
struct e 
{ 
  char a; // 1 bytes  
  int b; // 8 bytes  
  char c; // 12 bytes  
  //  This way of writing compares to the memory consumption A Will become big  
}; 
 
struct f 
{ 
  char a; // 1 
  short b; // 4 Note that there short It's left over 3 Of the last two bytes  
  //  Memory alignment is always at 2 Multiple alignment  
  char c; //  So this is 6 
  int d; // 12 
  short e; // 16 
  char f; // 16 
}; 


The struct implements array assignment in a disguised form


struct name 
{ 
  char array[10]; 
}; 
 
int main() 
{ 
  char name1[10] = "name1"; 
  char name2[20] = "name2"; 
  name1 = name2; //  This is an error. You can't assign between arrays  
  struct name a1 = { "hello" }; 
  struct name a2 = { 0 }; 
  a2 = a1; //  Here the array assignment is achieved by means of the properties that the struct can assign  
  return 0; 
} 


There are leaks in the structure


#include <stdio.h> 
#include <stdlib.h> 
 
union A 
{ 
  char a; 
  char *b; //  Pointer members of the union should be especially careful  
}; 
 
int main() 
{ 
  A a; 
  a.b = (char *)malloc(10); // b Points to the 1 The address of the heap  
  //  If there are pointer members in the union, then 1 Must use up this pointer, and free Only after the pointer can any other member be used  
  a.a = 10; // b The value of theta is also equal to theta 10 the  
  free(b); //  At this time to release b It's wrong because it's up here 1 Line of a At the time of assignment, the b A memory leak was caused when the value of  
  return 0; 
} 


Related articles: