Re: Possible to partially free an object?
{ clc++m banner removed; you should remove it yourself. In addition,
please avoid top-posting; it is discouraged in the group. -mod }
I don't think my question was clear. Assume I have a POD type:
struct foo {
int x;
char y;
float z;
};
And a very similar struct minus one member:
struct bar {
int x;
char y;
};
Then I want to do this:
foo* test = malloc(sizeof(foo));
// ... do some stuff with test
// now I'd like to replace the foo object stored at test with a bar
object
bar* test2 = (bar *)test;
char* area_to_delete = (char *)test + sizeof(bar);
free((char[sizeof(foo) - sizeof(bar)])area_to_delete);
Will that ever explode? I realize I asked for class objects and delete/
new, but given that my classes have no vtbl and no multiple
inheritance, I'm guessing that the compiler will usually pick the same
layout as a POD type.
On Apr 20, 4:14 pm, joshuamaur...@gmail.com wrote:
On Apr 20, 12:49 pm, "k04j...@gmail.com" <k04j...@gmail.com> wrote:
This is going to be a hacky question, but I'm optimizing a low level
smart pointer class so I think it's legitimate. I have a class A and a
subclass B (that's the whole inheritance hierarchy, no multiple
inheritance or other classes involved). Neither have any virtual
functions (not even destructor -- the calling code is able to know
when it has one versus the other). B contains a few additional members
not defined in A. I want to replace an instance of B with an instance
of A. Since B has members in addition to those inherited from A, we
know that sizeof(B) > sizeof(A). Can I free sizeof(B) - sizeof(A)
memory safely somehow? Then I can use placement new on the remaining
memory to make the A instance.
I would just do pointer arithmetic and cast to a suitably sized char
array, but I'm afraid that the compiler might pad such an array with
extra data for alignment concerns, and then try to free that extra
memory too. Does that ever happen? I know it does with structs.
There are parts to this, and you're conflating them. There's the
memory of the object(s), and the lifetime of the objects. For POD
types, these 2 are mostly the same thing. For non-PODs, they're not.
operator new and operator delete allocate and return blocks of memory.
There is no way to only free part of it. So to answer you question
literally, no. However, it may be possible to separately allocate the
two different blocks and free only one of the blocks.
Then there's the question of the objects' lifetimes. Again, the
literal answer effectively boils down to no. However, if you got rid
of the inheritance and made B a stand alone class, and made A and B
both POD, then you might be able to do something. It is allowed by the
standard to access a POD struct of type A through a pointer (or
reference) to POD struct B as long as you access only the common
leading part (if any), so you may want to make B look like
struct B
{ A a; //B contains an A
//put members of B not in A here.
};
Then you could reinterpret_cast a B* to an A* and access that A* as
normal, and cast an A* to a B* and access only the A part as normal.
Finally, judging from your questions, I'd be very careful about doing
any of this if you want portable correct code. Strict aliasing will be
the biggest problem, though with such low level hackery, you're bound
to run into other things.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]