473,395 Members | 1,647 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,395 software developers and data experts.

Question about delete

Hi:

I have a question from the book---C++ primer, page 677.

char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?

Thanks.

john
Jul 22 '05 #1
10 1438
John wrote in news:c3**************************@posting.google.c om in
comp.lang.c++:
Hi:

I have a question from the book---C++ primer, page 677.

char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

After the above two lines, arena and ptr point to the same memory.
If I "delete arena",
delete [] arena;
that memory will be released, right?
Is ptr an dangling pointer now?


Yes and so is arena.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
John wrote:
Hi:

I have a question from the book---C++ primer, page 677.

char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?


If you say delete area, you have undefined behavior. It needs
to be delete[] arena.

Yes ptr is dangling after that. In addition, you may also lose
any resorces allocated in the Image class since it never gets
destructed.
Imagine:
class Image {
std::string name;
char pixels[100000];
};

Your above code snippet will leak resources that std::string allocates
internally.
Jul 22 '05 #3
Ron Natalie wrote:
John wrote:
Hi:

I have a question from the book---C++ primer, page 677.

char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?

If you say delete area, you have undefined behavior. It needs
to be delete[] arena.

Yes ptr is dangling after that. In addition, you may also lose
any resorces allocated in the Image class since it never gets
destructed.
Imagine:
class Image {
std::string name;
char pixels[100000];


public:
Image(const std::string& n) : name(n) {}
};

Your above code snippet will leak resources that std::string allocates
internally.


....and to fight that you need to explicitly call the Image's d-tor:

ptr->~Image();

before attempting to free the memory.

Just my $0.02...

V
Jul 22 '05 #4


John wrote:
Hi:

I have a question from the book---C++ primer, page 677.

char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?


you only need to delete it once, but also choose what you delete
carefully. if you delete 'arena' no destructor will be called, but if
you delete 'ptr' the destructor will be called if there is one. (your
second statement has the side effect of calling a constructor if there
is one) I don't think delete ever nulls pointers though, so in a sense
both pointers are dangling after the delete. however, if you did two
news on the same memory address and both were objects with constructors,
you could cause yourself problems depending on what those constructors
do.

David

Jul 22 '05 #5

"David Lindauer" <ca*****@bluegrass.net> skrev i en meddelelse
news:41***************@bluegrass.net...


John wrote:
Hi:

I have a question from the book---C++ primer, page 677.

char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?


you only need to delete it once, but also choose what you delete
carefully. if you delete 'arena' no destructor will be called, but if
you delete 'ptr' the destructor will be called if there is one. (your
second statement has the side effect of calling a constructor if there
is one) I don't think delete ever nulls pointers though, so in a sense
both pointers are dangling after the delete. however, if you did two
news on the same memory address and both were objects with constructors,
you could cause yourself problems depending on what those constructors
do.

David

delete ptr will cause undefined behaviour. What is needed is ptr->~ptr() to
call the destructor of Image and delete[] arene to release memory.

/Peter
Jul 22 '05 #6
Ron Natalie <ro*@sensor.com> wrote in message news:<41***********************@news.newshosting.c om>...
John wrote:
Hi:

I have a question from the book---C++ primer, page 677.

char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

After the above two lines, arena and ptr point to the same memory.
If I "delete arena", that memory will be released, right?
Is ptr an dangling pointer now?

If you say delete area, you have undefined behavior. It needs
to be delete[] arena.

Yes ptr is dangling after that. In addition, you may also lose
any resorces allocated in the Image class since it never gets
destructed.
Imagine:
class Image {
std::string name;
char pixels[100000];
};


Sorry, I can not understand. Is "size of Image" = size of name + 10000?
Is the size of memory that arena points to = size of name + 10000?

Your above code snippet will leak resources that std::string allocates
internally.


Why?
Jul 22 '05 #7
David Lindauer wrote:

you only need to delete it once, but also choose what you delete
carefully. if you delete 'arena' no destructor will be called, but if
you delete 'ptr' the destructor will be called if there is one.


However, calling delete on ptr will pass the memory back the wrong
allocation function. Objects don't have memory as to how they were
allocated, the context of the delete expression has to figure it out
(and does it wrong in this case).

As Victor pointed out, if you are going to create an object with
placement new, you'll have to make an explicit call to the destructor
prior to deallocating the storage.
Jul 22 '05 #8
John wrote:

Imagine:
class Image {
std::string name;
char pixels[100000];
};

Sorry, I can not understand. Is "size of Image" = size of name + 10000?
Is the size of memory that arena points to = size of name + 10000?


More or less. There might be some padding in there as well (although in
this case it would be unlikely).
Your above code snippet will leak resources that std::string allocates
internally.

Why?


Because std::string's member functions themselves allocate memory OUTSIDE
the class. Imagine this case instead.

classs SillyString {
char* ptr;
public:
SillyString() { ptr = new char[100]; }
~SillyString() { delete [] ptr; }
// need copy constructor and copy assignment operator too...
};
class Image {
public:
SillyString name;
char pixels[10000];
};

Now if we construct an Image object, SillyString allocates memory.
It can only get deallocated if somehow the destructor for Image is
invoked (which will in turn cause the destructor for SillyString\
to be called).
Jul 22 '05 #9


Ron Natalie wrote:
David Lindauer wrote:

you only need to delete it once, but also choose what you delete
carefully. if you delete 'arena' no destructor will be called, but if
you delete 'ptr' the destructor will be called if there is one.


However, calling delete on ptr will pass the memory back the wrong
allocation function. Objects don't have memory as to how they were
allocated, the context of the delete expression has to figure it out
(and does it wrong in this case).

As Victor pointed out, if you are going to create an object with
placement new, you'll have to make an explicit call to the destructor
prior to deallocating the storage.


thanks for the heads up!
Jul 22 '05 #10
Ron Natalie <ro*@sensor.com> wrote in message news:<41***********************@news.newshosting.c om>...
John wrote:

Imagine:
class Image {
std::string name;
char pixels[100000];
};

Sorry, I can not understand. Is "size of Image" = size of name + 10000?
Is the size of memory that arena points to = size of name + 10000?


More or less. There might be some padding in there as well (although in
this case it would be unlikely).
Your above code snippet will leak resources that std::string allocates
internally.

Why?


Because std::string's member functions themselves allocate memory OUTSIDE
the class. Imagine this case instead.

classs SillyString {
char* ptr;
public:
SillyString() { ptr = new char[100]; }
~SillyString() { delete [] ptr; }
// need copy constructor and copy assignment operator too...
};
class Image {
public:
SillyString name;
char pixels[10000];
};

Now if we construct an Image object, SillyString allocates memory.
It can only get deallocated if somehow the destructor for Image is
invoked (which will in turn cause the destructor for SillyString\
to be called).


Yes. I agree.
But in my original post as below:
char *arena = new char[ sizeof Image ];
Image *ptr = new (arena) Image( "Quasimodo" );

arena and ptr point to the same memory. If I "delete [] arena", that
part of memory is deallocated. Certainly, ptr is a dangling pointer
now. But the memory that ptr points to is deallocated. So if "size of
Image" = size of name + 10000, the memory that the string "name"
occupied is also deallocated. Am I right? So the question is: is the
string "name" a part of image?

John
Jul 22 '05 #11

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

Similar topics

5
by: J | last post by:
Dear Advanced users, Here are parts of a code sample and I need to find out a few things from this. Please tell me your input. I am supposed to get others help as well to make the answers as...
6
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) {...
4
by: WertmanTheMad | last post by:
Ok here goes, another odd SQL Question ....as always.. How do I get ... The value of a paramater passed to say a Trigger OR The SQL Itself Like this , say I have a Trigger set on delete, ...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
0
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on...
5
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
7
by: subramanian100in | last post by:
The following discussion is for learning purpose only. 1) Suppose for a type T, I have T *ptr = new T; To free ptr, suppose I use ( I deliberately omit in delete.) delete ptr;
0
by: topmind | last post by:
siddharthkh...@hotmail.com wrote: Good luck. The behavior and conventions of web versus fat-client (or paper-oriented) reports are so different that making a generic anything that serves both of...
0
by: =?Utf-8?B?SmVhbi1GcmFuY29pcyBCcmV0b24=?= | last post by:
"siddharthkhare@hotmail.com" wrote: The context is important in this kind of design concern : I assume there's a lot of user and that application will evolve to add richer functionality. My...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.