Last Updated: February 25, 2016
· maandree

C and C++ memory management

If you are C program you probably know about malloc and free.
'mallocis awesome because it does not zero-initialise. If you are new to C and finds this annoying, there is alwayscalloc`.

But C also has a valuable function that is missing in C++, realloc
it extends or shrinks the allocation for a variable (memory address)
and only create a new allocation if it is required, which is often is not.

For C++ programmers, the good news is the malloc, calloc, realloc
and free is still supported. You do not have do it the C++ way, and I
recommend to always do it the C way (for arrays, not objects.)

3 Responses
Add your response


Only problem is that stdlib alloc stuff doesn't work with classes. Which is shitty or you will need to use some ugly-as-fu** hacks.

over 1 year ago ·


Well use the C way for arrays.
But you can allows stop using C++ class and use object orientation in C, it is better anyway (but this is not the right medium for explaining why C is better than C++.)

over 1 year ago ·

@hauleth - it does, you just need custom new operator.

over 1 year ago ·