Strange Malloc Problem

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Zetr0
Member
Posts: 63
Joined: Mon Aug 22, 2011 12:38 pm

Strange Malloc Problem

Post by Zetr0 »

Greetings fellow z88dk'ers

Going through tidying up code and ( attempting ) to make it more modular I decided to use memory allocation ( stdlib.h ) function "malloc" to assert structures and sizes as opposed to direct declaration of vectors / structures

Imagine my surprise when I was met with errors - it must be a Saturday ;)

Host System:

Code: Select all

System : Linux / Ubuntu x86 64
z88dk version : z88dk Cross-C Compiler - v22110-51889e5300-20231219
Code : malloctest.c

Code: Select all

/*
        Malloc Test
*/

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char *ptr;

    ptr = ( char* ) malloc( 400 * sizeof( char ) );  // allocated 

    if( ptr == NULL )
    {
        printf("Error : ptr == NULL ");
    }
    else
    {
        printf("Success : ptr != NULL ");
    }

    free( ptr );

    return 0;

}

// EOF

Compile Line: ( executable shell script ) "make.sh"

Code: Select all

#!/bin/bash
z88dk.zcc +zx -lp3 -DPLUS3 -lm -create-app malloctest.c > build.txt
Compile Error:

Code: Select all

alloc/malloc-classic/free.asm:25: error: undefined symbol: _heap
  ^---- _heap
alloc/malloc-classic/malloc.asm:27: error: undefined symbol: _heap
  ^---- _heap
I am unsure what is going on as an error but I suspect I have to link these in another way or include another header descriptor for the symbol: _heap

I would appreciate any clues on how I messed up =)

Thanks for reading
User avatar
dom
Well known member
Posts: 2319
Joined: Sun Jul 15, 2007 10:01 pm

Re: Strange Malloc Problem

Post by dom »

I would appreciate any clues on how I messed up =)
Try here: https://github.com/z88dk/z88dk/wiki/Cla ... ry-malloch

Note that some of those initialisation methods have been added in the past week or so. For the version you're on -DAMALLOC is the answer.
Zetr0
Member
Posts: 63
Joined: Mon Aug 22, 2011 12:38 pm

Re: Strange Malloc Problem

Post by Zetr0 »

@dom
A huge thanks sir, I do so appreciate the heads up.

from my perspective, instead of adding <stdlib.h> to my build and then additionally configuring a pragma - I might as well declare a large 6-8k byte array ( since that's what I am working with ) and store all the graphical data in there.

Then using a structure ( like this )

Code: Select all

struct BITMAP
{
    ubyte *imgdata;
    uword byte_size;
    ubyte byte_width;
    ubyte byte_height;
};

ubyte gfx_data[8000];

struct BITMAP cave_wall[5];
I can then assign to cave_wall[ n ].imgdata the location of the gfx_data[ n ];

unless there is a limit of array size? ( I was keeping in mind the 8k page size )
User avatar
dom
Well known member
Posts: 2319
Joined: Sun Jul 15, 2007 10:01 pm

Re: Strange Malloc Problem

Post by dom »

No worries.

There's no limit to the size of the array apart from how much memory you've got available.
Zetr0
Member
Posts: 63
Joined: Mon Aug 22, 2011 12:38 pm

Re: Strange Malloc Problem

Post by Zetr0 »

@dom
dom wrote: Wed Mar 20, 2024 8:36 pm No worries.

There's no limit to the size of the array apart from how much memory you've got available.
Would this mean I could do something quite crazy like declaring -

Code: Select all

ubyte wadfile[64000];
ubyte music[32000];
and then use pointers indexing these arrays without impunity on a 128k Spectrum +3 ( +3e ) ?
User avatar
dom
Well known member
Posts: 2319
Joined: Sun Jul 15, 2007 10:01 pm

Re: Strange Malloc Problem

Post by dom »

If you want that much, then have a read of the __far thread: viewtopic.php?f=16&t=11937

You can allocate up to 65534 bytes in one block with that.
Post Reply