Thursday | 10 OCT 2024
[ previous ]
[ next ]

Playing with C Memory

Title:
Date: 2021-05-04
Tags:  

This is just a stab at trying to figure out how C works from a memory persepective. I think I sort of understand but playing with it an writing about it hopefully helps.

Let's get started. test code snippet <--Test -->

#include <stdio.h>

void main() {
    char x = 'A';
    char *addr = "Hi!";

    printf("1.Value: %s\n", addr);
    printf("Pointer: %p\n", addr);
    printf("Address: %p\n", &addr);
    printf("Dereferenced: %c\n", *addr);

    void *test = addr + 4;
    printf("Mem: %s\n", test);

    printf("Dereferenced Next: %p\n", (addr+3));
    printf("Dereferenced Next: %c\n", *(addr+4));
    printf("Dereferenced Next: %c\n", *(addr+5));
    printf("Dereferenced Next: %c\n", *(addr+6));
    printf("Dereferenced Next: %c\n", *(addr+7));

    printf("\n");

    printf("Value: %c\n", x);
    printf("Pointer: %p\n", x);
    printf("Address: %p\n", &x);
}
1.Value: Hi!
Pointer: 0x400790
Address: 0x7fffb0321168
Dereferenced: H
Mem: 1.Value: %s

Dereferenced Next: 0x400793
Dereferenced Next: 1
Dereferenced Next: .
Dereferenced Next: V
Dereferenced Next: a

Value: A
Pointer: 0x41
Address: 0x7fffb0321177

The first 2 steps is setting up 2 variables, one an actual variable the other is a pointer to a variable. For x, printing out &x will print out the address of x and that address contains the char x.

The addr pointer however is different. Here we are declaring a variable that will hold a pointer. This pointer in turn points elsewhere in memory that contains our string. This means if get the address of the addr variable we are getting the address of the variable that contains the address of our string. :)

The first print statement prints out our string value. However if we print out our addr variable as a pointer we see the real address. Somewhere in printf or in C is something that says given a pointer, the default is to go to that value.

Next we print out the address of our pointer variable. I wonder why this address is so much bigger than the the address it contains. It looks like that address is double the size of the address to our string. Next I try the dereference option and there that works as expected. Given an address, it manually says to go to the value. This is why we need to use the C option. After deferencing, the address points to a single star.

Next we can do pointer arthimetic. In this case given the addr variables addr, we add 4 to it. This is a bit strange. In printf addr by itself printed out what was pointed to by the the pointer. When doing arithmetic though, we are treating addr as the address. We set the test pointer variable to +4. This is the +4 of the 4 characters. The C book mentioned that pointer math knows what the type is so it increments properly. In this case it would work anyway I think. Here we want to see whats 4 bytes away.

Here it prints out 1.value. This means that Hi! and 1.Value are right next to each other. If I loop through all the available memory, I'm guessing I'll be able to see the entire memory space and all the junk within it. This is a pretty cool idea.

Surprisingly, I find the next string that was inside the printf. This means that Hi! and 1.Value are right next to each other. I'm guessing if I do this in a loop, I'll find the entire memory space and all the