DEV Community

Cover image for Writing My Own Dynamic Memory Management

Writing My Own Dynamic Memory Management

Frank Rosner on November 30, 2020

Introduction So far, whenever we needed some memory, e.g. to store a string, we allocated it like that: char my_string[10]. This stateme...
Collapse
 
cklasacrcez profile image
cklasacrcez • Edited

Hi! I checked your algorithm on a few examples and it does not work properly at all. That is, it does not free memory properly.

// CONSOLE OUTPUT
a:00293D34
b:00292D24
c:00292D10
d:00291D00  -> Here the address should be the same as for b
f:00291D00
Enter fullscreen mode Exit fullscreen mode
     // THE CODE
    int* a = (int*)mem_alloc(sizeof(int)); 
    int* b = (int*)mem_alloc(0x1000);
    int* c = (int*)mem_alloc(sizeof(int));
    std::cout << "a:" << a << std::endl; 
    std::cout << "b:" << b << std::endl;
    std::cout << "c:" << c << std::endl;

    mem_free(b);

    int* d = (int*)mem_alloc(0x1000); // here should be adress of B 
    std::cout << "d:" << d << std::endl;

    int* e = (int*)mem_alloc(sizeof(int));
    mem_free(d);

    int* f = (int*)mem_alloc(0x1000);
    std::cout << "f:" << f << std::endl;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
frosnerd profile image
Frank Rosner

Hmmmm... I tested it a bit but didn't write any thorough unit tests. Do you know what's broken?

Collapse
 
fox3211 profile image
Fox

your merge_current_node_into_previous function not returning anything when it suppose to return void*

Thread Thread
 
frosnerd profile image
Frank Rosner

Oops! Thanks! :) Feel free to submit a PR :)

Thread Thread
 
fox3211 profile image
Fox

how i new to this site

Thread Thread
 
frosnerd profile image
Frank Rosner

You can open a new PR on GitHub. You can find instructions how to contribute to GitHub projects on the internet, such as dataschool.io/how-to-contribute-on...

Let me know if you get stuck :)

Thread Thread
 
fox3211 profile image
Fox

ok so is my answer is the right answer about whats broken

Thread Thread
 
frosnerd profile image
Frank Rosner

I don't know but if you make a PR / try it out we can test it :)

Thread Thread
 
fox3211 profile image
Fox

is there a private chat feature

Thread Thread
 
frosnerd profile image
Frank Rosner

I followed you so we can use dev.to/connect or you can hit me up on Twitter :)

Collapse
 
juanjoseamaker profile image
juanjoseamaker

hi, if I want to boot this in my old computer, i have to use the dd command to write the img in the hard disk, because that isn't working

Collapse
 
frosnerd profile image
Frank Rosner

Great! I always wanted to try it but was too lazy. What did you try exactly and what was the error?

Collapse
 
juanjoseamaker profile image
juanjoseamaker

I will try it again with an usb

Collapse
 
raven_lee profile image
Raven Black

What next paging?

Collapse
 
wiilf24 profile image
C T • Edited

merge_current_node_into_previous (mem.c):

Update void * to void in the function signature to resolve Clang warning about non-void function not returning a value.

Cleaner console output ;)