Memory allocation overflow in c program

This is a discussion on Memory allocation overflow in c program within the C Programming forums, part of the General Programming Boards category; In a large system I try to allocate about 1GB of memory on a 4GB system and it blows up ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    Memory allocation overflow in c program

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    BYTE *bb=(BYTE*) calloc(0.9, 0x40000000);
    0.9 bytes? That will convert to zero integer value. 40000000 records of 0 length may turn into a zero length, which will make calloc return NULL.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Well you also need to reboot your OS with the /3GB flag

    Also bear in mind that your program code and loaded DLLs occupy some part of the user address space, as does the stack required by your program. These will also limit the amount of contiguous RAM you could allocate in a single block.

    And what is "0.9" doing in your calloc call anyway?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4
    for matsp:

    OK, that's true, so I then just allocated a specific number of bytes 400*1024*1024, for example
    But I couldn's allocate more than that!
    What is the use of having 3GB or so available, if I can't make use of them?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4
    for Salem:

    OK, that looks as if it might be the case...contiguous RAM.
    So there is no way of knowing what the max. CONTIGUOOUS RAM is that I can allocate.
    It looks like I have to have a loop which starts by allocating, say 500MB and then gradually decreasing it until the allocation function does not return null.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bassam_fci View Post
    for matsp:

    OK, that's true, so I then just allocated a specific number of bytes 400*1024*1024, for example
    But I couldn's allocate more than that!
    What is the use of having 3GB or so available, if I can't make use of them?
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    	char *p = calloc(1, 1500 * 1024 * 1024);
    	printf("%p\n", (void *)p);
    	if (!p)
    	{
    		printf("Errno = %d\n", errno);
    	}
    	free(p);
    	return 0;
    }
    Works just fine.

    As I don't have /3GB on the OS boot options (and I don't really want to reboot my machine and check if it works), I can't say for sure that it will work above 2GB, but I don't see why not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4
    matsp & Salem; thanks a lot for your effort.
    I'll try something to see if it works or not.
    thanks in advance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Help! -Linked Lists, Memory Allocation, Time Steps, Debugg
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-14-2009, 08:50 PM
  3. Mysterious memory allocation problem
    By TomServo1 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 11:29 AM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21