malloc error in C

>c:\s.t>cl /MD /GS- a.c
>Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40310.41 for AMD64
>Copyright (C) Microsoft Corporation. All rights reserved.
>
> x.c
> Microsoft (R) Incremental Linker Version 8.00.40310.39
> Copyright (C) Microsoft Corporation. All rights reserved.
>
> /out:x.exe
> a.obj
>
> T:\s.t>x
> 3000000000 0
>
> c:\second.try>type a.c
> // cl /MD /GS-
> #include<malloc.h>
> main(){__int64 n=3000000000;
> printf("%I64d %i\n",n,malloc(n));}

why does malloc(n) return 0. What is wrong with this code


Answer this question

malloc error in C

  • Leo Cohn

    Last gasp before we, yet again, lose to the Linux crowd: link the program with the /LARGEADDRESSAWARE option. They might have changed the default...



  • Joysen

    Well, it's actually more than 3 GB, actually 24GB (thouse are __int64, and each of them is an 8 byte integer).

    No, it really is 3 GB. Did you post this before having your morning coffee :) Take a look at the code again.


  • Poster Ho

    Three gigabytes. You don't have that much virtual memory. Drop a few zeros.



  • dpatfield66

    Thanks for the response and I certainly agree in general, but this is a 64 bit process on 64 bit hardware,  running a 64 bit OS, 64 bit compiler.... there is no reason why I can see why I can't malloc beyond 2gb.

    This code works fine on linux; exact same code. Anybody have any ideas


  • GuntherM1466

    sorry I should relayed those details.

    \

    Windows x64 2003 R2, 16 gb or RAM.


  • Scott Weiss

  • Anbalagan C

    64 bit with 16 gb or ram.

    Why wouldn't I have that much VM

    j


  • Gareth Ch

    Sorry to resurrect this old thread. I have a situation where my malloc() is failing (returning NULL) for various sizes between 600MB and 1GB. 594MB works, but 648MB doesn't. The machine is XP Pro Dual Xeon 2.4G 2GB, 3GB pagefile. It also fails on newer XP Pro Pentium D 2GB machines. Compiler is VC++ .Net (Microsoft Developement Environment 2002 Version 7.0.9466) with release and debug builds. The function in question is ANSI C in a .cpp file.

    TIA,

    - Mike


  • Ryan mystique

    Worked just fine on this side on Win2k3 SP1 x64. Malloc is not blocking allocating this big chuck of memory. It is most likely OS specific issue which prevents malloc from allocating memory and this is why it fails.

    Nikola



  • Daniel Svensson

    Well, I actually never drank coffee in my entire life, but at a second look I see your point. He allocates n bytes not n __int64, so yes, the allocated size is "about" 3GB. Because, it's actually 3000000000/1024*1024*1024 = 2.79GB. :D

  • malloc error in C