-
December 28th, 2008, 11:59 PM
#1
Using delete on an object without new
Hi,
I need a special class X to make this work without errors
so what am i looking for besides overloading delete?
void main()
{
X x;
delete x;
}
thanks
Last edited by Gorbo; December 29th, 2008 at 12:03 AM.
-
December 29th, 2008, 12:21 AM
#2
Re: Using delete on an object without new
Originally Posted by Gorbo
Hi,
I need a special class X to make this work
Why? What purpose does instantiating an object, and then calling "delete" serve?
Also, main() returns int, not void.
Code:
struct SomeX
{
};
typedef SomeX* X;
int main()
{
X x;
x = 0;
delete x;
}
That is the only way to do what you want. The delete requires that the argument is a pointer. Since it must be a pointer, the X is a typedef for a pointer to some real class. Also, I had to set it to 0, so that issuing the delete call doesn't lead to undefined behaviour.
Regards,
Paul McKenzie
-
December 29th, 2008, 12:22 AM
#3
Re: Using delete on an object without new
Originally Posted by Gorbo
so what am i looking for besides overloading delete?
void main()
{
X x;
delete x;
}
1. main returns an int, always.
2. What you're after is completely unintuitive. If you explain your reasoning, we can suggest proper solutions.
-
December 29th, 2008, 04:30 AM
#4
Re: Using delete on an object without new
One ocassion where in I wanted something like that was when I had 2 code paths and each led to either a pointer to an object on the stack or the free-store and it was a tough to find out when it was pointing to what and correspondingly choose to call delete or not (without adding a state). So, I opted to change the naked pointer usage with a shared_ptr with the deleter set to a noop routine which I used in case the pointer was to the stack object else it had the default deleter as delete in cases it were pointing to the free store. But, I did not need to use delete explicitly as the OP is requesting for. So, not sure what he actually needs that for.
Last edited by exterminator; December 29th, 2008 at 04:48 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 29th, 2008, 10:00 AM
#5
Re: Using delete on an object without new
So main is void instead of int big deal it's not a real program just a test program and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...
anyway i hope i cleared things up and you can point me to the right direction now...
-
December 29th, 2008, 10:02 AM
#6
Re: Using delete on an object without new
You must overload operator new() before overload operator delete().
Thanks for your help.
-
December 29th, 2008, 10:03 AM
#7
Re: Using delete on an object without new
Originally Posted by Gorbo
So main is void instead of int big deal it's not a real program just a test program and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...
anyway i hope i cleared things up and you can point me to the right direction now...
Sentences are a good thing.
That doesn't clear anything up as you didn't provide an explanation as to why it's required. Let's try again. Why do you need to call delete on a stack allocated object? What are you trying to accomplish?
-
December 29th, 2008, 10:06 AM
#8
Re: Using delete on an object without new
Originally Posted by Gorbo
So main is void instead of int big deal it's not a real program just a test program
Test programs are real programs.
and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...
You're responding to professional programmers who have decades of experience here -- don't expect that we won't ask you questions on what you're really trying to do.
Operator delete requires a pointer. That class instance is not a pointer. Therefore there is no way you can get that code to compile unless X is a pointer -- that's why the typedef was needed.
Unless you can change the rules of the C++ language, you're stuck. And because you're stuck is indeed the reason why we want to know what you're really trying to accomplish.
Regards,
Paul McKenzie
-
December 29th, 2008, 10:11 AM
#9
Re: Using delete on an object without new
Just as an FYI....
I know a decent number of instructors (including secondary school teachers, and university procfessors) who will (correctly) FAIL and assignment with main returning a void.
When I give training courses (and I have a course specifically for teachers) I stringly encourage them to adopt this attitude.
A peice of code which has main returning a void, is NOT a valid C++ program. PERIOD.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 29th, 2008, 02:34 PM
#10
Re: Using delete on an object without new
Lets try this again
i do not know the purpose of this program i do not know what i am trying to accomplish
all i know is i was given this code and was told to make it work by creating my own X class that's it!
now if you know how to accomplish this please respond if not don't bother.
-
December 29th, 2008, 02:41 PM
#11
Re: Using delete on an object without new
Originally Posted by Gorbo
i do not know the purpose of this program i do not know what i am trying to accomplish
all i know is i was given this code and was told to make it work by creating my own X class that's it!
Sounds like a useless assignment from a misguided teacher. Anyway, Paul McKenzie's suggestion of a typedef should work, if by "work" you mean "compile". It may or may not run "correctly" since there would be undefined behaviour, but I am not sure if you can actually avoid that without changing the main function itself.
-
December 29th, 2008, 02:43 PM
#12
Re: Using delete on an object without new
Originally Posted by Gorbo
Lets try this again
i do not know the purpose of this program i do not know what i am trying to accomplish
all i know is i was given this code and was told to make it work by creating my own X class that's it!
now if you know how to accomplish this please respond if not don't bother.
What you're asking makes no sense as many of us have coded for decades without the need and it's not defined in the language. You don't know why you need to do it, so the only real logical conclusion is you have a serious design flaw or you don't understand the requirements.
Stack allocated objects get deleted when they go out of scope. You just need to define the scope appropriately so that the objects exist only as long as they're needed.
Objects allocated on the heap using new exist until they're explicitly deleted, which is why you need to delete them.
What you're asking shows that you, or whoever is asking you to do this, lacks an understanding of how C++ and its memory management works. You can keep asking and keep getting frustrated, but it won't change the fact that it just doesn't work that way, and if you understood how it does work, you'd realize what you're asking for makes no sense.
-
December 29th, 2008, 02:54 PM
#13
Re: Using delete on an object without new
Originally Posted by GCDEF
What you're asking makes no sense as many of us have coded for decades without the need and it's not defined in the language. You don't know why you need to do it, so the only real logical conclusion is you have a serious design flaw or you don't understand the requirements.
From what Gorbo has stated, I believe the problem is with the requirements. The assignment question is fundamentally flawed.
Speaking of changing the rules of the language, another "creative" way of solving the assignment question besides using a typedef might be:
Code:
#define delete ;
class X{};
int main()
{
X x;
delete x;
}
I cannot seem to tell from the C++ standard if redefining delete in this way is not allowed, results in undefined behaviour, or is actually okay, but seeing that the assignment is just wrong one might as well submit this as an "answer".
-
December 29th, 2008, 03:46 PM
#14
Re: Using delete on an object without new
Originally Posted by Gorbo
Lets try this again
i do not know the purpose of this program i do not know what i am trying to accomplish
all i know is i was given this code and was told to make it work by creating my own X class that's it!
So you didn't have any idea that what they are asking you to do is designed to waste your time? That's what it seems like to me, because you cannot do what you say you want to do. It's like asking us to divide by zero and give you a numerical answer.
now if you know how to accomplish this please respond if not don't bother.
Again, and this is the last time, there is no way to delete something that isn't a pointer value, so no one can tell you how to accomplish it. What we can do is explain why it cannot be done, which we have painstakingly done, but you refuse to listen.
Regards,
Paul McKenzie
-
December 29th, 2008, 04:00 PM
#15
Re: Using delete on an object without new
Originally Posted by laserlight
I cannot seem to tell from the C++ standard if redefining delete in this way is not allowed, results in undefined behaviour, or is actually okay, but seeing that the assignment is just wrong one might as well submit this as an "answer".
I don't have a copy of the standard on me, but I can tell you that that's UB.
Another solution:
Code:
struct X
{
operator X*() const {return 0;}
};
int main()
{
X x;
delete x;
}
Like in the typedef example demonstrated by Paul, the invocation of scalar/vector operator delete on a null pointer is guaranteed to be a logical no-op.
Last edited by Plasmator; December 29th, 2008 at 04:04 PM.
Reason: Fixed typo.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|