Jump to content

Memory allocation overflow in c program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
bassam_fci

bassam_fci

    Newbie

  • Members
  • Pip
  • 3 posts
In a large system I try to allocate about 1GB of memory on a 4GB system

and it blows up (returns a NULL pointer in C), Now I tried to allocate

1GB from a small test program and it also returns null.

Also, I have implemented the /LARGEADDRESSLIMIT switch on the Linker,

to be able to address more than 2GB RAM. But the system does not want

to allocate 1GB, when the available memory is 1.8GB according to the

Task Manager!!

ex: BYTE *bb=(BYTE*) calloc(0.9, 0x40000000);
where BYTE is unsigned char

I can only allocate about 400MB, otherwise I get NULL

How much memory I can allocate from a C program ?

It looks like there is something I don't quite understand about the x86

memory model.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The amount of memory you can allocate will depend on the OS, the hardware, the compiler, and various other parameters. You have given us very little information to help you determine the answer.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
bassam_fci

bassam_fci

    Newbie

  • Members
  • Pip
  • 3 posts
WingedPanther:
I knew that i didn't give you the whole picture, but my question is specific in the area of:
How can I avoid this problem in c program, with 86x memory system and 4GB of RAM .

#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Again, as WingedPanther said, its totally dependent. The language itself has no limitation, bar the limit of an int (32 or 64bits) Now, most OS's are limited to 4GB because they either don't run in long mode or they don't use PAE. Linux provides mmap() which can be used to map and unmap far more memory then is normally available to a single process. Windows provides VirtualAlloc.

Unless you give us more details, we can't give you an exact amount.

#5
bassam_fci

bassam_fci

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks a lot WingedPanther & TkTech, I'll try something and if there is any other problem i'll inform you with more details ... thanks again