views:

951

answers:

8
+1  Q: 

What is data area?

In C++ the storage class specifier static allocates memory from the data area. What does "data area" mean?

+5  A: 

I'm not familiar with the term “data area” but memory is often divided into “code section” and “data section”. Code resides in the former, data in the latter. I presume this is what's meant here.

Classically, there's no distinction between the two. However, many modern operating systems can prohibit the execution of code int he data segment (provided the CPU supports this distinction). This sometimes goes by the catch phrase of “NX flag”, as in “no execution” and can effectively prevent some cases of malicious code injection.

/EDIT: Notice that the C++ standard doesn't mention a “data area”.

Konrad Rudolph
Classically, the distinction (at least on x86-based processors) is that the code section is loaded/referenced through the code segment register (CS) and data through the data segment register (DS). Under the flat memory model, there is effectively no difference except NX, as you said.
Zooba
+1  A: 

What Konrad said.

I'd like to add that there are still CPUs out there that can't read data if it's placed in the code section and vice versa. These have been more common decades ago, but they are still alive in the embedded world.

In a nutshell the linker just groups symbols of equal kind together. On the PC you often have even more than simple code and data areas. You will find areas for uninitialized data, read only data and other OS dependent stuff as well.

Nils Pipenbrinck
A: 

With little googling I found more information on these subjects here:

Roskoto
A: 

I think 'data area' is referring to the heap, whereas local variables would usually be located on the stack.

Or it means that the memory allocated for this variable is located in the .data section of the executable, but that would be specific to Windows and the PE format.

Treb
Linux's (Unix's?) ELF has similar facilities: Google says http://www.cs.ucdavis.edu/~haungs/paper/node14.html
Simon Buchan
No. The "data area" is not the heap, and statics are not allocated on the heap.
janm
PE is a variant of COFF, and ELF is a replacement for COFF, so they naturally have many similarities.
wnoise
A: 

There are many places that data might end up. Usually, local variables are allocated on the stack, and you can allocate things on the heap using malloc (or de default version of 'new'). Static data, however, is usually allocated when your program starts, and might end up anywhere -- where exactly is up to the compiler, OS, and executable format.

Jan de Vos
+5  A: 

The names of the areas vary by platform, compiler and linker.

In general, there are:

  • program text: The executable code space.
  • constants: Non-executable constants.
  • stack: The stack.
  • bss: Broadly "statics" in C/C++ terms. "Block Started by Symbol"
  • data: Uninitialised globals
  • heap: Storage allocated at runtime.

In this case the documentation in question is using the name "data area" for what is traditionally called the bss segment.

In C terms, the storage class specifier "static" means memory that exists for the lifetime of the program and is initialised to zero or value of the initialiser. In the example:

static int s_value_one;
static int s_value_two = 123;

The value of s_value_one is guaranteed to be zero and the value of s_value_type is 123 at the point of the first statement in main(). How this comes to be true is an implementation issue.

janm
+2  A: 

In addition to what Konrad said, declaring a variable as static basically means that the memory for it gets allocated with the program as it is loaded, as opposed to on the heap or the stack. Historically, using only static variables in a critical applications meant that the memory footprint of the application would not change at run-time and hence it was less likely to fail due to resource limitations. Don't know if this is still true of modern operating systems.

If you get your compiler to generate a mapfile as part of its output, you can have a look see at what is in all the various sections, including data.

Shane MacLaughlin
A: 

Executable has lots of information in it.

An executable, has many types / classes of data stored inside its physical file.

eg's are

  1. Executable code instructions
  2. Resources
  3. Dependency information (which dlls this binary depends on)
  4. The symbols that are exported from this binary

etc

There needs to be some way to organize

all this information inside the .exe file format such that the OS can easily find all the information and load the executable and get things working. For this purpose a common binary format (created by M$ of-course) called PE (portable Executable) is used in the windows world. All the information i just listed (and many more) are described in detail in different sections of the binary.

.data section

One such section is the .data section. The .data section contains all the initialized global and static data, while the .bss section contains the uninitialized global data.

Why do you require a separate section for globals ?

Well, a global behaves like a global because it is created in an area of memory that exists for the lifetime of a program and is not a temporary data structure like a stack which might be overwritten / reused. (like normal auto variables).

Compiler

Therefore these variables need to be allocated in some permanent address in the heap, which unfortunately cannot be known at the time of compilation. So the compiler places all the global and static variables in this .data / .bss section, and the instructions that refer to these variables refer to these relatively permanent addresses in the .data / .bss.

Linker

When the linker loads the executable in the real world, it decides where these sections have to be placed and creates FIX UPs for these temp addresses such that the instructions that refers to the globals refer to the now real virtual addresses in the programs memory.

Now you know what the .data section / area is and why the globals needs to be allocated some space in that area and how that helps the program in real time. Googling PE format and linker and .data section etc would get you the links.