NOTES: Program memory management. BSS. Data. rodata. Text Stack heap

Source: Internet
Author: User

1. Uninitialized global variable (. BSS segment)

The BSS segment is used to store global variables that have not been initialized and have been initialized to 0 . The following example code:

#include <stdio.h>int bss_array[1024*1024];int Main (int argc, char *argv[]) {      return 0;}

Compile and view:

$ gcc-g mainbss.c-o mainbss$ ls-l mainbss-rwxrwxr-x. 1 hy hy 8330 Apr 19:33 mainbss$ objdump-h mainbss |grep BSSMAINBSS:     file format elf32-i386. BSS          00400020
   0804a020  0804a020  00001018  2**5
$ size MAINBSS   text   data    BSS    Dec    hexfilename   1055    27241943364195663 40054fmainbss

The global variable Bss_array has a size of 4MB = 1024*1024*sizeof (int) byte = 4194304 byte. Viewing data by size is a BSS segment

and the executable file MAINBSS has about 8KB. It isknown that the global variables of the BSS type only occupy the runtime's memory space and do not occupy the file space of the executable file itself .

2. A global variable (. Data segment) that has been initialized to a nonzero

The data segment is used to hold global variables that have been initialized to non-0 . The following code, which only initializes the first element of the matrix to 1:

#include <stdio.h>int data_array[1024*1024]={1};int Main (int argc, char *argv[]) {     return 0;}

Compile view

[Email protected] memcfg]$ gcc-g maindata.c-o maindata[[email protected] memcfg]$ ls-l maindata-rwxrwxr-x. 1 hy hy 4202682 Apr 19:48 maindata[[email protected] memcfg]$ objdump-h maindata |grep \\.data. Data         00400020
   0804a020  0804a020  00001020  2**5[[email protected] memcfg]$ size maindata   text   data    BSS    Dec    hexfilename   10554194604      

and the executable file Maindata has about 4MB. See the data segment by size

It is known thatthe global variables of the data type occupy both the runtime memory space and the file space of the executable file itself .

3. Constant data (. Rodata segment)

1) Rodata is used to store constant data. Ro:read only

2) The string will be automatically placed in the Rodata by the compiler, and constant data for the Const keyword will be placed in the Rodata

3) In some embedded systems, the Rodata is placed in the ROM (or nor Flash), the runtime is read directly, do not have to be loaded into RAM memory.

Therefore, in the embedded development, the known constant coefficients, tabular data, and so on are often used to watchmaking the const keyword. The ROM is present, avoiding the use of RAM space.

4. Code (. Text field)

The text segment holds code (such as: functions) and some integer constants (which should refer to some immediate numbers), which are executable.

5. Stacks (Stack)

1) Local variables and function parameters for stack storage functions

2) The parameters and return values of the called function are stored in the stack area of the current program, and then the function is called to allocate space on the stack for its own automatic variables and temporary variables.

3) When the function returns, the data of the stack area will be released, in the order of first in and out (FILO).

6. Heaps (heap)

The heap is used to dynamically allocate memory and is determined by the program itself to open and release.

6.1 Malloc/free
#include <stdio.h> #include <stdlib.h>int main (int argc, char *argv[]) {    int *p = (int *) malloc (10*1);    p= (int *) malloc (10*1);    if (p==null) {        printf ("malloc p err\n");        return-1;    }    Free (p);    printf ("p =%4x\n", p);    p = NULL;    printf ("p =%4x\n", p);    return 0;}

Program Run Result:

[[email protected] memcfg]$ gcc maindata.c[[email protected] memcfg]$./a.out p = 9cf4008p =    0

1) Open up space, it is necessary to release the timely. When released, the pointer should point to the memory space at the time of opening, so when using pointers, be careful not to modify their addresses or save the start-up address.

2) When the pointer is free, its address is not necessarily null. As in code p, after free (p), printf (p) is not 0. It is recommended to add a p=null immediately after free (p).

3) Check P's address if (P!=null) {...}

6.2 Calloc/realloc

1) calloc () function

void *calloc (size_t nmemb, size_t size);

The parameter nmemb represents the number of elements to allocate, size for each element, and the amount of memory space to divide by nmemb*size; The return value is a pointer to the void* type, pointing to the allocated memory header address.

Usage one: Allocates memory of 1024*sizeof (int) bytes size and empties to 0

int *p = (int *) calloc (1024,sizeof (int));

Usage II: malloc usage equivalent to Alloc

int *p = (int *) malloc (1024*sizeof (int)); memset (p,0,1024*sizeof (int));

Variance: Use one, which is initialized to 0 based on the type of assignment, such as assigning int, initialized to (int) 0, or null pointer if the pointer type, or, if floating point, is initialized to floating-point.

Usage two, is not guaranteed to be null pointer value and floating point type. (related to the definition of null constants and floating-point types)

2) realloc () function

ReAlloc () is used to reallocate the size of a piece of memory that is in use.

Defined:

void *realloc (void *ptr, size_t size);

Usage examples:

int *p = (int *) malloc (1024x768);    p = (int *) realloc (+);        Reassign to 512-byte size memory, reduce data loss p = (int *) realloc (2048);      Reassign to 2048-byte size memory    

Note: The start address of the memory space after realloc () adjustment may be different from the original.

Excerpt from a book

"Arm embedded Linux system development detailed" Chanley and other authoring

"High quality embedded Linux C programming" Liang and other authoring

NOTES: Program memory management. BSS. Data. rodata. Text Stack heap

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.