Unless you are forced to use C, you should never use malloc
. Always use new
.
If you need a big chunk of data just do something like:
char *pBuffer = new char[1024];
Be careful though this is not correct:
//This is incorrect - may delete only one element, may corrupt the heap, or worse...
delete pBuffer;
Instead you should do this when deleting an array of data:
//This deletes all items in the array
delete[] pBuffer;
The new
keyword is the C++ way of doing it, and it will ensure that your type will have its constructor called. The new
keyword is also more type-safe whereas malloc
is not type-safe at all.
The only way I could think that would be beneficial to use malloc
would be if you needed to change the size of your buffer of data. The new
keyword does not have an analogous way like realloc
. The realloc
function might be able to extend the size of a chunk of memory for you more efficiently.
It is worth mentioning that you cannot mix new
/free
and malloc
/delete
.
Note: Some answers in this question are invalid.
int* p_scalar = new int(5); // Does not create 5 elements, but initializes to 5
int* p_array = new int[5]; // Creates 5 elements
new
/ delete
- Allocate / release memory
- Memory allocated from 'Free Store'.
- Returns a fully typed pointer.
new
(standard version) never returns a NULL
(will throw on failure).
- Are called with Type-ID (compiler calculates the size).
- Has a version explicitly to handle arrays.
- Reallocating (to get more space) not handled intuitively (because of copy constructor).
- Whether they call
malloc
/ free
is implementation defined.
- Can add a new memory allocator to deal with low memory (
std::set_new_handler
).
operator new
/ operator delete
can be overridden legally.
- Constructor / destructor used to initialize / destroy the object.
malloc
/ free
- Allocate / release memory
- Memory allocated from 'Heap'.
- Returns a
void*
.
- Returns
NULL
on failure.
- Must specify the size required in bytes.
- Allocating array requires manual calculation of space.
- Reallocating larger chunk of memory simple (no copy constructor to worry about).
- They will NOT call
new
/ delete
.
- No way to splice user code into the allocation sequence to help with low memory.
malloc
/ free
can NOT be overridden legally.
Table comparison of the features:
Feature |
new / delete |
malloc / free |
Memory allocated from |
'Free Store' |
'Heap' |
Returns |
Fully typed pointer |
void* |
On failure |
Throws (never returns NULL ) |
Returns NULL |
Required size |
Calculated by compiler |
Must be specified in bytes |
Handling arrays |
Has an explicit version |
Requires manual calculations |
Reallocating |
Not handled intuitively |
Simple (no copy constructor) |
Call of reverse |
Implementation defined |
No |
Low memory cases |
Can add a new memory allocator |
Not handled by user code |
Overridable |
Yes |
No |
Use of constructor / destructor |
Yes |
No |
Technically, memory allocated by new
comes from the 'Free Store' while memory allocated by malloc
comes from the 'Heap'. Whether these two areas are the same is an implementation detail, which is another reason that malloc
and new
cannot be mixed.
Best Answer
top
will tell you the amount of physical memory assigned to your process. Virtual memory is an abstraction on top of physical memory, andmalloc
/free
provide an abstraction on top of that.malloc
reserves space from the heap of your program. The heap is simply an area your program's virtual address space used for temporary storage. As you callmalloc
more, the heap is expanded using thebrk
system call. However, although the virtual size of your heap increases, physical memory isn't actually assigned until you read or write to your newly allocated memory. For instance, since you never write to the memory allocated to theinner
fields of your records, those allocations will not take up any physical RAM.free
just releases parts of the heap allocated bymalloc
. This doesn't necessarily reduce the virtual size of the heap, so the physical memory associated with it may not be released. This is why you're not seeing a reduction in physical memory usage.