473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

'new' operator primary types

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

Feb 12 '07 #1
4 1531
IR
George Exarchakos wrote:
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,
--
IR
Feb 12 '07 #2
George Exarchakos wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 12 '07 #3
On 12 Feb., 20:54, "George Exarchakos" <gexarcha...@gmail.comwrote:
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

Feb 12 '07 #4
On Feb 12, 8:18 pm, "peter koch" <peter.koch.lar...@gmail.comwrote:
On 12 Feb., 20:54, "George Exarchakos" <gexarcha...@gmail.comwrote: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
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

Feb 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
9
by: Ernesto | last post by:
Hi everybody: I have the following typedefs: typedef unsigned char UCHAR; typedef unsigned char BYTE; I am implementing a class String with the following operators overloaded: String&...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
10
by: deepak | last post by:
Hi, when I execute following program which find's one's compliment of 2, I'm getting -3 where I was expecting -2. Is there anything fundamentally wrong in my understanding? main() {
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.