Using new and removing in C ++

I need help understanding when I will use the following parameters

char *a = new char();

      

and

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

      

and how should the corresponding memory deallocation calls be made?

+3


source to share


5 answers


The first die allocates one char. You remove it with:

delete a;

      

The second selects an array of characters. The length you chose is a little odd. You free it up with:



delete[] a;

      

Now ... I hope you don't think you can put a string number in the second a

(something like "123456"

, because you will need many more bytes). Let's say at least 12 if int

- 32 bits. A funny formula is needed to calculate the minimum length. This is an approximation of log10 fooobar.com/questions/1892152 / ...

To be clear, on my machine sizeof(int)

== 4, but in int

I can put -2147483648

, which is 10 digits plus -

, so 11 (plus the null terminator)

+4


source


Anytime you use new T

, you must call delete

on the resulting pointer afterwards.

Anytime you use new T[n]

, you must call delete[]

on the resulting pointer later.

And that's really all there is.



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

If you need a string, don't allocate a char array. Just declare std::string

(no use new

). If you want an array that is sized at runtime, don't allocate the array. Declare std::vector

(no use new

).

+10


source


None of the expressions you show make sense.

Also, as a general rule of thumb, never use explicit delete

(leave that for smart pointers and collections) and try to avoid using explicit new

.

For simple strings with elements, char

just use

std::string a;

      

This is an example of outputting new

-ing and delete

-ing to a library class (which is very preferable), in this case std::string

.

Include a title <string>

to get the type declaration std::string

.

+5


source


char *a = new char();

      

This creates a pointer to one initialized one char

.

char *a = new char;

      

Creates a pointer to one uninitialized one char

.

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

      

Creates a dynamically allocated char array of size sizeof(int)+1

, i.e. 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 one char*

and delete[]

for an array char

.

+3


source


One more thing to note, as well as other answers, is not to confuse the two statements: you have to know if you have allocated a pointer or an array to char* c

so that:

  • You are calling the correct delete

    / operator delete[]

    when clearing memory
  • You are not trying to access data outside the scope of the pointer.

For example, if you did this:

// 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!

      

Since there are no pointers to pointers trying to access c[1]

it will do something, but in this context it will not return what you expect, most likely it will return the memory saved after the pointer char* c

or random memory.

0


source







All Articles