Memory Allocation Test Case

Use the following code to test your memory allocation implementation. The outputs for DEBUG 1 and DEBUG 0 are shown below.

NOTE: Your memory addresses will likely be different. That’s fine! Just make sure the allocated regions of memory are the correct size. For instance, if our struct is 160 bytes and the user requests 40 bytes of memory, then a total of 200 bytes will be allocated. You can subtract the start address from the end address to verify.

Code

int main(void) {
    heap_addr = sbrk(0);
    LOG("Program starting. Heap address: %p\n", heap_addr);

    int *my_ints = mem_alloc(150 * sizeof(int), "muh ints");
    double *my_double = mem_alloc(sizeof(double), "a single double");
    *my_double = 16.8;
    int *more_ints = mem_alloc(25 * sizeof(int), "some more ints!");

    printf("---\n");
    print_mem();
    printf("---\n");

    /* These should all be initialized to zero, right? */
    int total = 0;
    int i;
    for (i = 0; i < 150; ++i) {
        total += my_ints[i];
    }
#if DEBUG == 0
    if (total == 0) {
        printf("All elements are zero.\n");
    } else {
        printf("BAD: all elements are not zero??\n");
    }
#else
    printf("Element 0: %x\nElement 149: %x\n", my_ints[0], my_ints[149]);
#endif

    double *db = mem_lookup("a single double");
    printf("This should be 16.8: %f\n", *db);
    mem_free_name("a single double");
    mem_free_name("muh ints");

    printf("---\n");
    print_mem();
    printf("---\n");

    void *a = mem_alloc(1, "hello world!");
    void *b = mem_alloc(98, "USF");

    int *intz = mem_lookup("USF");
    printf("int (should be 0 or 0xAAAAAAAA in debug mode): %x\n", intz[4]);

    printf("---\n");
    print_mem();
    printf("---\n");

    mem_free(a);
    mem_free(b);
    mem_free(more_ints);

    mem_alloc(100, "Replaces 'more_ints'");
    mem_alloc(601, "New allocation");
    mem_alloc(600, "Uses existing 600-byte block");
    printf("---\n");
    print_mem();
    printf("---\n");

    return 0;
}

Output (Debug ON)

NOTE: your log messages may be different. That’s fine.

allocate.c:206:main(): Program starting. Heap address: 0x106873000
allocate.c:73:mem_alloc(): Allocating memory; size: 600
allocate.c:98:mem_alloc(): Allocation successful; address of 'muh ints': 0x106873000
allocate.c:111:mem_alloc(): Initializing first block at 0x106873000
allocate.c:73:mem_alloc(): Allocating memory; size: 8
allocate.c:98:mem_alloc(): Allocation successful; address of 'a single double': 0x1068732f8
allocate.c:73:mem_alloc(): Allocating memory; size: 100
allocate.c:98:mem_alloc(): Allocation successful; address of 'some more ints!': 0x1068733a0
---
"muh ints": start 0x106873000 end 0x1068732f8 size 600 [IN_USE]
"a single double": start 0x1068732f8 end 0x1068733a0 size 8 [IN_USE]
"some more ints!": start 0x1068733a0 end 0x1068734a4 size 100 [IN_USE]
---
Element 0: aaaaaaaa
Element 149: aaaaaaaa
This should be 16.8: 16.800000
---
"muh ints": start 0x106873000 end 0x1068732f8 size 600 [FREE]
"a single double": start 0x1068732f8 end 0x1068733a0 size 8 [FREE]
"some more ints!": start 0x1068733a0 end 0x1068734a4 size 100 [IN_USE]
---
allocate.c:73:mem_alloc(): Allocating memory; size: 1
allocate.c:77:mem_alloc(): Reusing existing free block: 0x1068732f8
allocate.c:73:mem_alloc(): Allocating memory; size: 98
allocate.c:77:mem_alloc(): Reusing existing free block: 0x106873000
int (should be 0 or 0xAAAAAAAA in debug mode): aaaaaaaa
---
"USF": start 0x106873000 end 0x1068732f8 size 600 [IN_USE]
"hello world!": start 0x1068732f8 end 0x1068733a0 size 8 [IN_USE]
"some more ints!": start 0x1068733a0 end 0x1068734a4 size 100 [IN_USE]
---
allocate.c:163:mem_free(): Freeing block hello world!
allocate.c:163:mem_free(): Freeing block USF
allocate.c:163:mem_free(): Freeing block some more ints!
allocate.c:73:mem_alloc(): Allocating memory; size: 100
allocate.c:77:mem_alloc(): Reusing existing free block: 0x1068733a0
allocate.c:73:mem_alloc(): Allocating memory; size: 601
allocate.c:98:mem_alloc(): Allocation successful; address of 'New allocation': 0x1068734a4
allocate.c:73:mem_alloc(): Allocating memory; size: 600
allocate.c:77:mem_alloc(): Reusing existing free block: 0x106873000
---
"Uses existing 600-byte block": start 0x106873000 end 0x1068732f8 size 600 [IN_USE]
"hello world!": start 0x1068732f8 end 0x1068733a0 size 8 [FREE]
"Replaces 'more_ints'": start 0x1068733a0 end 0x1068734a4 size 100 [IN_USE]
"New allocation": start 0x1068734a4 end 0x10687379d size 601 [IN_USE]
---

Output (Debug OFF)

---
"muh ints": start 0x104be3000 end 0x104be32f8 size 600 [IN_USE]
"a single double": start 0x104be32f8 end 0x104be33a0 size 8 [IN_USE]
"some more ints!": start 0x104be33a0 end 0x104be34a4 size 100 [IN_USE]
---
All elements are zero.
This should be 16.8: 16.800000
---
"muh ints": start 0x104be3000 end 0x104be32f8 size 600 [FREE]
"a single double": start 0x104be32f8 end 0x104be33a0 size 8 [FREE]
"some more ints!": start 0x104be33a0 end 0x104be34a4 size 100 [IN_USE]
---
int (should be 0 or 0xAAAAAAAA in debug mode): 0
---
"USF": start 0x104be3000 end 0x104be32f8 size 600 [IN_USE]
"hello world!": start 0x104be32f8 end 0x104be33a0 size 8 [IN_USE]
"some more ints!": start 0x104be33a0 end 0x104be34a4 size 100 [IN_USE]
---
---
"Uses existing 600-byte block": start 0x104be3000 end 0x104be32f8 size 600 [IN_USE]
"hello world!": start 0x104be32f8 end 0x104be33a0 size 8 [FREE]
"Replaces 'more_ints'": start 0x104be33a0 end 0x104be34a4 size 100 [IN_USE]
"New allocation": start 0x104be34a4 end 0x104be379d size 601 [IN_USE]
---