473,385 Members | 2,015 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,385 software developers and data experts.

delete

hs
hi,

If I have a C struct in C++ like the following:

struct {
int a;
int b;
int c[100];
char d[20];
char *ptr;
}X;
....

I do the following:
..
..
X *ptrX = new X;

ptrX->ptr = new char[200];
..
..
..
delete(ptrX);
Does delete automatically remove memory allocated for ptrX->ptr?
Or does this lead to memory leak and I need to remove heap memory
specifically by doing delete(ptrX->ptr) first ?

Thanks
hs
Jul 22 '05 #1
3 2864
hs wrote:
hi,

If I have a C struct in C++ like the following:

struct {
int a;
int b;
int c[100];
char d[20];
char *ptr;
}X;
...

I do the following:
.
.
X *ptrX = new X;

ptrX->ptr = new char[200];
.
.
.
delete(ptrX);
Does delete automatically remove memory allocated for ptrX->ptr?
Or does this lead to memory leak and I need to remove heap memory
specifically by doing delete(ptrX->ptr) first ?


free what you malloc
delete what you new
delete[] what you new[]

If you "new"ed it, it's your job to delete it :)

Chris
Jul 22 '05 #2
On 9 Aug 2004 10:04:14 -0700, hs <hs******@hotmail.com> wrote:
hi,

If I have a C struct in C++ like the following:

struct {
int a;
int b;
int c[100];
char d[20];
char *ptr;
}X;
...

I do the following:
.
.
X *ptrX = new X;

ptrX->ptr = new char[200];
.
.
.
delete(ptrX);
Does delete automatically remove memory allocated for ptrX->ptr?
Or does this lead to memory leak and I need to remove heap memory
specifically by doing delete(ptrX->ptr) first ?


Neither. You must use

delete[] ptrX->ptr;

because you used new[] you must also use delete[].

john
Jul 22 '05 #3
hs wrote:

hi,

If I have a C struct in C++ like the following:

struct {
int a;
int b;
int c[100];
char d[20];
char *ptr;
}X;

The others answered your question about the pointer. However, what you
have above is not a definition for "struct X" at all. It's an anonyomous
struct of which type you declare one variable, named X.
I think you were going for the old C trick of:

typedef struct {blah blah} X;
That's not necessary in C++, so you should have written it:

struct X {
int a;
int b;
int c[100];
char d[20];
char *ptr;
};


Brian Rodenborn
Jul 22 '05 #4

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

Similar topics

2
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
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...
3
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. I am...
1
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.