Topic: operator new[] / delete []
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Fri, 12 Jan 2001 18:01:39 GMT Raw View
In article <3A5F16C6.D79F65E@edamail.fishkill.ibm.com>, Florian Krohm
<florian@edamail.fishkill.ibm.com> writes
>My being confused does not stem from the 8 byte overhead, but from the
>fact that the memory chunk as allocated by new() has a size of 88 bytes
>whereas when it is passed to delete(void*, size_t) its size is said to be
>only 80 bytes. I verified that the address returned by "new" is identical
>to the one that is passed to "delete" later on.
>I would have expected the size being passed to "delete" to be the same
>as the size that was passed to "new" for the given chunk [88 in this example].
>So it is the difference in size that confuses me and makes me wonder
>whether it is conformant behavior.
OK, operator new[] needs to know how much memory is required including
the amount that the new[] expression needs for internal housekeeping.
operator delete[] has no such need (at least on many systems) and the
size parameter is not actually used, and so your compiler is generating
an 'arbitrary' value (the sizeof the array itself). Now my guess as to
the extra eight bytes is that the compiler is ensuring maximal alignment
and will locate the address used for the start of the array suitably
(i.e. use the first address in the allocated space that has the desired
alignment after allowing for space to store the number of elements. Now
if it was using 4-byte alignment and 4-byte ints that actually requires
7-bytes overhead, added to the 80 bytes required for the array, that
would naturally be rounded up to 88.
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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 ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kanze <kanze@gabi-soft.de>
Date: Mon, 15 Jan 2001 17:51:15 GMT Raw View
Francis Glassborow <francis.glassborow@ntlworld.com> writes:
|> In article <3A5F16C6.D79F65E@edamail.fishkill.ibm.com>, Florian Krohm
|> <florian@edamail.fishkill.ibm.com> writes
|> >My being confused does not stem from the 8 byte overhead, but from
|> >the fact that the memory chunk as allocated by new() has a size of
|> >88 bytes whereas when it is passed to delete(void*, size_t) its
|> >size is said to be only 80 bytes. I verified that the address
|> >returned by "new" is identical to the one that is passed to
|> >"delete" later on. I would have expected the size being passed to
|> >"delete" to be the same as the size that was passed to "new" for
|> >the given chunk [88 in this example]. So it is the difference in
|> >size that confuses me and makes me wonder whether it is conformant
|> >behavior.
|> OK, operator new[] needs to know how much memory is required
|> including the amount that the new[] expression needs for internal
|> housekeeping. operator delete[] has no such need (at least on many
|> systems) and the size parameter is not actually used, and so your
|> compiler is generating an 'arbitrary' value (the sizeof the array
|> itself).
How can you say that a user defined operator delete[] has no need of the
size parameter? I would presume that if the user defined the version
with the size parameter, rather than the version without, he did so
because he *had* a need for it. And if the standard is written so that
the standard delete operator will call delete with or without the size
parameter, it is precisely to give the user this possibility. Passing a
different size to delete[] than was passed to new[] makes this use
impossible.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
---
[ 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 ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Florian Krohm <florian@edamail.fishkill.ibm.com>
Date: Thu, 11 Jan 2001 21:56:30 GMT Raw View
I have a class X (with no base classes) for which operator new[](size_t) and
operator delete[](void *, size_t) are overloaded.
Suppose, that sizeof(X) == 8. Stepping through
X *foo = new X[10];
delete [] foo;
I see that the overloaded "new" function gets passed a size of 88
whereas the overloaded "delete" function gets passed a size of 80.
I was wondering whether this is conformant behavior or whether
the compiler is broken. I read through section "12.5 Free Store"
and it does not require this behavior explicitly. But I might be
missing something...
Thanks,
Florian
---
[ 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 ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kuyper <kuyper@wizard.net>
Date: Fri, 12 Jan 2001 04:54:56 GMT Raw View
Florian Krohm wrote:
>
> I have a class X (with no base classes) for which operator new[](size_t) and
> operator delete[](void *, size_t) are overloaded.
> Suppose, that sizeof(X) == 8. Stepping through
>
> X *foo = new X[10];
> delete [] foo;
>
> I see that the overloaded "new" function gets passed a size of 88
> whereas the overloaded "delete" function gets passed a size of 80.
> I was wondering whether this is conformant behavior or whether
> the compiler is broken. I read through section "12.5 Free Store"
> and it does not require this behavior explicitly. But I might be
> missing something...
Take a look at section 5.3.4p10: "A _new-expression_ passes the amount
of space requested to the allocation function as the first argument of
type std::size_t. That argument shall be no less than the size of the
object being created; it may be greater than the size of the object
being created only if the object is an array. ..."
The typical reason why the allocated size might be larger than the size
of the array is so the implementation can use the extra space to store
the size of the array. That information is needed when the array gets
delete[]'d. Now, on typical systems that size would certainly fit in 4
bytes, and might fit in 2 bytes. So why does it allocate 8 extra bytes?
One possibility is that the alignment requirement for X is 8, so the
smallest amount that can be added to the size is 8 bytes.
---
[ 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 ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Fri, 12 Jan 2001 09:44:42 GMT Raw View
In article <3A5E273C.F72BB257@edamail.fishkill.ibm.com>, Florian Krohm
<florian@edamail.fishkill.ibm.com> writes
>I have a class X (with no base classes) for which operator new[](size_t) and
>operator delete[](void *, size_t) are overloaded.
I think you mean over-riden, and could you show us the code? I can think
of several reasons why the code might be like that but until I see the
code, I would be speculating.
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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 ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Florian Krohm <florian@edamail.fishkill.ibm.com>
Date: Fri, 12 Jan 2001 10:44:40 CST Raw View
James Kuyper wrote:
> Florian Krohm wrote:
> >
> > I have a class X (with no base classes) for which operator new[](size_t) and
> > operator delete[](void *, size_t) are overloaded.
> > Suppose, that sizeof(X) == 8. Stepping through
> >
> > X *foo = new X[10];
> > delete [] foo;
> >
> > I see that the overloaded "new" function gets passed a size of 88
> > whereas the overloaded "delete" function gets passed a size of 80.
> > I was wondering whether this is conformant behavior or whether
> > the compiler is broken. I read through section "12.5 Free Store"
> > and it does not require this behavior explicitly. But I might be
> > missing something...
>
> Take a look at section 5.3.4p10: "A _new-expression_ passes the amount
> of space requested to the allocation function as the first argument of
> type std::size_t. That argument shall be no less than the size of the
> object being created; it may be greater than the size of the object
> being created only if the object is an array. ..."
>
> The typical reason why the allocated size might be larger than the size
> of the array is so the implementation can use the extra space to store
> the size of the array. That information is needed when the array gets
> delete[]'d. Now, on typical systems that size would certainly fit in 4
> bytes, and might fit in 2 bytes. So why does it allocate 8 extra bytes?
> One possibility is that the alignment requirement for X is 8, so the
> smallest amount that can be added to the size is 8 bytes.
My being confused does not stem from the 8 byte overhead, but from the
fact that the memory chunk as allocated by new() has a size of 88 bytes
whereas when it is passed to delete(void*, size_t) its size is said to be
only 80 bytes. I verified that the address returned by "new" is identical
to the one that is passed to "delete" later on.
I would have expected the size being passed to "delete" to be the same
as the size that was passed to "new" for the given chunk [88 in this example].
So it is the difference in size that confuses me and makes me wonder
whether it is conformant behavior.
I'm including the complete code snippet below [as requested by Francis Glassborow].
When executed it prints:
new: size = 88 address = 57838
del: size = 80 address = 57838
Florian
--------------------------------------------------------------
#include <new.h>
#include <iostream.h>
#include <stdlib.h>
class pos {
public:
void *operator new[](size_t);
void operator delete[](void *, size_t);
pos(void) {};
private:
int x;
int y;
};
//-------------------------------------------------------------
// Member function implementation
//-------------------------------------------------------------
void *
pos::operator new[](size_t size)
{
void *address = malloc(size);
cout << "new: size = " << size << " address = " << address << "\n";
return address;
}
void
pos::operator delete[](void *address, size_t size)
{
cout << "del: size = " << size << " address = " << address << "\n";
free(address);
}
int
main(void)
{
pos *p2 = new pos[10];
delete [] p2;
return 0;
}
---
[ 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 ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Roger Orr <roger_orr@my-deja.com>
Date: Fri, 12 Jan 2001 17:25:05 GMT Raw View
In article <3A5E273C.F72BB257@edamail.fishkill.ibm.com>,
florian@edamail.fishkill.ibm.com wrote:
> I have a class X (with no base classes) for which operator
new[](size_t) and
> operator delete[](void *, size_t) are overloaded.
> Suppose, that sizeof(X) == 8. Stepping through
>
> X *foo = new X[10];
> delete [] foo;
>
> I see that the overloaded "new" function gets passed a size of 88
> whereas the overloaded "delete" function gets passed a size of 80.
> I was wondering whether this is conformant behavior or whether
> the compiler is broken. I read through section "12.5 Free Store"
> and it does not require this behavior explicitly. But I might be
> missing something...
12.5p5: "When a delete-expression is executed, the selected deallocation
function shall be called with the address of the block of storage to be
reclaimed as its first argument and (if the two-parameter style is used)
the size of the block as its second argument."
So it looks like a bug in the compiler.
--
Roger Orr. MVP in C++ for Brainbench at http://www.brainbench.com
Sent via Deja.com
http://www.deja.com/
---
[ 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 ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]