Starting memory location in C

I am considering a memory layout for this process. I noticed that the initial memory of each process is not 0. On this website, TEXT starts at 0x08048000. One reason may be to distinguish an address using a NULL pointer. I'm just wondering if there are any other good reasons? Thanks.

+7
c memory
source share
3 answers

The null pointer should not really be 0. It is guaranteed in the C standard when the value 0 is specified in the context of the pointer that the compiler processed as NULL .

But the value 0 that you use in your source code is just syntactic sugar that has nothing to do with the actual physical address, the value of the null pointer points to.

For more details see:

  • Why is NULL / 0 an illegal memory cell for an object?
  • Why is a null pointer used for a null pointer?

An application in your operating system has a unique address space, which it sees as a continuous block of memory (memory is not physically continuous, it is simply an โ€œimpressionโ€ that the operating system provides to each program).

For the most part, each virtual memory space of a process is exposed in a similar and predictable way (this is the memory layout in a Linux process, 32-bit mode):

Memory layout in a Linux process (image from Anatomy program in memory )

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

Why is the magic 0x08048000? Probably because Linux borrowed this address from System V i386 ABI.

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

The value was chosen to place the stack under the .text section, growing down. 0x48000 bytes can be displayed on a single page table already required by the .text section (thus preserving the page table in most cases), while the remaining 0x08000000 will allow more space for hungry applications.

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

See also:

+3
source share

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 on the machine, including the kernel itself. Thus, part of the virtual address space must be reserved for the kernel.

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

This is always the first memory block that appears, and therefore includes the address 0. The user-mode space goes beyond this, and therefore both the stack and the heap are here.

NULL pointer difference

Even if the user mode space started at address 0 , there would be no data allocated for address 0 , since it will be on the stack or heap that themselves do not start at the beginning of the user's area. Therefore, NULL (with a value of 0 ) can also be used and is not the reason for this layout.

However, one advantage associated with NULL , and the first block that is the kernel memory, is any attempt to read / write to NULL, which produces a segmentation error.

+2
source share

The loader loads binary code into segments in memory: text (constants), data, code. There is no need to start from 0, and since C has a problem with errors that revolve around a zero value, for example, in a[i] , which is even dangerous. This allows (on some processors) to catch segmentation errors.

This would be the C runtime, representing a linear address space of 0. This can be imagined where C is the language of the operating system implementation. But it has no purpose; to start the heap from 0. The memory model is one of the segments. A code segment can be protected from modification by some processors.

And in the allocation of segments occurs in blocks of managed memory run-time C.

I would add that physical 0 and above is often used by the operating system itself.

0
source share

All Articles