This assignment investigates how C programs organize memory by exploring the four main memory segments:
- Stack: Automatic storage for local variables and function calls
- Heap: Dynamic memory allocated during program execution
- Data: Global and static variables with fixed addresses
- Text: Program code (not directly explored in this assignment)
memory_detective.c- Main assignment file with TODO sections to completeREADME.md- This file
You need a C compiler installed on your machine. See Resource 1 in the assignment for installation instructions.
Verify your installation:
gcc --versionchmod +x compile_and_run.sh # Make script executable (only needed once)
./compile_and_run.shgcc -Wall -Wextra memory_detective.c -o memory_detective
./memory_detectivegcc -Wall -Wextra memory_detective.c -o memory_detective.exe
memory_detective.exeComplete all TODO sections in memory_detective.c:
- Part 1: Data Types and Sizes - Declare variables of different types and print their addresses and sizes
- Part 2: Stack Allocation - Investigate local variables in functions
- Part 3: Data Segment - Examine global and static variables
- Part 4: Heap Allocation - Allocate memory dynamically using malloc()
%dfor int%ffor float%lffor double%cfor char%pfor pointers/addresses (use with(void*)&variable)%zufor size_t (returned by sizeof())
Always cast addresses to (void*) when using %p:
printf("address = %p\n", (void*)&my_variable);int *ptr = (int*)malloc(sizeof(int)); // Allocate
if (ptr != NULL) {
*ptr = 777; // Use
free(ptr); // Free when done
}- Code: Push your completed
memory_detective.cto your GitHub repository - Reflection: Include
reflection.txtorreflection.md(400-500 words) addressing:- Python vs C Memory Model (200-250 words)
- Memory Observations (200-250 words)