structure padding not considered by 'new'

  • Thread starter karthikbalaguru
  • Start date
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

Jim Langston

Erik Wikström said:
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.
 
B

Bo Persson

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
 
J

Jim Langston

Bo Persson said:
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.
 
K

karthikbalaguru

Jim said:
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.

Yeah, new is returning 28 bytes and the 2 padding bytes are at the end
of the data.
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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top