Skip to content

Instantly share code, notes, and snippets.

@gwpl
Created March 13, 2024 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwpl/70795cf77a2c0d17c3c3277874aa3a0d to your computer and use it in GitHub Desktop.
Save gwpl/70795cf77a2c0d17c3c3277874aa3a0d to your computer and use it in GitHub Desktop.
Toy example illustrating that on Linux memory is initialized when used.
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
// Allocate 16GB of memory
size_t size = 16UL * 1024 * 1024 * 1024;
char* memory = malloc(size);
// Access only some memory in the middle
size_t middle = size / 2;
memory[middle] = 42;
// Sleep for a while to allow for memory usage inspection
sleep(6);
free(memory);
return 0;
}
// To compile this program, use the following command:
// gcc -o memtest memtest.c
// To run this program and measure its memory usage, use the following command:
// /usr/bin/time -v ./memtest
// Please note that the actual memory usage might be different depending on the system's memory management.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment