overload new , size is strange

P

Prawit Chaivong

Hi, gurus

I've overloaded new operator. and I found that size is strange.
It's more than sizeof(myClass) 4 bytes.
Where does it come from.

I don't know the other compilers do the same thing.
This may be bug of gcc.
 
J

Jacques Labuschagne

Prawit said:
Hi, gurus

I've overloaded new operator. and I found that size is strange.
It's more than sizeof(myClass) 4 bytes.
Where does it come from.

I don't know the other compilers do the same thing.
This may be bug of gcc.

Compilers are allowed to insert as much padding into objects as they
like. The standard usually only talks about the bytes that participate
in the object's value representation.

Jacques.
 
P

Prawit Chaivong

Prawit said:
Hi, gurus

I've overloaded new operator. and I found that size is strange.
It's more than sizeof(myClass) 4 bytes.
Where does it come from.

I don't know the other compilers do the same thing.
This may be bug of gcc.

Sorry, Please ignore previous post. I was confusing.
The real situation is that I overload new and new[].
---------------------------------------------
void* MyClass::eek:perator new(size_t size);
and
void* MyClass::eek:perator new[](size_t size);
----------------------------------------------
In new size = 20; //assume
whilst in new[], size = 204;

In my opinion, it should be 200.
Where does more 4 bytes come from?

Regards,
 
P

Peter Koch Larsen

Prawit Chaivong said:
Prawit said:
Hi, gurus

I've overloaded new operator. and I found that size is strange.
It's more than sizeof(myClass) 4 bytes.
Where does it come from.

I don't know the other compilers do the same thing.
This may be bug of gcc.

Sorry, Please ignore previous post. I was confusing.
The real situation is that I overload new and new[].
---------------------------------------------
void* MyClass::eek:perator new(size_t size);
and
void* MyClass::eek:perator new[](size_t size);
----------------------------------------------
In new size = 20; //assume
whilst in new[], size = 204;

In my opinion, it should be 200.
Where does more 4 bytes come from?

Regards,
Probably gcc uses four bytes to store the number of elements in the array.
Why don't you use std::vector?

/Peter
 
R

Ron Natalie

Jacques said:
Compilers are allowed to insert as much padding into objects as they
like. The standard usually only talks about the bytes that participate
in the object's value representation.
They are allowed to insert padding WITHIN objects, but they can not
insert padding between objects nor will they call the allocation
function with more than sizeof (T).

The array form of the allocator can have some constant overhead added
to the n*sizeof(T).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top