Page 1 of 1

[Z88dk-users] Understanding arrays and the stack

Posted: Fri Nov 01, 2019 5:22 am
by WauloK

I figured rather than filling up the forum it might be better to ask here.
I had this reply from Dom:
int i,j,fontdatastart,textchar; int fontoffset,arrayoffset; char
Which defines an array with 0 elements - i.e. it consumes no space on the
stack. So you've got on the stack:
sp+0 = fontarray
sp+0 = arrayoffset
sp+2 = fontoffset
And then you've got:
fontarray[0] = fontspec.width;
fontarray[1] = fontspec.height;

Which will overwrite array offset with the value 7 * 256 + 5 = 1797
I want to understand when things get added to the stack and if it's not
possible to declare an array but not set a size since it won't grow by
pushing other variables down, but will overwrite other variables.
Should I use malloc to set the array size since I don't know at the start
how large the array size will need to be until a separate file with array
data is read in?
Does declaring fontoffset and arrayoffset not reserve the space of an int?
Are arrays with no size declared not usable with z80 C programming?
For other code reference, this is from:

I tried using malloc then got an error saying missing _heap so I read I
need to declare "long heap" which I did, then I read I need "void
mallinit(void)" and now my code doesn't run at all.

-- - Voice acting and geeky stuff
<> - Fitbit Ionic clock faces and games
<> - Australian technology podcast - My retro 80s radio show - My retro VZ200 website