Strange memory allocation bug

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
torii
Posts: 15
Joined: Sun Feb 02, 2025 5:59 pm
GitHub: https://github.com/Toriiiiiiiiii
Contact:

Strange memory allocation bug

Post by torii »

Hello all!
I am experiencing a strange bug when using the liballoc implementation of malloc.
When first accessing allocated memory, the next write to memory seems to be ignored completely, leaving the memory uninitialised. I have added some example screenshots below:

Code: Select all

    int *n = malloc(sizeof(int));

    *n = 10;
    char buf[10] = {0};
    utos(*n, 10, buf);
    fputs(1, buf);
    fputs(1, "\n");

    *n = 12;
    utos(*n, 10, buf);
    fputs(1, buf);
malloc bug 2.png
malloc bug 2.png (452 Bytes) Viewed 158 times
By moving the declaration of `buf` before assigning to *n:

Code: Select all

    int *n = malloc(sizeof(int));
    char buf[10] = {0};

    *n = 10;
    utos(*n, 10, buf);
    fputs(1, buf);
    fputs(1, "\n");

    *n = 12;
    utos(*n, 10, buf);
    fputs(1, buf);
malloc bug 3.png
malloc bug 3.png (351 Bytes) Viewed 158 times
Edit: forgot to upload the github link https://github.com/Toriiiiiiiiii/Solkern
Writing bad code since 2019
Image Image
Octocontrabass
Member
Member
Posts: 5876
Joined: Mon Mar 25, 2013 7:01 pm

Re: Strange memory allocation bug

Post by Octocontrabass »

Have you tried disassembling this function? I suspect your code isn't doing what you think it's doing. I'm especially interested in the parameters being passed to utos().
Post Reply