Drake Johnson
Drake Johnson

Reputation: 664

What are the differences between memory management in C and C++

How does one dynamically allocate memory for an array of const char*s? That is, for example, const char** array = /* ??? */. I know malloc is similar to new in C++, but always returns a void*.

More broadly, what are the differences between how memory management is handled in C vs C++? For example, what things do I have to watch out for in C that someone may not think about in C++?

Upvotes: 1

Views: 1797

Answers (3)

Lundin
Lundin

Reputation: 213693

The main difference between malloc and new is that new will call the default constructor for the allocated object, if applicable. This is one reason why mixing these two in C++ is very bad practice.

malloc requires you to check the return value for NULL to check if it fails, whereas new will throw a std::bad_alloc exception out in the wild when it fails.

Another big difference is that C++ has the memory leak operator new[], which allocates an array that will leak when one yet again accidentally uses delete rather than delete[].

C++ also allows you to overload these operators - valid cases of when you need to do this exist, but mostly it is wildly questionable practice.

Manual memory allocation in C++ is mostly frowned upon, for good reasons. You'd rather use std::string in this case and let that class worry about memory allocation. Alterntatively you could use a smart pointer class to do the deallocation for you.

Upvotes: 3

Ademi
Ademi

Reputation: 340

Another Memory management aspect that is present in C++ but not in C, is that C++ have object oriented approach to classes . When an instance gets created the constructor of that instance is called, when that instance goes out of scope the destructor is called, this behavior is automated by the compiler, and is transparent to the programmer.

Therefore, it is good practice to run dynamic memory de allocation in the destructor, to ensure that any dynamic memory allocated by the instance is freed when the instance is no longer in scope.

Constructor and desctructor concept only present in C++ and not in C.

Upvotes: 1

ChrisMM
ChrisMM

Reputation: 10018

To allocate the array, you would do something like this:

const char **array = malloc( x * sizeof( char * ) );

Then allocate each element of the array.

if ( array )
    for ( int i = 0; i < x; ++i )
        array[i] = malloc( y * sizeof( char ) );
else
    printf( "malloc failed :(" );

malloc returns a void* but it's compatible with other pointer types.

In both C and C++ you need to manage the memory yourself, but C does not have things like unique_ptr -- also, no new and delete, use malloc and free. There's also calloc and realloc.

malloc can return NULL in the case when it fails, so you should check for that too. Note that it doesn't throw like new can in C++. More information on malloc can be found here.

As requested, when freeing up the array, you basically just do the same thing in reverse. Noting that free does not take in a size, just the pointer. The free function will ignore NULL pointers, so you don't have to worry about that here.

for ( int i = 0; i < x; ++i )
   free( array[i] );
free( array );

Of course, as in C++, don't double free memory. That is bad. :)

Upvotes: 3

Related Questions