473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2068
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: | last post by:
When we have a structure in the following form typedef struct { int I1; int I2; int I3; int I4; float F1; float F2; float F3;
15
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a...
13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
2
by: Sachin | last post by:
typdef struct { int i; char ch; }str; str str_var; char x, y; main() { //do nothing
16
by: quantumred | last post by:
Can I use pointer arithmetic on the members of a structure in the following way? Should I be worried about structure padding? This works in my debugger but I wonder if I'm bending some rule here. ...
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
4
by: junky_fellow | last post by:
Can somebody please tell me about the structure alignment rules ? What I found was that on my system (cygwin running on PC, size of int=4 sizeof long=4, size of long long = 8) the cygwin compiler...
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.