CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2006
    Posts
    141

    Can I malloc a space larger than 3G bytes?

    We know that a process memory space is limited by 4G (1G for kernel).
    I am thinking if I can malloc a space larger than 3G. So I tried the following:

    Code:
    int main()
    {
    	char* x = (char*)malloc(3*1024*1024*1024);
    	return 0;
    }
    AND It works! ....
    Anybody knows why?

    I also tried
    Code:
    char x[0x7fffffff];
    This time compiler complains for its size. So I guess the implementations of malloc and char[] are different, right?

  2. #2
    Join Date
    Apr 2008
    Location
    Shanghai' China
    Posts
    32

    Re: Can I malloc a space larger than 3G bytes?

    Yes, the char [] alloc mem from stack and malloc from heap.
    Because you cannot alloc a mem from stack which is large than stack,
    if you wanna alloc a large mem pls look you compile manual to find out how to adjust stack size.

  3. #3
    Join Date
    May 2008
    Posts
    16

    Re: Can I malloc a space larger than 3G bytes?

    virtual memeroy alloction

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can I malloc a space larger than 3G bytes?

    How much you can malloc depends on a number of factors:

    1) Whether your OS is 32-bit or 64-bit
    2) How much memory is in your graphics card
    3) Whether or not you have virtual memory

    etc.

  5. #5
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Can I malloc a space larger than 3G bytes?

    First thing first, 3GB (or 2GB) user space does NOT mean that you can allocate it as single chunk of bytes. The system may not (most probably) have that large 'single' chunk of bytes.

    You may have to loop for multiple allocations.

    On Windows, new and malloc are slow. You should use HeapAlloc for moderate allocation (around 500B to few KBs). For larger allocations you can use VirtualAlloc, do memory mappping (to actually play with pointers). Even then you CANNOT have more than 2/3 GB alloaction for your process.

    For this, On 32-bit systems you can use PAE - Physical Address Extension. On Windows you can achieve the same by using AWE library. Look at MSDN.

    On 64-bit platform, you must have that much of physical and/or virtual memory.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  6. #6
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: Can I malloc a space larger than 3G bytes?

    Quote Originally Posted by Lindley
    How much you can malloc depends on a number of factors:

    1) Whether your OS is 32-bit or 64-bit
    2) How much memory is in your graphics card
    3) Whether or not you have virtual memory

    etc.
    The memory on your graphics card affects the amount of memory you can allocate?
    John 3:16
    For God so loved the world ...

  7. #7
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Can I malloc a space larger than 3G bytes?

    That´s true for Windows XP 32bit. The uppermost GB (>3) is reserved for I/O mapping, the more RAM you have on your graphics adapter the more space is required to map that RAM into Windows´ address space.
    - Guido

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Can I malloc a space larger than 3G bytes?

    That´s true for Windows XP 32bit. The uppermost GB (>3) is reserved for I/O mapping, the more RAM you have on your graphics adapter the more space is required to map that RAM into Windows´ address space.
    Not that way.
    Default is 2 GB Kernel + 2 GB User space. 1GB Kernel and 3 GB user space is applicable to a process only when following conditions are met:
    1. System is started with /3G parameter in BOOT.INI file.
    2. The image (.EXE) is linked with /LARGEADDRESSAWARE option.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured