Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data section in C++

I have a question to clarify my confusion about the memory organization in computer in C++.

In C++, different data is put in different location. My understanding is like this.

1) data segment section, where global and static data are located;

2) heap section, the objects created by new

3) stack section, the local variable

4) text section, the code itself.

Is that right? Is there anything I missed or did wrong?

Thanks!

like image 454
skydoor Avatar asked Feb 19 '10 16:02

skydoor


2 Answers

I wrote an article called "C++ Internals :: Memory Layout" which will clarify this for you.

Short excerpt from the article:

.text segment

It's a Read-Only, fixed-size segment.

The text segment, a.k.a. code segment, contains executable instructions provided by the compiler and assembler.

.data segment

It's a Read-Write, fixed-size segment.

The data segment, a.k.a. initialized data segment, contains initialized:

  • global variables (including global static variables)
  • static local variables.

The segment's size depends on the size of the values in the source code, the values can be altered at run-time.

.rdata/.rodata segment

It's a Read-Only segment

The segments contains static unnamed data (like string constants)

.bss segment

It's a Read-Write and adjacent to the .data segment.

BSS segment, a.k.a. uninitialized data segment, contains statically-allocated (global and static) variables represented solely by zero-valued bits on program start. BSS stands for Block Started by Symbol, a pseudo-operation that existed in a very old assembler developed for the IBM.

heap & stack

You are quite right about those. Anyway, if you want to check some examples and get a closer look, please, refer to mentioned article.

like image 50
Sergei Danielian Avatar answered Nov 02 '22 15:11

Sergei Danielian


There are typically at least two data sections. One with initialized globals, another without (BSS). A stack section isn't typically emitted in the binary.

Of course, these kind of very implementation specific questions are kinda useless if you don't specify the implementation.

like image 20
Hans Passant Avatar answered Nov 02 '22 15:11

Hans Passant