468,738 Members | 2,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

structure padding not considered by 'new'

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

Sep 4 '07 #1
24 1856
karthikbalaguru wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 4 '07 #2
On 2007-09-04 10:43:47 -0400, karthikbalaguru
<ka***************@gmail.comsaid:
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.

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

Sep 4 '07 #3
On Sep 4, 8:24 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-09-04 10:43:47 -0400, karthikbalaguru
<karthikbalagur...@gmail.comsaid:


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.

--
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

Sep 4 '07 #4
On 2007-09-04 12:37:06 -0400, karthikbalaguru
<ka***************@gmail.comsaid:
On Sep 4, 8:24 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2007-09-04 10:43:47 -0400, karthikbalaguru
<karthikbalagur...@gmail.comsaid:


>>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.

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?

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

Sep 4 '07 #5
karthikbalaguru <ka***************@gmail.comwrote in
news:11**********************@r29g2000hsg.googlegr oups.com:
On Sep 4, 8:24 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2007-09-04 10:43:47 -0400, karthikbalaguru
<karthikbalagur...@gmail.comsaid:
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...)

Sep 4 '07 #6
On Sep 4, 10:57 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote innews:11**********************@r29g2000hsg.google groups.com:


On Sep 4, 8:24 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-09-04 10:43:47 -0400, karthikbalaguru
<karthikbalagur...@gmail.comsaid:
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

Sep 4 '07 #7
"karthikbalaguru" <ka***************@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
On Sep 4, 10:57 pm, Andre Kostur <nntps...@kostur.netwrote:
>karthikbalaguru <karthikbalagur...@gmail.comwrote
innews:11**********************@r29g2000hsg.googl egroups.com:
On Sep 4, 8:24 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-09-04 10:43:47 -0400, karthikbalaguru
<karthikbalagur...@gmail.comsaid:
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?
Sep 5 '07 #8
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
Sep 5 '07 #9

"karthikbalaguru" <ka***************@gmail.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.com...
>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
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.
>3. What is sizeof( int ) and sizeof( float ) on your machine?- Hide
quoted text -
2 bytes, 4 bytes respectively.

Sep 5 '07 #10
On Sep 4, 10:57 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote innews:11**********************@r29g2000hsg.google groups.com:


On Sep 4, 8:24 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-09-04 10:43:47 -0400, karthikbalaguru
<karthikbalagur...@gmail.comsaid:
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

Sep 5 '07 #11
On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11*********************@22g2000hsm.googlegrou ps.com...
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

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

Sep 5 '07 #12
On Sep 4, 7:58 pm, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
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
How can it be both?

Sep 5 '07 #13
"karthikbalaguru" <ka***************@gmail.comwrote in message
news:11*********************@d55g2000hsg.googlegro ups.com...
On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11*********************@22g2000hsm.googlegro ups.com...
>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

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 . :):)
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.
Sep 5 '07 #14
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:iO***************@newsfe02.lga...
"karthikbalaguru" <ka***************@gmail.comwrote in message
news:11*********************@d55g2000hsg.googlegro ups.com...
>On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>>"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11*********************@22g2000hsm.googlegr oups.com...

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

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 . :):)

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
Sep 5 '07 #15
karthikbalaguru <ka***************@gmail.comwrote in
news:11*********************@d55g2000hsg.googlegro ups.com:
On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11*********************@22g2000hsm.googlegro ups.com...
>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

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
So what's the problem then?
Sep 5 '07 #16
On Sep 5, 7:37 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote innews:11*********************@d55g2000hsg.googleg roups.com:


On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message
>news:11*********************@22g2000hsm.googlegro ups.com...
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
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

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

Sep 6 '07 #17
"karthikbalaguru" <ka***************@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
On Sep 5, 7:37 pm, Andre Kostur <nntps...@kostur.netwrote:
>karthikbalaguru <karthikbalagur...@gmail.comwrote
innews:11*********************@d55g2000hsg.google groups.com:
On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message
>>news:11*********************@22g2000hsm.googlegr oups.com...
>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
>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

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 ?
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?!?
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 ?

Sep 6 '07 #18
On Sep 6, 9:13 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...


On Sep 5, 7:37 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote
innews:11*********************@d55g2000hsg.googleg roups.com:
On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message
>news:11*********************@22g2000hsm.googlegro ups.com...
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
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
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 ?

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?!?
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 ?- Hide quoted text -

- 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

Sep 6 '07 #19
On Sep 6, 9:13 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...


On Sep 5, 7:37 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote
innews:11*********************@d55g2000hsg.googleg roups.com:
On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message
>news:11*********************@22g2000hsm.googlegro ups.com...
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
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
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 ?

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?!?
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 ?- Hide quoted text -

- 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

Sep 6 '07 #20
On 2007-09-06 08:36, karthikbalaguru wrote:
On Sep 6, 9:13 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegr oups.com...


On Sep 5, 7:37 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote
innews:11*********************@d55g2000hsg.google groups.com:
On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message
>>news:11*********************@22g2000hsm.googlegr oups.com...
>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
>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
>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 ?

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?!?
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 ?- Hide quoted text -

- 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.
new does the Right Thing (TM), which means that there are three
possibilities, 1) you have a defect implementation, 2) you
misinterpreted the data, or 3) you platform is not 4-byte aligned.

--
Erik Wikström
Sep 6 '07 #21
"Erik Wikström" <Er***********@telia.comwrote in message
news:PY****************@newsb.telia.net...
On 2007-09-06 08:36, karthikbalaguru wrote:
>On Sep 6, 9:13 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>>"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googleg roups.com...

On Sep 5, 7:37 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote
innews:11*********************@d55g2000hsg.googl egroups.com:

On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11*********************@22g2000hsm.googleg roups.com...

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

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

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 ?

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?!?

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 ?- Hide quoted text -

- 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.

new does the Right Thing (TM), which means that there are three
possibilities, 1) you have a defect implementation, 2) you misinterpreted
the data, or 3) you platform is not 4-byte aligned.
4) floats do not have to be 4 byte aligned on your implementation.

new is returning 28 bytes. The 2 padding bytes are at the end of the data,
which you're not looking at.

start using offsetof
size_t offsetof( structName, memberName )

Just because you are only looking at 26 bytes doesn't mean that's all the OS
returned by new.
Sep 6 '07 #22
Erik Wikström wrote:
:: On 2007-09-06 08:36, karthikbalaguru wrote:
:::
::: '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.
::
:: new does the Right Thing (TM), which means that there are three
:: possibilities, 1) you have a defect implementation, 2) you
:: misinterpreted the data, or 3) you platform is not 4-byte aligned.
::

Most likely 2 and 3.

When "printed the data", what does pad bytes look like? Could it be
that they contain nul characters (or soemthing) that are not visible
in the output?
Bo Persson
Sep 6 '07 #23
"Bo Persson" <bo*@gmb.dkwrote in message
news:5k************@mid.individual.net...
Erik Wikström wrote:
:: On 2007-09-06 08:36, karthikbalaguru wrote:
:::
::: '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.
::
:: new does the Right Thing (TM), which means that there are three
:: possibilities, 1) you have a defect implementation, 2) you
:: misinterpreted the data, or 3) you platform is not 4-byte aligned.
::

Most likely 2 and 3.

When "printed the data", what does pad bytes look like? Could it be that
they contain nul characters (or soemthing) that are not visible in the
output?
They contain some undefined data. Since the compiler is never going to do
anything with them, they could be anything. Most likely they contain
whatever happened to be in the memory the compiler grabbed to give to the
structure. But since it's undefined, it's not even easy to speculate.
Sep 6 '07 #24

Jim Langston wrote:
"Erik Wikström" <Er***********@telia.comwrote in message
news:PY****************@newsb.telia.net...
On 2007-09-06 08:36, karthikbalaguru wrote:
On Sep 6, 9:13 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegr oups.com...

On Sep 5, 7:37 pm, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote
innews:11*********************@d55g2000hsg.google groups.com:

On Sep 5, 8:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"karthikbalaguru" <karthikbalagur...@gmail.comwrote in message

news:11*********************@22g2000hsm.googlegr oups.com...

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

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

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 ?

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?!?

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 ?- Hide quoted text -

- 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.
new does the Right Thing (TM), which means that there are three
possibilities, 1) you have a defect implementation, 2) you misinterpreted
the data, or 3) you platform is not 4-byte aligned.

4) floats do not have to be 4 byte aligned on your implementation.

new is returning 28 bytes. The 2 padding bytes are at the end of the data,
which you're not looking at.

start using offsetof
size_t offsetof( structName, memberName )

Just because you are only looking at 26 bytes doesn't mean that's all theOS
returned by new.
Yeah, new is returning 28 bytes and the 2 padding bytes are at the end
of the data.
Thx.

Karthik Balaguru

Sep 14 '07 #25

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Sourcerer | last post: by
13 posts views Thread by Amarendra | last post: by
2 posts views Thread by Sachin | last post: by
28 posts views Thread by kyle york | last post: by
4 posts views Thread by junky_fellow | last post: by
12 posts views Thread by Kislay | last post: by
10 posts views Thread by Scott Townsend | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.