简体   繁体   中英

How much memory calloc and malloc can allocate?

How much memory calloc and malloc can allocate?

As malloc and calloc can allocate memory dynamically

Example

void *malloc (size_in_bytes);

And calloc can allocate memory depending on the number of blocks

Example

void *calloc (number_of_blocks, size_of_each_block_in_bytes);

You can allocate as much bytes as type size_t has different values. So in 32-bit application it is 4GB in 64-bit 16 I don't even know how to call that size

All in all you can allocate all memory of machine.

Aside from being limited by the amount RAM in the PC, it is system dependent, but on Windows, it's _HEAP_MAXREQ according to the MSDN article on malloc . Note though malloc and calloc are not guaranteed to allocate anything. It all depends on how much memory is available on the executing PC.

malloc sets errno to ENOMEM if a memory allocation fails or if the amount of memory requested exceeds _HEAP_MAXREQ.

_HEAP_MAXREQ is defined as follows in malloc.h (at least in the Visual Studio 2010 includes).

#ifdef  _WIN64
#define _HEAP_MAXREQ    0xFFFFFFFFFFFFFFE0
#else
#define _HEAP_MAXREQ    0xFFFFFFE0
#endif

You shouldn't really worry about this though. When using malloc you should decide how much memory you really need, and call it with that as the request. If the system cannot provide it, malloc will return NULL . After you make your call to malloc you should always check to see that it's not NULL . Here is the C example from the MSDN article on proper usage. Also note that once you've finished with the memory, you need to call free .

#include <stdlib.h>   // For _MAX_PATH definition
#include <stdio.h>
#include <malloc.h>

int main( void )
{
   char *string;

   // Allocate space for a path name
   string = malloc( _MAX_PATH );

   // In a C++ file, explicitly cast malloc's return.  For example, 
   // string = (char *)malloc( _MAX_PATH );

   if( string == NULL )
      printf( "Insufficient memory available\n" );
   else
   {
      printf( "Memory space allocated for path name\n" );
      free( string );
      printf( "Memory freed\n" );
   }
}    

As far as the language definition is concerned, the only upper limit per call is what size_t will support (eg, if the max size_t value is 2 32 -1, then that's the largest number of bytes malloc can allocate for a single block).

Whether you have the resources available for such a call to succeed depends on the implementation and the underyling system.

On linux, indefinite amounts, ie much more than you have either physical or virtual memory.

As long, as you don't actually use it, you're fine, it's just when you actually use more memory than available that the out-of-memory killer starts running amok, and shoots down your process.

This is the reason why many people don't bother checking the result of malloc() anymore, because it's not null even when the returned buffer can never be backed...

1) It depends on the resource limits of the user. 2) It also depends on the availability of address space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM