🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Can't allocate memory for DB or BMP

Started by
1 comment, last by t3rr0r 24 years, 9 months ago
This is because you are using DOS real mode.
In this case it does not matter how much physical memory you have (64 MB or 128 MB or more), you can only use 1MB, and this 1MB is segmented. You have to declare the pointers pointing to the memory buffer with the "far" keyword:

//Declaring the pointer pointing to the //memory buffer

unsigned char far*bufferptr;

//Allocating 64000 bytes:

bufferptr=(unsigned char far*)malloc(64000*sizeof(unsigned char));

This code allocates 64000 bytes in the far heap, which size is about 400-500 Kbyte, so if you want to allocate big memory, you have to use the "far" keyword. Without it, the program tries to allocate the memory in the data segment, which is 64Kbyte large.

I hope this could help you, but I suggest forgetting the DOS and Real mode,and start to develop in Windows Protected mode. Belive me, in this case you can allocate as much memory as you need, and you can use easily the malloc() function.

Advertisement
I use Borland C++ 3.1/DOS, large memory, 300MHZ, 64MB. Technically, why would malloc return NULL? I'm trying to allocate memory for the double buffer (64000 bytes) and .bmps (usually 800). malloc fails more times than it succeeds, even with small .bmps and no DB. If it does succeed, the program crashes. Why?
Hmm, in windows I would recommend using the new operator, since they use malloc, and check the return value....

------------------
Dance with me......

This topic is closed to new replies.

Advertisement