Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you allocate a very large single chunk of memory ( > 4GB ) in c or c++?

Tags:

c++

c

memory

malloc

With very large amounts of ram these days I was wondering, it is possible to allocate a single chunk of memory that is larger than 4GB? Or would I need to allocate a bunch of smaller chunks and handle switching between them?

Why??? I'm working on processing some openstreetmap xml data and these files are huge. I'm currently streaming them in since I can't load them all in one chunk but I just got curious about the upper limits on malloc or new.

like image 828
KPexEA Avatar asked Oct 08 '08 01:10

KPexEA


People also ask

Do you allocate memory in C?

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.

How does C memory allocation work?

In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.

What happens when heap memory is full in C?

Your heap will get full. When this happens, malloc() won't be able to allocate memory anymore and it's going to return NULL pointers indefinitely.


1 Answers

Short answer: Not likely

In order for this to work, you absolutely would have to use a 64-bit processor. Secondly, it would depend on the Operating System support for allocating more than 4G of RAM to a single process.

In theory, it would be possible, but you would have to read the documentation for the memory allocator. You would also be more susceptible to memory fragmentation issues.

There is good information on Windows memory management.

like image 170
Benoit Avatar answered Oct 08 '22 00:10

Benoit