Problems with malloc (for Z80)

  • Oleg N. Cher

    Oleg N. Cher - 2013-07-16

    Running this code, malloc returns a piece of memory first two times, but after it, malloc returns only 0, although the default size of heap is 1 Kb - that should be enough.

    include "malloc.h" // I've removed # and use "" instead of other brackets

    struct Person *PersonPtr;
    struct Person {
    CHAR name[20];
    INTEGER age;
    CHAR sex;
    PersonPtr next;
    } Person;

    static PersonPtr john, mike, lisa, bob, iren;

    int main(int argc, char **argv)
    john = malloc(sizeof(Person));
    mike = malloc(sizeof(Person));
    lisa = malloc(sizeof(Person));
    bob = malloc(sizeof(Person));
    iren = malloc(sizeof(Person));
    return 0;

    Here is BAT-file to compile (under Windows):

    @SET Bin=....\Bin
    @SET Lib=....\Lib
    @SET CodeAddr=40000
    @SET DataAddr=50000

    %Bin%\sdcc DynMem.c -mz80 --code-loc %CodeAddr% --data-loc %DataAddr% --reserve-regs-iy -I "." -I %Lib%\include -I %Lib% -L %Lib%/z80 Basic.lib --disable-warning 85
    @IF errorlevel 1 PAUSE

    I've experimented with this code under ZX Spectrum emulator, here is link to have more info:

    Other interest moment is that I cannot compile standard SDCC's file _heap.c (if I've specified the option -mz80, SDCC does not accept keyword __xdata) - thus, we lose the possibility to set the size of the heap. But if I do not specify -mz80, SDCC compiles _heap.c perfectly, but for CPU 8051 - by default. How can I specify heap size for Z80?

  • Maarten Brock

    Maarten Brock - 2013-07-17

    You should use heap.s instead of _heap.c for the z80.
    You should also call _sdcc_heap_init() for the z80.

  • Oleg N. Cher

    Oleg N. Cher - 2013-07-18

    Thank you, Maarten! It works.

    But good to add this things to SDCC's documentation. (I not found it there).


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks