5

I used the following code to find it out but I always get 1 as the answer. is there something wrong. Thanks

#include <stdio.h>
#include <stdlib.h>

int main(){
    int mult = 0;
    int chk =8;
    do{
        mult+=1;
        int *p = (int*)malloc(1024*1024*1024*mult);
        if(p==0){
            chk =0;

        }else{
            free(p);
        }
    }while(chk !=0);
    mult = mult -1;
    printf("The number of gigs allocated is : %d\n",mult);
    return 0;
}

Just to help, I have a 64 bit system with both windows and linux installed. Thus, is the above logic correct even though I am getting just 1 gb as the answer on a 64 bit system?

user465983
  • 115
  • 5
  • 13
  • It depends on your o/s. It may depend on the limits set by the o/s, which may be adjusted by appropriately privileged users. Have you tried allocating 1023*1024*1024*2? – Jonathan Leffler Feb 02 '11 at 16:16
  • 4
    I think this code risks suffering from integer overflow, if int is a signed 32-bit variable. Try making the constants unsigned long (or unsigned long long if your compiler has it). – unwind Feb 02 '11 at 16:20
  • @unwind That worked for me. I was able to get past 500 GBs before shutting it off. (Mac OS X uses `mmap()` beyond a certain size.) Make your comment an answer so I can up vote it. – chrisaycock Feb 02 '11 at 16:29

3 Answers3

3

If it is a 32-bit OS, then it is not surprising that the largest contiguous block would be 1GB (or somewhere between that and 2GB). On a 64-bit OS, larger blocks would be possible.

If you change your code to allocate smaller individual pieces, you will likely be able to allocate more than 1GB total.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
  • 1
    Sometimes, you can get up to about 3, 3.5 GB on 32-bit system. 4 GB is the hard limit, of course. It is some value smaller than *that*. – Jonathan Leffler Feb 02 '11 at 16:15
  • @Jonathan: I was going to mention numbers in my answer and then thought better of it since I didn't know what platform he was using. Plus there are other issues such as fragmentation to consider. Allocating 1MB blocks in a loop would "overstate" the maximum compared to most real world applications (at least in my experience). – Mark Wilkins Feb 02 '11 at 16:23
  • @Jonathan: If a 32-bit OS allows you to allocate more than 2gb contiguous and yet has a 32-bit `ptrdiff_t`, it is **broken** and **nonconformant** (it will yield the wrong sign for pointer differences, which could be extremely dangerous!). – R.. GitHub STOP HELPING ICE Feb 02 '11 at 21:40
  • @R..: not sure about one block of contiguous memory, but there are options on Windows to allow you to get more then 2 GiB total shared memory. I don't know what the options are - I don't work enough on Windows to care, either. But the products I work on (for the Unix ports) are able to use more than 2 GiB on Windows (and on Unix) on 32-bit systems. More than 2 GiB in aggregate...but less than 4 GiB. – Jonathan Leffler Feb 02 '11 at 23:13
  • 1
    If the memory is obtained via some method outside of the functions specified by the C standard, then I suppose an implementation can document that pointer subtraction on such memory does not necessarily work. But `ptrdiff_t` is supposed to be large enough to store any pointer difference whose result is defined, and subtracting pointers within a single chunk obtained by `malloc` is certainly well-defined. – R.. GitHub STOP HELPING ICE Feb 02 '11 at 23:44
  • We used to use the [/3GB] flag in boot.ini to tell the 32-bit Windows kernel to limit itself to 1GB, and allow applications access to 3GB. That doesn't mean you can get a pointer to one 3GB chunk in 32-bit windows of course! But as memory gets allocated and fragmented, it gets harder and harder to get even a 1GB chunk (which must of course be contiguous). – moodboom Dec 12 '13 at 15:02
1

These questions might help you a bit: How much memory was actually allocated from heap for an object? and How do I find out how much free memory is left in GNU C++ on Linux

Community
  • 1
  • 1
jwir3
  • 6,019
  • 5
  • 47
  • 92
1
int main(void){
    int MB = 0;
    while(malloc(1<<30)){
        ++MB;
    }
    printf("The number of gigs allocated is : %d\n",MB);
    return EXIT_SUCCESS;
}
  • 1
    What is this supposed to show? You can very easily come into a situation where malloc can return without error but allocate more space than you actually have. Take a look at "memory overcommit" – Falmarri Feb 08 '11 at 00:34