Page 1 of 1

Default stack size / memory map for CP/M

Posted: Sun Sep 22, 2019 10:47 pm
by alank2
Does the stack start at one side and the malloc from the other and they grow towards each other? Or is there is a fixed stack size? Is there stack overflow checking?

Posted: Mon Sep 23, 2019 9:18 am
by dom
If you're using newlib, then Alvin explained heap sizing in this post: ... 713#p14713

For classic, AMALLOC is the easiest way to do things, it's detailed in this thread:

For CP/M, any heap will start from the end of BSS memory and extend up to the values configured above. If it's dynamically sized using the above mechanisms, the actual size will change depending on the TPA of the machine that it's running on.

There's no bounds checking, it would be possible to add on entry to a function, but even with a fudge factor we might still overlap depending on the complexity of the function.

Posted: Mon Sep 23, 2019 12:05 pm
by alank2
I am using newlib (zcc +cpm -clib=sdcc_iy -create-app %1.c

What I want to do is keep the dynamic area from running into a specific stack size. I'd rather it return NULL on malloc when it is out of memory.

I tried this as a test to see how large a block I can malloc, but changing the stack size does not diminish the block size.

#include <stdio.h>
#include <alloc.h>

#define CRT_STACK_SIZE 16384

int main()
unsigned int i1;
char *p;

for (i1=65280;i1!=0;i1-=256)

if (p!=NULL)
printf ("%04x",p);

return 0;

Posted: Mon Sep 23, 2019 2:11 pm
by dom
Try replacing the #define with following:

#pragma define CRT_STACK_SIZE = 16384

With that option I get a value of 44800 using zxcc, without it it's 60672

Posted: Mon Sep 23, 2019 2:13 pm
by alank2
Excellent - yes - that does it! Thank you dom!