The source of memory in C language Program: Stack data segment

Source: Internet
Author: User

Program at run time, the source of its memory mainly through three methods: Stack data segment, generally speaking, the stack is generally used to store small memory local variables, heap memory and data segment properties are similar, in use, if the variable is accompanied by the program has been present, the use of global variables, that is, in the data section, If a variable is used, then it is appropriate to use heap memory (apply first, then release),


One: Stack:

1: The stack in use is the compiler automatically allocate memory space, do not need the programmer's interference, and then the size of the stack is limited, so when we define the variables need large amount of memory is not suitable for the use of the stack,

2: The stack is a normal variable, the stack is used repeatedly, so the stack memory is dirty, in the definition of ordinary variables, if the variable is not initialized, then the value of the variable is random.

3: The stack is advanced, and its memory space is growing downward.


Second: Heap (heap):

1: The heap is used by the programmer to operate, the programmer through malloc to the memory of the heap memory, the use of free after the release of this part of memory, if this part of memory after the use of memory is not released, then this part of the memory manager will think this part of memory has been occupied, The embodiment of the program to eat memory, which is called a memory leak, if the memory is not freed, then the memory is occupied only when the program is finished will not be released. Generally need a large amount of memory to use the heap, heap is first-out, and its memory space is upward growth.

code example:

#include <stdio.h>
#include <stdlib.h>
int main (void)
{
Request Memory
int *p = (int *) malloc (100);
if (p = = NULL)//Error checking
{
printf ("Error \ n");
return-1;
}
Memory usage S
* (p+4) = 1024;
* (p+3000) = 111;
printf ("* (p+3) =%d\n", * (p+4));
printf ("* (p+30000)) =%d\n", * (p+3000));
printf ("p =%p \ n", p);
printf ("(p +4) =%p \ n", (P +4));

Free (p); Freeing memory

printf ("* (p+3) =%d\n", * (p+4));
printf ("* (p+30000)) =%d\n", * (p+3000));
printf ("p =%p \ n", p);
printf ("(p +4) =%p \ n", (P +4));

p = NULL; Avoid wild hands

return 0;
}

Operation Result:

* (p+3) = 1024

* (p+30000)) = 111

p = 0x8efb008

(p +4) = 0x8efb018

* (p+3) = 1024

* (p+30000)) = 111

p = 0x8efb008

(p +4) = 0x8efb018

Analysis:

1: Heap memory can be accessed across the border, but in practice it is best not to, because when you use the cross-border access means stepping on someone else

2: The heap exists after the release can also be accessed, and the value of the access is still unchanged, indicating that the heap memory is also dirty, heap memory is released when the use of the non-existent cleanup.


Three: Data segment (. Data)

1: A program has data segment (. data) code snippet and BSS segment, different segments have different segment properties

Data segment: (also called data area, static data area, static area) data segment is stored in the program in the initialization of the global variables (not including the global variables initialized to 0), but also need to note that the global variable is the program's data, local variables are not program variables, just function data

Code Snippets: Code Snippets are executable parts of your program, and intuitive understanding of code snippets is composed of function stacks. The code snippet is read-only.

BSS segment: (also known as the Zero initial segment) The BSS segment holds the global variable that has been initialized to 0 for the display initialization (the default global variable in C is initialized to 0), so it can be understood that the BSS segment is the data segment initialized to 0.

2: There are two variables in the. Data segment: The first is explicitly initialized to a global variable other than 0, and the other is a static local variable, which is a local variable that is statically modified (the normal variable is allocated on the stack, and the static local variable is distributed in the. Data segment)



IV: Special data in a code snippet

1: C language uses char *p = "Linux"; When you define a string, the string "Linux" is actually assigned to the code snippet, in other words, the string "Linux" here is actually a constant string instead of a variable string.

2:const modified type constants, const implementation methods are mainly two, one is to compile the const-modified variables in the code snippet to implement non-modifiable (because the code snippet is read-only), and the other is the compiler to check to ensure that the Const modifier constants, But this kind of actual and ordinary constant sample, is also placed in the data section, so if you can implement the modification by bypassing the compiler, the concrete implementation method is to use the pointer to point to the constant memory space, and then dereference to modify the constant, which is how GCC is implemented.






The source of memory in C language Program: Stack data segment

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.