Topic: Incomplete types
Author: "Igor A. Goussarov" <igusarov@akella.com>
Date: Thu, 14 Jun 2001 20:33:39 GMT Raw View
Ron Natalie wrote:
> This is the part I do not understand. You can't codify the old behavior
> because you don't know what it was to begin with. How is the spec going
> to read.
[...]
> That's my point. I don't think we need to invent new ways for people
> to invoke undefined behavior. There are plenty others in the standard if
> you close this hole.
I see. Actually, I didn't want undefined behaviour for deleting a
void*, instead I wanted it to be equivalent to deallocation of the
memory. Look, one can construct the object like
Foo* p = new Foo;
and one can do it this way
char* tmp = new char[sizeof(Foo)];
Foo* p = new(tmp) Foo;
So you see that at object constuction there's an ability to make
memory allocation and actual object constuction in two separate steps.
The same holds true for the destruction (type of destruction
corresponds to the type of construction):
delete p;
vs.
p->~Foo();
delete[] reinterpret_cast<char*>(p);
So what I wanted delete void* to be is a merely memory deallocation like
in the last example.
Igor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: celtschk@web.de ("Christopher Eltschka")
Date: Fri, 15 Jun 2001 19:29:25 GMT Raw View
"Igor A. Goussarov" <igusarov@akella.com> writes:
> Ron Natalie wrote:
> > This is the part I do not understand. You can't codify the old behavior
> > because you don't know what it was to begin with. How is the spec going
> > to read.
> [...]
> > That's my point. I don't think we need to invent new ways for people
> > to invoke undefined behavior. There are plenty others in the standard if
> > you close this hole.
>
> I see. Actually, I didn't want undefined behaviour for deleting a
> void*, instead I wanted it to be equivalent to deallocation of the
> memory. Look, one can construct the object like
>
> Foo* p = new Foo;
>
> and one can do it this way
>
> char* tmp = new char[sizeof(Foo)];
> Foo* p = new(tmp) Foo;
>
> So you see that at object constuction there's an ability to make
> memory allocation and actual object constuction in two separate steps.
> The same holds true for the destruction (type of destruction
> corresponds to the type of construction):
>
> delete p;
>
> vs.
>
> p->~Foo();
> delete[] reinterpret_cast<char*>(p);
Easier:
void* tmp = operator new(sizeof Foo);
Foo* p = new(tmp) Foo;
p->~Foo();
operator delete(p);
Why not just say what you want? You don't want an array of chars
(i.e. new char[]), but raw memory (i.e. operator new()). And then you
don't have to use ugly casts to get rid of the memory finally.
>
> So what I wanted delete void* to be is a merely memory deallocation like
> in the last example.
But "merely memory allocation" is spelled "operator delete" in C++.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Igor A. Goussarov" <igusarov@akella.com>
Date: Mon, 11 Jun 2001 20:51:32 GMT Raw View
Ron Natalie wrote:
>
> "Igor A. Goussarov" wrote:
>
> > Deleting of void* should be allowed regardless to the fact that void
> > is never complete since it is known to have no destructor.
>
> You can't new void's, it's not legal to destroy them at all.
Sorry, I didn't make myself clear enough. Deleting something is a
two-step process. First, an object is destroyed. Second, the memory
allocated for that object is freed. Destroying a builtin type is a
no-op. Thus deleting an int, char, double, pointer or any other 'simple'
type is effectively the same as freeing up the memory. That's why I said
that deleting something via void* can be tolerated, but an explicit cast
to void* should be required as a sign that a programmer knows what he is
doing.
Igor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@spamcop.net>
Date: Mon, 11 Jun 2001 21:03:54 GMT Raw View
"Igor A. Goussarov" wrote:
>
> Sorry, I didn't make myself clear enough. Deleting something is a
> two-step process. First, an object is destroyed. Second, the memory
> allocated for that object is freed. Destroying a builtin type is a
> no-op. Thus deleting an int, char, double, pointer or any other 'simple'
> type is effectively the same as freeing up the memory. That's why I said
> that deleting something via void* can be tolerated, but an explicit cast
> to void* should be required as a sign that a programmer knows what he is
> doing.
You've lost me. Why does a cast to void* indicate that the programmer
knows what he is doing. If he knew what he was doing, the pointer would
be properly declared. These 'simple types' are always complete anyhow.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Igor A. Goussarov" <igusarov@akella.com>
Date: Tue, 12 Jun 2001 21:06:32 GMT Raw View
Ron Natalie wrote:
>
> "Igor A. Goussarov" wrote:
>
> >
> > Sorry, I didn't make myself clear enough. Deleting something is a
> > two-step process. First, an object is destroyed. Second, the memory
> > allocated for that object is freed. Destroying a builtin type is a
> > no-op. Thus deleting an int, char, double, pointer or any other 'simple'
> > type is effectively the same as freeing up the memory. That's why I said
> > that deleting something via void* can be tolerated, but an explicit cast
> > to void* should be required as a sign that a programmer knows what he is
> > doing.
>
> You've lost me. Why does a cast to void* indicate that the programmer
> knows what he is doing. If he knew what he was doing, the pointer would
> be properly declared. These 'simple types' are always complete anyhow.
Let's start again. The current Standard doesn't forbid a deletion of
an incomplete type. Such action is currently described as 'undefined
behaviour'. This seems to be plain wrong because this is a very likely
error. So (IMO) it should be made a compile-time error.
So let's imagine it is made an error. But since the deletion of an
incomplete type was allowed up to now, there should be a way (kinda
'backward compatibility') of doing such deletion asif it is still
allowed (however I can't imagine a program that rely on this deletion
with undefined behaviour, so maybe this is not that necessary).
And what I was proposing is:
1) Make the deletion of an incomplete type an error.
2) Leave some way to do this obscure deletion 'old style'.
One of the possible ways of achieving 2) is allowing this obscure
deletion via explicit cast to void*. If you think that 2) should not be
allowed at all then well, I can understand you.
Igor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@spamcop.net>
Date: Tue, 12 Jun 2001 22:31:04 GMT Raw View
> Let's start again. The current Standard doesn't forbid a deletion of
> an incomplete type. Such action is currently described as 'undefined
> behaviour'.
The fact that something is "undefined behavior" doesn't give you "permission"
to do it, it's just that the compiler is freed from having to detect that you're
screwing up.
> This seems to be plain wrong because this is a very likely
> error. So (IMO) it should be made a compile-time error.
I can buy this idea, it would seem to be an easy thing to do at compile
time.
> So let's imagine it is made an error. But since the deletion of an
> incomplete type was allowed up to now, there should be a way (kinda
> 'backward compatibility') of doing such deletion asif it is still
> allowed (however I can't imagine a program that rely on this deletion
> with undefined behaviour, so maybe this is not that necessary).
This is the part I do not understand. You can't codify the old behavior
because you don't know what it was to begin with. How is the spec going
to read.
> 2) Leave some way to do this obscure deletion 'old style'.
> One of the possible ways of achieving 2) is allowing this obscure
> deletion via explicit cast to void*. If you think that 2) should not be
> allowed at all then well, I can understand you.
>
That's my point. I don't think we need to invent new ways for people
to invoke undefined behavior. There are plenty others in the standard if
you close this hole.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Igor A. Goussarov" <igusarov@akella.com>
Date: Thu, 7 Jun 2001 17:03:17 GMT Raw View
Hello everybody,
Given this code
class SomeClass;
void Foo(SomeClass* A)
{
delete A;
};
can you explain why is deleting an object of an incomplete type
considered an undefined behaviour rather then a compile-time error?
Deleting of void* should be allowed regardless to the fact that void
is never complete since it is known to have no destructor. But deleting
a pointer to forwardly declared class is more then undefined behaviour:
it's incorrect behaviour. So why isn't it an error? (If one really wants
just to free the memory an explicit cast to void* can be done.)
Igor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@spamcop.net>
Date: Thu, 7 Jun 2001 18:00:17 GMT Raw View
"Igor A. Goussarov" wrote:
> can you explain why is deleting an object of an incomplete type
> considered an undefined behaviour rather then a compile-time error?
Got me.
> Deleting of void* should be allowed regardless to the fact that void
> is never complete since it is known to have no destructor.
You can't new void's, it's not legal to destroy them at all.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]