Thread: malloc not actually allocating right amount of memory?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    malloc not actually allocating right amount of memory?

    I'm taking a course on C (Software/Hardware Interfaces) and we just talked about using malloc in lecture the other day, so i decided to play with it and see how much memory I could allocate before receiving a failure. I made a program which looped a call to malloc that allocated a meg, and counted how many megs it could allocate before failing- but on my 1 gig laptop the final result was that it could allocate 2 gigs before failing o.O

    Then I tried the below script to allocate half a gig, and opened Task Manager to see how much memory was actually being taken up, and it was less than a MB

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
        void *test;
        int megabytes = 512;
    
            if((test = malloc(1024*1024*megabytes))==0)
            {
                printf("ALLOC FAIL"); fflush(stdout);
            }
            else
            {
                printf("ALLOC SUCCESS");
            }
    
        while(1)// hangs program so it doesn't close, and can be viewed in task manager
        {
    
        }
    
        return 0;
    }
    Can anyone explain what's going on? TIA

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    oh and here's the script I originally used (which is a little longer- i used a link list to store all the values, on the off chance there was any sort of auto memory-management system that realized that there where no pointers pointing to the chunks of memory being stored)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node
    {
        void *dat;
        struct node* next;
    } node;
    
    int main()
    {
        node *first;
        node *test;
    
        int count=0;
        int go=1;
    
        first = (node*)malloc(sizeof(node));
        test = first;
    
        while(go)
        {
            if((test->dat = malloc(1024*1024))==0)
            {
                printf("ALLOC FAIL at %d mb", count); fflush(stdout);
                go=0;
            }
            else
            {
                test->next = (node*)malloc(sizeof(node));
                test = test->next;
                count+=1;
            }
        }
        while(1){}
    
        return 0;
    }

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    1. Your original app successfully allocated about 2GB, which is the standard limit for a 32-bit process. This has nothing to do with the actual amount of RAM in your PC, because of something called virtual memory.
    2. Your second app successfully allocated 512MB. Task Manager is well known to be a piece of crap for displaying actual memory usage. It most likely only showed how much your app was actively touching. You want to look at the 'private bytes' setting, which you can if you download the awesome, every-programmer-shoud-have-it Process Explorer from Sysiternals.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Not that I disagree with the recommendation, but you don't exclusively need it to look at any of the memory stats it shows. You can turn them on in Task Manager via View->Select Columns... Private Bytes corresponds to the Commit Size checkbox.
    Last edited by adeyblue; 10-02-2011 at 12:30 PM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    aaah that makes sense, i forgot that it could allocate to the hard disk! thanks for the responses guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocating memory with malloc
    By steve1_rm in forum C++ Programming
    Replies: 32
    Last Post: 12-18-2008, 06:51 PM
  2. Amount of memory space need?
    By jnwk888hwq in forum C Programming
    Replies: 8
    Last Post: 10-25-2003, 02:55 PM
  3. Memory for Char*[x] - How to get the proper amount?
    By Bill 101 in forum C Programming
    Replies: 11
    Last Post: 11-12-2002, 01:18 PM
  4. amount of allocated memory...
    By lobo in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2002, 10:18 PM
  5. How to find the amount of memory
    By Prakash in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 11:12 AM