Thread overview
malloc return NULL when coreleft()<10000
Apr 07, 2010
veb
Apr 08, 2010
veb
Apr 08, 2010
Walter Bright
Apr 09, 2010
veb
Apr 09, 2010
Walter Bright
Apr 17, 2010
veb
Apr 17, 2010
Walter Bright
April 07, 2010
Hello,

I have a problem with malloc and realloc.

When coreleft is under 9500 bytes, malloc return NULL.

my parameters to compile are: -2 -Pz -Jm -R -Ab -msd -o+space -Nc -Ns main.c

Thanks to help me to resolve the problem.
April 08, 2010
Hello,

I made some new test:
coreleft, stackavail and _chkstack return a value >8000
and _heapchk return -2  (_HEAPOK)

BUT

_memavl() doesn't return anything, and the programm doesn't respond (and I need to
reset everything)

I hope this test will help to resolve my problem
April 08, 2010
veb wrote:
> I hope this test will help to resolve my problem

This could very well be a pointer bug in your program, which is difficult to resolve in the -ms memory model. What I do is thoroughly debug a program first in 32 bit mode, then try porting it to 16 bits.
April 09, 2010
Walter Bright wrote:
>This could very well be a pointer bug in your program, which is >difficult to resolve in the -ms memory model. What I do is thoroughly debug a >program first in 32 bit mode, then try porting it to 16 bits.

I made another test:
I affected a different value to unsigned __cdecl _stack
When it is too small, Some operations don't work, but I can allocate more memory
than if _stack is bigger.
Which size is best for _stack ? (or how allocate in _stack ?)
How to calculate exactly how many memory I can still allocate when the programm
runs? (since memavl() doesn't work)

Thanks
April 09, 2010
veb wrote:
> Walter Bright wrote:
>> This could very well be a pointer bug in your program, which is >difficult to
>> resolve in the -ms memory model. What I do is thoroughly debug a >program first
>> in 32 bit mode, then try porting it to 16 bits.
> 
> I made another test:
> I affected a different value to unsigned __cdecl _stack
> When it is too small, Some operations don't work, but I can allocate more memory
> than if _stack is bigger.
> Which size is best for _stack ? (or how allocate in _stack ?)
> How to calculate exactly how many memory I can still allocate when the programm
> runs? (since memavl() doesn't work)

The default is the best size for _stack.

At the level you're working at, I suggest studying the source code for malloc(), etc. It isn't too complicated, and when you're trying to fit things in 64K it helps a lot to understand the details all the way down.
April 17, 2010
Walter Bright wrote:
> The default is the best size for _stack.
> At the level you're working at, I suggest studying the source code for malloc(),
> etc. It isn't too complicated, and when you're trying to fit things in 64K it
> helps a lot to understand the details all the way down.

Ok,

I look for malloc's source in the sources of open watcom, and I don't really realize why _stack needs to be big.

I would like to reduce the size of _stack,
 and I would instead alloc many time a small buffer with malloc to alloc a big
buffer (will it work?)

For example if I write unsigned __cdecl _stack=2048;
and would like to allocate a 1024 bytes buffer, should I write:
"buffer=realloc(buffer,500); buffer = realloc(buffer,1024);"
instead of"buffer=realloc (buffer,1024);" ?

Thanks for your help
April 17, 2010
veb wrote:
> Walter Bright wrote:
>> The default is the best size for _stack.
>> At the level you're working at, I suggest studying the source code for malloc(),
>> etc. It isn't too complicated, and when you're trying to fit things in 64K it
>> helps a lot to understand the details all the way down.
> 
> Ok,
> 
> I look for malloc's source in the sources of open watcom, and I don't really
> realize why _stack needs to be big.

Looking in Watcom's source code won't help much, should look in Digital Mars' sources.


> I would like to reduce the size of _stack,
>  and I would instead alloc many time a small buffer with malloc to alloc a big
> buffer (will it work?)
> 
> For example if I write unsigned __cdecl _stack=2048;
> and would like to allocate a 1024 bytes buffer, should I write:
> "buffer=realloc(buffer,500); buffer = realloc(buffer,1024);"
> instead of"buffer=realloc (buffer,1024);" ?
> 
> Thanks for your help

I'd just use 1024.