Loading [Contrib]/a11y/accessibility-menu.js
Back
Close

Discovering memory management in the OS

Zwifi
988 views

What does the memory of a process looks like ?

The code snippet below is a simple program that declares a bunch of elements, before displaying their adresses in memory.

#include <stdio.h>
#include <stdlib.h>
void f(){NULL;}
int global = 0;
int main(){
int* p = malloc(3*sizeof(int));
*p = 1;
*(p+3) = 3;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Some remarks:

  • Contiguous allocation: You can notice that the addresses of p and (p+3) are separated by an actual offset of 3. The malloc function allocates contiguous memory, all the addresses are one after the other.
  • Zones: The values of the addresses may change from one execution to the other. However, you may notice that some addresses are closer to each other. Try to add another function, or another local variable, and see how the addresses are grouped.
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants