user987316
user987316

Reputation: 942

Use of new and delete in C++

I need help in understanding when shall I use the following options

char *a = new char();

and

char *a = new char[sizeof(int)+1];

and how the respective memory freeing calls should be made?

Upvotes: 4

Views: 2034

Answers (5)

Peter Monks
Peter Monks

Reputation: 4389

Another thing worth pointing out as well as the other answers is do not mix the two statements up - you should know whether you allocated a pointer or an array to char* c so that:

  1. You call the correct delete/delete[] operator when cleaning up memory
  2. You do not try accessing data beyond the pointer's scope.

For example, if you did:

// Create a pointer to a single char and set the value
char* c = new char();
*c = 'a';

// Access the pointer using array syntax
char tmp1 = c[0];    // Works, returns 'a'
char tmp2 = c[1];    // Illegal!

Because there are no safe-guards on pointers trying to access c[1] would do something, but in this context it will not return what you expect, it will most likely return memory stored after the char* c pointer or random memory.

Upvotes: 0

xanatos
xanatos

Reputation: 111840

The fist one allocates a single char. You delete it with:

delete a;

The second one allocates an array of chars. The length you have chosen is a little strange. You deallocate it with:

delete[] a;

Now... I hope you don't think you can put a stringified number in the second a (something like "123456", because you'll need many more bytes. Let's say at least 12 if an int is 32 bits. There is a funny formula to calculate the minimum length necessary. It's an approximation of the log10 https://stackoverflow.com/a/2338397/613130

To be clear, on my machine sizeof(int) == 4, but in an int I can put -2147483648 that is 10 digits plus the -, so 11 (plus the zero terminator)

Upvotes: 5

Luchian Grigore
Luchian Grigore

Reputation: 258568

char *a = new char();

This creates a pointer to a single initialized char.

char *a = new char;

Creates a pointer to a single uninitialized char.

char *a = new char[sizeof(int)+1];

Creates a dynamically allocated char array, of size sizeof(int)+1, i.e. an uninitialized c-string of size sizeof(int)+1. Probably 5 or 9, depending on sizeof(int).

char *a = new char[sizeof(int)+1]();

The same, but the string is initialized.

You need to explicitly free the memory with delete for a single char* and delete[] for the char array.

Upvotes: 3

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247919

Any time you use new T, you have to call delete on the resulting pointer afterwards.

Any time you use new T[n], you have to call delete[] on the resulting pointer afterwards.

And that's really all there is to it.

But note that you typically shouldn't use them at all.

If you need a string, don't allocate a char array. Just declare a std::string (without using new). If you need an array whose size is determined at runtime, don't allocate an array. Declare a std::vector (without using new).

Upvotes: 17

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145239

None of the expressions you show make much sense.

Also, as a general rule, never use an explicit delete (leave that to smart pointers and collections), and try to avoid using any explicit new.

For simple strings with char elements, just use

std::string a;

This is an example of leaving the new-ing and delete-ing to a library class (which is strongly preferable), in this case std::string.

Include the <string> header to get a declaration of the std::string type.

Upvotes: 5

Related Questions