CS330 Process Memory Addresses

Highlights of this lab:


"Every Linux process has its own dedicated memory address space. The kernel maps these "virtual" addresses in the process address space to actual physical memory addresses as necessary. In other words, the data stored at address 0xDEADBEEF in one process is not necessarily the same as the data stored at the same address in another process. When an attempt is made to access the data, the kernel translates the virtual address into an appropriate physical address in order to retrieve the data from memory... we are not really concerned with physical addresses. Rather, we are investigating exactly how the operating system allocates more virtual addresses to a process." (from: http://www.linux-mag.com/2001-07/compile_01.html)

User Versus Kernel Space

A process may operate in 'user' mode or 'system' mode (kernel mode). The programs that we have been programming actually switch between these two modes. When a process changes mode, it is termed a context switch.

To summarize these two modes:

  1. User Mode
  2. Kernel Mode

When a program is loaded as a process, it is allocated a section of virtual memory which is known as user space.

By contrast, there is another section of memory which is known as the kernel space. This is where the kernel executes and provides its services.

The remainder of these notes focus on user space.

Three Segments of User Space

The user context of a process is made up of the portions of the address space that are accessible to the process while it is running in user mode.

There are a few definitions that come in handy when we are talking about memory:

The portions of address space are: text, data, and stack.  They are described in further detail below:

  1. Text
  2. Data
  3. Stack

Notice that the stack grows towards the uninitialized data and the heap grows towards the stack.

Some compilers and linkers call uninitialized data and heap bss (Block Started by Symbol—not bs) for historical reasons.

Memory references to Text, Data and Stack in a user space program are done with virtual addresses. The kernel translates these virtual addresses to physical memory addresses.

In working with these virtual addresses, you have access to three external variables:

Program 1.4, in Interprocess Communications in UNIX: The Nooks & Crannies, provides an example of displaying these external variables. The program also displays the address of some key identifiers to verify that the identifiers (variables) are put into the correct segments.
// Program 1.4 in Interprocess Communications in UNIX: The Nooks & Crannies.
#include <stdio.h>
#include <stdlib.h>   //needed for exit()
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#define SHOW_ADDRESS(ID, I) printf("The id %s \t is at:%8X\n", ID, &I)

extern int etext, edata, end;

char *cptr = "Hello World\n"; // static by placement
char buffer1[25];

int main(void) 
    void showit(char *); // function prototype
    int i=0; //automatic variable, display segment adr

    printf("Adr etext: %8X\t Adr edata: %8X\t Adr end: %8X\n\n",
            &etext, &edata, &end);

    // display some addresses
    SHOW_ADDRESS("main", main); 
    SHOW_ADDRESS("showit", showit);
    SHOW_ADDRESS("cptr", cptr);
    SHOW_ADDRESS("buffer1", buffer1);
    SHOW_ADDRESS("i", i);
    strcpy(buffer1, "A demonstration\n");   // library function
    write(1, buffer1, strlen(buffer1) + 1); // system call 
    for (; i<1; ++i)
        showit(cptr); /* function call */

    return 0;

void showit(char *p) 
    char *buffer2;
    SHOW_ADDRESS("buffer2", buffer2);
    buffer2 = (char *)malloc(strlen(p)+1);
    if (buffer2 != NULL) 
        strcpy(buffer2, p);    // copy the string
        printf("%s", buffer2); // display the string
        free(buffer2);         // release location
        printf("Allocation error.\n");

A sample run on a Linux machine produced the following output:

Adr etext:  804878B      Adr edata:  8049A1C     Adr end:  8049A3C

The id main      is at adr: 8048540
The id showit    is at adr: 8048654
The id cptr      is at adr: 80498F0
The id buffer1   is at adr: 8049A20
The id i         is at adr:BFFFDF04
A demonstration
The id buffer2   is at adr:BFFFDEE4
Hello World