memory starting location in C [duplicate]
Asked Answered
L

3

7

I am looking into to the memory layout of a given process. I notice that the starting memory location of each process is not 0. On this website, TEXT starts at 0x08048000. One reason can be to distinguish the address with the NULL pointer. I am just wondering if there is any another good reasons? Thanks.

Lonni answered 9/10, 2014 at 7:35 Comment(1)
Why is 0x00400000 the default base address for an executable? blogs.msdn.com/b/oldnewthing/archive/2014/10/03/10562176.aspxRegnal
B
4

The null pointer doesn't actually have to be 0. It's guaranteed in the C standard that when a 0 value is given in the context of a pointer it's treated as NULL by the compiler.

But the 0 that you use in your source code is just syntactic sugar that has no relation to the actual physical address the null-pointer value is "pointing" to.

For further details see:

An application on your operating system has its unique address space, which it sees as a continuous block of memory (the memory isn't physically continuous, it's just "the impression" the operating system gives to every program).

For the most part, each process's virtual memory space is laid out in a similar and predictable manner (this is the memory layout in a Linux process, 32-bit mode):

Memory layout in a Linux process (image from Anatomy of a Program in Memory)

Look at the text segment (the default .text base on x86 is 0x08048000, chosen by the default linker script for static binding).

Why the magical 0x08048000? Likely because Linux borrowed that address from the System V i386 ABI.

... and why then did System V use 0x08048000?

The value was chosen to accommodate the stack below the .text section, growing downward. The 0x48000 bytes could be mapped by the same page table already required by the .text section (thus saving a page table in most cases), while the remaining 0x08000000 would allow more room for stack-hungry applications.

Is there anything below 0x08048000? There could be nothing (it's only 128M), but you can pretty much map anything you desire there, using the mmap() system call.

See also:

Burse answered 9/10, 2014 at 8:7 Comment(0)
S
2

I think this sums it up:

Each process has its own set of page tables, but there is a catch. Once virtual addresses are enabled, they apply to all software running in the machine, including the kernel itself. Thus a portion of the virtual address space must be reserved to the kernel.

So while the process gets it's own address space. Without allocating a block to the kernel, it would not be able to address kernel code and data.

This is always the first block of memory it appears and so includes address 0. The user mode space starts beyond this, and so that is where both the stack and heap reside.

Distinguishing from NULL pointer

Even if the user mode space started at address 0, there would not be any data allocated to the address 0 as that will be in the stack or the heap which themselves do not start at the beginning of the user area. Therefore NULL (with the value of 0) could be used still and is not a reason for this layout.

However one benefit related to the NULL and the first block being kernel memory is any attempt to read/write to NULL throws a Segmentation Fault.

Scheld answered 9/10, 2014 at 7:50 Comment(1)
Still, what exactly resides in the 0 .. 0x08048000 range?Unmade
B
0

A loader loads a binary in segments into memory: text (constants), data, code. There is no need to start from 0, and as C is has the problem from bugs accessing around null, like in a[i] that is even dangerous. This allows (on some processors) to intercept segmentation faults.

It would be the C runtime introducing a linear address space from 0. That might be imaginable where C is the operating system's implementation language. But serves no purpose; to have the heap start from 0. The memory model is one of segments. A code segment might be protected against modification by some processors.

And in segments allocation happens in C runtime managed memory blocks.

I might add, that physical 0 and upwards is often used by the operating system itself.

Blunger answered 9/10, 2014 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.