Archived

This topic is now archived and is closed to further replies.

Can't allocate memory for DB or BMP

This topic is 6645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Hmm, in windows I would recommend using the new operator, since they use malloc, and check the return value....

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites