structure padding not considered by 'new'

  • Thread starter karthikbalaguru
  • Start date
K

karthikbalaguru

Hi,

I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?

struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;

p2 = new Dataunit; // This is allocating 26 bytes.

This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?

Thx in advans,
Karthik Balaguru
 
V

Victor Bazarov

karthikbalaguru said:
I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?

struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;

p2 = new Dataunit; // This is allocating 26 bytes.

This allocates 26 bytes. Strange . :(:(.

Why is it strange? On a system where sizeof(int) == 2 that's what I'd
expect...
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?

Well, the code you posted does not compile. Please post a compilable
example. Padding is implementation-specific. Please at least name the
compiler you're using. How do you determine how many bytes the 'new'
allocates and what did you expect, and why are they different (if they
are different)?

V
 
P

Pete Becker

Hi,

I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?

struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;

p2 = new Dataunit; // This is allocating 26 bytes.

This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?

Structure padding occurs in arrays. new doesn't allocate arrays, so it
can deal in the unpadded size of the type.
 
K

karthikbalaguru

I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?
struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;
p2 = new Dataunit; // This is allocating 26 bytes.
This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?

Structure padding occurs in arrays. new doesn't allocate arrays, so it
can deal in the unpadded size of the type.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)- Hide quoted text -

- Show quoted text -

The output of sizeof(struct Dataunit) will definitely be different as
it takes into account the
padding based on the architecture(Processor).
Does 'new' do not take the structure padding into account while size
calculation for allocating memory ?

Thx in advans,
Karthik Balaguru
 
P

Pete Becker

I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?
struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;
p2 = new Dataunit; // This is allocating 26 bytes.
This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?

Structure padding occurs in arrays. new doesn't allocate arrays, so it
can deal in the unpadded size of the type.

The output of sizeof(struct Dataunit) will definitely be different as
it takes into account the
padding based on the architecture(Processor).
Does 'new' do not take the structure padding into account while size
calculation for allocating memory ?

Sorry, I confused the issue by talking about arrays. There may or may
not be internal padding -- that's up to the compiler. new allocates the
number of bytes the compiler asks for, and in this case, it's
sizeof(Dataunit). Why do you think 26 bytes is strange?
 
A

Andre Kostur

On 2007-09-04 10:43:47 -0400, karthikbalaguru
<[email protected]> said:
I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?
struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;
p2 = new Dataunit; // This is allocating 26 bytes.
This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?

Structure padding occurs in arrays. new doesn't allocate arrays, so it
can deal in the unpadded size of the type.

The output of sizeof(struct Dataunit) will definitely be different as
it takes into account the
padding based on the architecture(Processor).
Does 'new' do not take the structure padding into account while size
calculation for allocating memory ?


Hmm.. How do you know that it only allocated 26 bytes? And what is the
sizeof(Dataunit) on your machine? Do you know that your machine isn't
2-byte aligned? (In which case there should be no padding in that
struct, everthing in it has a size which is an even multiple of 2...)
 
K

karthikbalaguru

On 2007-09-04 10:43:47 -0400, karthikbalaguru
<[email protected]> said:
I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?
struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;
p2 = new Dataunit; // This is allocating 26 bytes.
This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?
Structure padding occurs in arrays. new doesn't allocate arrays, so it
can deal in the unpadded size of the type.
The output of sizeof(struct Dataunit) will definitely be different as
it takes into account the
padding based on the architecture(Processor).
Does 'new' do not take the structure padding into account while size
calculation for allocating memory ?

Hmm.. How do you know that it only allocated 26 bytes? And what is the
sizeof(Dataunit) on your machine? Do you know that your machine isn't
2-byte aligned? (In which case there should be no padding in that
struct, everthing in it has a size which is an even multiple of 2...)- Hide quoted text -

- Show quoted text -

As per the spec with me, my machine is 4-byte aligned.

Thx,
Karthik Balaguru
 
J

Jim Langston

karthikbalaguru said:
On 2007-09-04 10:43:47 -0400, karthikbalaguru
<[email protected]> said:
I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?
struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;
p2 = new Dataunit; // This is allocating 26 bytes.
This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?
Structure padding occurs in arrays. new doesn't allocate arrays, so it
can deal in the unpadded size of the type.
The output of sizeof(struct Dataunit) will definitely be different as
it takes into account the
padding based on the architecture(Processor).
Does 'new' do not take the structure padding into account while size
calculation for allocating memory ?

Hmm.. How do you know that it only allocated 26 bytes? And what is the
sizeof(Dataunit) on your machine? Do you know that your machine isn't
2-byte aligned? (In which case there should be no padding in that
struct, everthing in it has a size which is an even multiple of 2...)-
Hide quoted text -

As per the spec with me, my machine is 4-byte aligned.

You have not answered the questions however.

1. How do you know it is only allocating 26 bytes?
2. What does sizeof( Dataunit) show on your machine?
3. What is sizeof( int ) and sizeof( float ) on your machine?
 
K

karthikbalaguru

1. How do you know it is only allocating 26 bytes?
Printing the sizeof(Dataunit) returns 26 bytes
2. What does sizeof( Dataunit) show on your machine? 28 bytes
3. What is sizeof( int ) and sizeof( float ) on your machine?- Hide quoted text -
2 bytes, 4 bytes respectively.

Thx,
Karthik Balaguru
 
J

Jim Langston

karthikbalaguru said:
Printing the sizeof(Dataunit) returns 26 bytes
28 bytes

Umm... "Printing the sizeof(Dataunit) returns 26 bytes" ... "What does
sizeof(Dataunit) show on your machine? 28 bytes"

It's either one or the other.
 
K

karthikbalaguru

On 2007-09-04 10:43:47 -0400, karthikbalaguru
<[email protected]> said:
I find that the structure padding is not being taken into account
while using 'new' operator.
Is there a way to enable it ?
struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;
p2 = new Dataunit; // This is allocating 26 bytes.
This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using 'new' ?
Structure padding occurs in arrays. new doesn't allocate arrays, so it
can deal in the unpadded size of the type.
The output of sizeof(struct Dataunit) will definitely be different as
it takes into account the
padding based on the architecture(Processor).
Does 'new' do not take the structure padding into account while size
calculation for allocating memory ?

Hmm.. How do you know that it only allocated 26 bytes? And what is the
sizeof(Dataunit) on your machine? Do you know that your machine isn't
2-byte aligned? (In which case there should be no padding in that
struct, everthing in it has a size which is an even multiple of 2...)- Hide quoted text -

- Show quoted text -

I think, something like this should happen w.r.t my machine.
I am not 100% sure about this. But, i think, it should be something as
below -

struct Dataunit /* This is Before Compilation */
{
char dataid[20];
int keyid;
float result;
} *p2;

The above gets converted as below after compilation :):(:):(

struct Dataunit /* This is After Compilation */
{
char dataid[20];
int keyid;
char Padding0[2]; /* For the following 'float result' to be aligned on
a 4 byte boundary */
float result;
} *p2;

So, After compilation the structure is supplemented with padding bytes
to ensure a proper alignment for each of its members.

Any ideas why this is not happening w.r.t 'new' operator in C++ ?
Is structure padding not done by C++ ? :(:(

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

Umm... "Printing the sizeof(Dataunit) returns 26 bytes" ... "What does
sizeof(Dataunit) show on your machine? 28 bytes"

It's either one or the other.

It is 28 bytes . :):)

Karthik Balaguru
 
J

Jim Langston

karthikbalaguru said:
It is 28 bytes . :):)

Well, it seems fairly simple then.

28 bytes. you have 20 for char, 2 for int, 4 for float.

0-19 char
20-21 int // 20 is 4 byte aligned
22-23 padding
24-27 float // 24 is 4 byte aligned

chars don't have to be 4 byte aligned. Just the int and double. So with an
array the second element would be:

28-47 char
48-49 int // 48 is 4 byte aligned
30-31 padding
32-36 float // 32 is 4 byte aligned

The variables that need 4 byte alignment are getting them. The compiler is
using padding just fine and aligning things just fine.
 
J

Jim Langston

Jim Langston said:
Well, it seems fairly simple then.

28 bytes. you have 20 for char, 2 for int, 4 for float.

0-19 char
20-21 int // 20 is 4 byte aligned
22-23 padding
24-27 float // 24 is 4 byte aligned

chars don't have to be 4 byte aligned. Just the int and double. So with
an array the second element would be:

28-47 char
48-49 int // 48 is 4 byte aligned
30-31 padding
32-36 float // 32 is 4 byte aligned

The variables that need 4 byte alignment are getting them. The compiler
is using padding just fine and aligning things just fine.

Same thing, my numbers were just not off. There are 3 types of people in
the world, those who can count and those who can't.

28-47 char
48-49 int // 48 is 4 byte aligned
50-51 padding
52-53 float // 52 is 4 byte aligned
 
K

karthikbalaguru

So what's the problem then?- Hide quoted text -

- Show quoted text -

After compilation the structure is supplemented with padding bytes
to ensure a proper alignment for each of its members. So, 'sizeof'
returns 28 bytes.
But, why does 'new' allocate only 26 bytes ?

Any ideas why this is not happening w.r.t 'new' operator in C++ ?
Is structure padding not done by C++ ? :(:(

If 'new' operator does not take padding into consideration, then
'malloc' appears to very
very useful as it is dependent on 'sizeof' for allocating memory .

In simple terms, the below query's answer will solve many of my
queries/thoughts in my mind -
Does 'new' operator in c++ take into accound the structure padding
while allocating space ?

Thx in advans,
Karthik Balaguru
 
J

Jim Langston

karthikbalaguru said:
After compilation the structure is supplemented with padding bytes
to ensure a proper alignment for each of its members. So, 'sizeof'
returns 28 bytes.
But, why does 'new' allocate only 26 bytes ?

For the third time, how do you know that new is allocating 26 bytes? You
said that sizeof( Dataunit ) is 28 bytes. Where are you getting 26 from?!?
 
K

karthikbalaguru

For the third time, how do you know that new is allocating 26 bytes? You
said that sizeof( Dataunit ) is 28 bytes. Where are you getting 26 from?!?






- Show quoted text -- Hide quoted text -

- Show quoted text -

As you know, 'new' attempts to allocate enough memory on the heap for
the new data and, if successful, returns the address to the newly
allocated memory. When i print the data sequentially starting from the
address returned by the 'new' operator, I find that there is no
padding data present . This made me to raise the query/doubt , whether
'new' takes care of doing structure padding ?
There are other reasons for this query being raised . If it does not
take care of structure padding, then, the way(size of) the memory is
allocated in heap/free-store will differ between C and C++ which is
one important point that i am trying to understand as a major
difference between 'new' and 'malloc'. Because, C's malloc uses
'sizeof' which inturn takes care of structure padding. I wonder, why
structure padding/alignment is not taken care by 'new' ? Any other
inputs / ideas from your side ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

For the third time, how do you know that new is allocating 26 bytes? You
said that sizeof( Dataunit ) is 28 bytes. Where are you getting 26 from?!?






- Show quoted text -- Hide quoted text -

- Show quoted text -

'new', if successful, returns the address to the newly
allocated memory. I printed the data sequentially starting from the
address returned by the 'new' operator. By analysing that data, I find
that there is no
padding data of 2 bytes present between 'int' and 'float' for the
following 'float result' to be aligned on
a 4 byte boundary. Thus, i found that only 26 bytes are allocated
without any padding.

Thx,
Karthik Balaguru
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top