C vs. C + + memory mechanism comparison

Source: Internet
Author: User

Tag: Memory, Heap, stack

The C language is very similar to C + + memory, which is why I have been confused about him; here's a look at the difference between them:

1, first speak C language memory mechanism

    • Stack local variables (including function arguments) within the function are assigned and freed by the compiler, the function ends, and the stack variable is invalidated;

    • Heap The programmer allocates space with malloc ()/calloc ()/realloc () and free () frees the requested space. If the programmer forgets free (), it will cause a memory leak, which may be recycled by the operating system at the end of the program, or it may be occupied until shutdown.

    • Global Zone/static zone global variables and static variable storage areas where the program is compiled and the region exists. And the global variables and static variables initialized in the C language and uninitialized are placed in the adjacent two regions ( in C + +, because the compiler will automatically initialize the global variables and static variables assigned values, so there is no distinction). Because global variables occupy memory space and are difficult to maintain, it is recommended to use less. Released at the end of the program.

    • C-style string constant store where the string constants are stored, the program is released at the end;

    • Program code area the area where the binary code of the program is stored.


2, plus the memory mechanism of C + +

    • Stack local variables (including function arguments) within the function are allocated by the compiler, the function ends, and the stack variable is invalidated.

    • Heap Unlike C here, the heap is the memory that is applied by new and is released by the delete.

    • Free Storage Area The programmer allocates space with malloc ()/calloc ()/realloc () and is freed by free (). If the programmer forgets free (), it will cause a memory leak, and the program may end up with an operating system recycle, or it may be occupied until shutdown. corresponds to the heap mechanism of C.

    • Global Zone/static zone global variables and static variable storage areas where the program is compiled and the region exists. in C + +, initialization and uninitialized variables are not distinguished because the compiler automatically initializes assignments to global variables and static variables. because global variables occupy memory space and are difficult to maintain, it is recommended to use less. Released at the end of the program.

    • Constant Storage Area This is a special storage area that is dedicated to storing constants that cannot be modified (and of course, if you change with an abnormal means).



Below is a chestnut, comparing C with C + + in the global zone/Static zone (Linux):

#include <stdio.h>static int a;int b;int c = 1;int Main (void) {return 0;}

Compile the file into an executable file and print the size of the file:

As you can see, the data segment, which does not include BSS, is 252,BSS (uninitialized data segment) is 16;

The next step is to modify the test code (Initialize B):

#include <stdio.h>static int A;int b = 1;int c = 1;int Main (void) {return 0;}

Compile the file into an executable file and print the size of the file:

As you can see, the data segment, which does not include BSS, is 252+4, and BSS (uninitialized data segment) is 12 (16-4);


In contrast to the above code, data increases by 4, which is just 4 less BSS. It is now possible to determine that the initialization of variables in the global/static zone in the C language is placed in different regions for initialization.

Next look at the results of the C + + run:

b not manually initialized (int b;):

b manually initialized to 0 (int b = 0):

above in C + +, the data section has no change; therefore, in C language, the global variables are divided into initialization and uninitialized, in C + + There is no such distinction, they occupy the same piece of memory area together.


However, there is a very small detail for the initialization of B, if manually initialized to a number other than 0, the printed data segment is the same as the C language:

b initialized to 1:

b not initialized:


The above problems, what is the reason, has not been found out.

The possible causes are:

A BSS segment (BSS segment) usually refers to an area of memory that is used to hold uninitialized global variables in the program. (0 is not initialized)
Data segment typically refers to a chunk of memory that is used to hold a global variable that is initialized and not 0 in the program. (true initialization only for numbers initialized to 0)

Specific reasons for not knowing, just guessing.


C vs. C + + memory mechanism comparison

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.