0

I'm new to C, and I am confused about what allocator can do in C.

Just as the title asked, Can an allocator use the uninitialized data segment to satisfy heap requests if needed?

YINGYING
  • 47
  • 5
  • 1
    First you have to tell us what an allocator in C is. – DeiDei Feb 22 '17 at 04:07
  • maybe I should say heap allocator ? – YINGYING Feb 22 '17 at 04:18
  • 1
    And what do you consider the "uninitialized data segment" to be? If you are talking about the loader artefact, then basically the answer is "no", but it is not really well-formed because once the program has been loaded, that "segment" is no longer visible as such. – rici Feb 22 '17 at 04:25
  • Reading between the lines, the answer is 'No'. The uninitialized data segment, aka the BSS segment, is for statically allocated variables. It all has a use. Unless your allocator code creates a chunk of space in the BSS and only uses what it created, it is not allowed to use the BSS. – Jonathan Leffler Feb 22 '17 at 06:32

1 Answers1

2

The term "memory allocator" does not have any meaning in the standards that define the C language. They do, however, define a set of "Memory Management Functions" that the C Runtime library must provide, so I assume it is these functions you are asking about.

The standards also don't define where or even how the memory is allocated from, just that functions malloc, calloc, realloc and free must exist and have specified semantics. So, it is up to the implementation to decide where to allocate the memory from.

The term "uninitialized data segment" generally refers to a section of a compiled executable file that specifies that the loader should reserve an amount of memory for the program to use.

In all the implementations I am aware of (or at least those where the term "uninitialized data segment" has meaning), that section is used for uninitialized static variables.

In most implementations, the C Runtime will form the heap by making calls to the OS kernel to obtain block(s) of memory, which it will then allocate to the program.

There is a good description of process memory layout in Linux here.

harmic
  • 28,606
  • 5
  • 67
  • 91