'new' operator primary types

G

George Exarchakos

Hi there,

this is, I guess, very easy question but I cannot find a specific well
documented answer.

When I use:
int *x = new int(8);

it obviously allocates memory for a new int. But do I have to
explicitly deallocate (delete) it as with any object dynamically
created with 'new'?

Many thanks,
-- George
 
I

IR

George said:
When I use:
int *x = new int(8);

it obviously allocates memory for a new int. But do I have to
explicitly deallocate (delete) it as with any object dynamically
created with 'new'?

Yes.



Cheers,
 
V

Victor Bazarov

George said:
this is, I guess, very easy question but I cannot find a specific well
documented answer.

When I use:
int *x = new int(8);

it obviously allocates memory for a new int. But do I have to
explicitly deallocate (delete) it as with any object dynamically
created with 'new'?

Nah... If you don't care about memory leaks, that is.

Objects created by means of 'new' operator [in free store] live until
they are removed from free store by means of 'delete' operator. So,
if you don't delete your 'int', it will keep on living, just like any
other object. There is no difference between a built-in type and any
of UDTs you can think of.

V
 
P

peter koch

Hi there,

this is, I guess, very easy question but I cannot find a specific well
documented answer.

When I use:
int *x = new int(8);

it obviously allocates memory for a new int.
It allocates memory and initialises the int. The initialisation part
is quite important!
But do I have to
explicitly deallocate (delete) it as with any object dynamically
created with 'new'?
You do unless you want a memory leak or use a garbage collector.

/Peter
 
G

George Exarchakos

It allocates memory and initialises the int. The initialisation part
is quite important!> But do I have to

You do unless you want a memory leak or use a garbage collector.

/Peter

Many thanks to all! That seems to be the correct thing to do and I
have a confirmation from all of you so it is 120% sure.

Thanks,
-- George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top