473,399 Members | 3,603 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,399 software developers and data experts.

2/4 bytes boundary problem

Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries.
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
}s;
s s1;
... func(..., &s1.d, ...){...}
But I always got crash when calling func(). I interchanged the
position of "c" and "d", anything went ok.
As the compiler will pad it right. Why I cannot fetch the address of
"d"?
Although the position of "c" and "d" is swapped, "d" is still not on
4 byte boundary. But it works well, why?

Thomas

May 15 '06 #1
20 1914
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:
Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries. No, that's not guaranteed at all. Where did you hear that?
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c; Why not just use chars?
uint16 d; And if you insist on 16 bits, try small int
...
}s;
s s1;
... func(..., &s1.d, ...){...}
But I always got crash when calling func(). I interchanged the
position of "c" and "d", anything went ok.
As the compiler will pad it right. Why I cannot fetch the address of
"d"?
Although the position of "c" and "d" is swapped, "d" is still not on
4 byte boundary. But it works well, why?


May 15 '06 #2
Andrew Poelstra wrote:
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:
Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries.

No, that's not guaranteed at all. Where did you hear that?
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c;

Why not just use chars?


unsigned char would be the correct recommendation in this case.
uint16 d;

And if you insist on 16 bits, try small int


I don't know what a "small int" is but the corresponding type would be
unsigned short int on most implementations. If the OP wants exactly 16
bits this may not be suitable as short int can be more than 16 bits.

Robert Gamble

May 15 '06 #3
set the compiler optimization disable for the structure.

like this:

typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
} __attribute__((packed)) s;

May 15 '06 #4
Thomas.Chang wrote:
Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries.
Not on all systems.
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
}s;
s s1;
... func(..., &s1.d, ...){...}
But I always got crash when calling func(). I interchanged the
position of "c" and "d", anything went ok.
As the compiler will pad it right. Why I cannot fetch the address of
"d"?
Although the position of "c" and "d" is swapped, "d" is still not on
4 byte boundary. But it works well, why?


You've got an error on line 42 of func.

In other words, if you don't show us your code how are we expected to
know what you have done wrong? Post a small complete compilable program
that exhibits the problem.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 15 '06 #5
On 2006-05-15, Robert Gamble <rg*******@gmail.com> wrote:
Andrew Poelstra wrote:
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:
> Hi,
>
> As we know, the compiler will pad structure to make it align on 4
> byte boundaries.

No, that's not guaranteed at all. Where did you hear that?
> I got a structure like this.
> typedef struct{
> uint8 a;
> uint8 b;
> uint8 c;

Why not just use chars?


unsigned char would be the correct recommendation in this case.

Is a uint8 unsigned by default? I honestly don't know.
> uint16 d;

And if you insist on 16 bits, try small int


I don't know what a "small int" is but the corresponding type would be
unsigned short int on most implementations. If the OP wants exactly 16
bits this may not be suitable as short int can be more than 16 bits.

Yes, I meant 'short int'. I got four hours of sleep last night...
May 15 '06 #6
On 2006-05-15, Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-05-15, Robert Gamble <rg*******@gmail.com> wrote:
Andrew Poelstra wrote:
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:
> Hi,
>
> As we know, the compiler will pad structure to make it align on 4
> byte boundaries.
No, that's not guaranteed at all. Where did you hear that?

> I got a structure like this.
> typedef struct{
> uint8 a;
> uint8 b;
> uint8 c;
Why not just use chars?


unsigned char would be the correct recommendation in this case.

Is a uint8 unsigned by default? I honestly don't know.

Wait... that's what the u is for, isn't it? I've really got
to go to bed.
May 15 '06 #7
zh**********@gmail.com wrote:

Please provide context when posting. Google is most definitely *not*
Usenet. See the Google section at http://clc-wiki.net/wiki/Intro_to_clc
for more information.
set the compiler optimization disable for the structure.

like this:

typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
} __attribute__((packed)) s;


That's strange, my C compiler rejects that. Perhaps __attribute__ is not
part of C but an extension provided by some compilers? Do you have any
reason to suspect that the OP is only interested in compilers that
support this extension?
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 15 '06 #8
On Mon, 15 May 2006 06:58:15 -0700, Thomas.Chang wrote:
Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries.
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
}s;
s s1;
... func(..., &s1.d, ...){...}
But I always got crash when calling func(). I interchanged the
position of "c" and "d", anything went ok.
As the compiler will pad it right. Why I cannot fetch the address of
"d"?
Although the position of "c" and "d" is swapped, "d" is still not on
4 byte boundary. But it works well, why?

Thomas


Hard to say without more details. For example this:
#include <stdio.h>
typedef struct
{ char a;
char b;
char c;
short int d;
int e;
} s;
static void hmm( s* s, short int* d)
{ printf( "hmm %d -> %d\n", (char*)d-(char*)s, *d);
}
int main( int argc, char** argv)
{
s s1 = { 1, 2, 3, 444, 5};
hmm( &s1, &s1.d);
return 0;
}
compiled with "gcc -Wall -pedantic -ansi -o pad pad.c" (gcc 3.4.5) doesn't
show any problems -- abd prints hmm "4 -> 444" as one would expect.

Duncan

May 15 '06 #9
Andrew Poelstra wrote:
On 2006-05-15, Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-05-15, Robert Gamble <rg*******@gmail.com> wrote:
Andrew Poelstra wrote:
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:
> Hi,
>
> As we know, the compiler will pad structure to make it align on 4
> byte boundaries.
No, that's not guaranteed at all. Where did you hear that?

> I got a structure like this.
> typedef struct{
> uint8 a;
> uint8 b;
> uint8 c;
Why not just use chars?

unsigned char would be the correct recommendation in this case.
Is a uint8 unsigned by default? I honestly don't know.

Wait... that's what the u is for, isn't it?


One would assume so.
I've really got to go to bed.


Robert Gamble

May 15 '06 #10
On Mon, 15 May 2006 15:16:17 +0100
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
You've got an error on line 42 of func.

In other words, if you don't show us your code how are we expected to
know what you have done wrong? Post a small complete compilable
program that exhibits the problem.
http://clc-wiki.net/wiki/Intro_to_clc


Why people say that it's an "error on line 42"? Why specifically line
42? I looked for it on c-faq.com and that wiki but there's no entries
for it.
May 15 '06 #11
Rafael Almeida opined:
On Mon, 15 May 2006 15:16:17 +0100
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
You've got an error on line 42 of func.

In other words, if you don't show us your code how are we expected
to know what you have done wrong? Post a small complete compilable
program that exhibits the problem.
http://clc-wiki.net/wiki/Intro_to_clc


Why people say that it's an "error on line 42"? Why specifically line
42? I looked for it on c-faq.com and that wiki but there's no entries
for it.


Look up Hitchhikers Guide to the Galaxy, by Douglas Adams. I'd go even
further, and recommend you actually read it (and all the sequels).

42 is the answer to Life, Universe, and Everything. But, what is the
*question*?

--
Time is fluid ... like a river with currents, eddies, backwash.
-- Spock, "The City on the Edge of Forever", stardate 3134.0

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 15 '06 #12
On 2006-05-15, Rafael Almeida <ra*****@dcc.ufmg.br> wrote:
On Mon, 15 May 2006 15:16:17 +0100
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
You've got an error on line 42 of func.

In other words, if you don't show us your code how are we expected to
know what you have done wrong? Post a small complete compilable
program that exhibits the problem.
http://clc-wiki.net/wiki/Intro_to_clc


Why people say that it's an "error on line 42"? Why specifically line
42? I looked for it on c-faq.com and that wiki but there's no entries
for it.


42 is a reference to "Hitchhiker's Guide to the Galaxy" and is a very
profound number. Line 42 means that *you didn't post any code* (or not
enough code) and therefore must assume that the code is floating
through space and is part of the very fabric of the universe. And line
42 has a glitch which causes all the problems of the world.

In most cases, unposted code is merely unposted code and nothing else.
But we can hope...

(I starred the only important part of that paragraph)
May 15 '06 #13
Rafael Almeida wrote:
On Mon, 15 May 2006 15:16:17 +0100
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
You've got an error on line 42 of func.

In other words, if you don't show us your code how are we expected
to know what you have done wrong? Post a small complete compilable
program that exhibits the problem.
http://clc-wiki.net/wiki/Intro_to_clc


Why people say that it's an "error on line 42"? Why specifically line
42? I looked for it on c-faq.com and that wiki but there's no entries
for it.

It's the answer to the question, always. Life, the Universe,
everything.

<http://en.wikipedia.org/wiki/The_Ans...erse,_and_Ever
ything>


Brian
May 15 '06 #14

Rafael Almeida wrote:
On Mon, 15 May 2006 15:16:17 +0100
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
You've got an error on line 42 of func.

In other words, if you don't show us your code how are we expected to
know what you have done wrong? Post a small complete compilable
program that exhibits the problem.
http://clc-wiki.net/wiki/Intro_to_clc


Why people say that it's an "error on line 42"? Why specifically line
42? I looked for it on c-faq.com and that wiki but there's no entries
for it.


It's related to the cosmological constant (between 40 and 50, if it's
really a constant - Hubble diagrams help). The Hitchhiker's Guide...
(book, movie) makes it 42 and sets it as the answer to everything.
There's no theory behind 42 other than "between 40 and 50" so it's like
popping a number from the top of your head without having any idea what
the number is, exactly like finding the error in a piece of code that
you don't have access to.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

May 16 '06 #15
Nelu said:
There's no theory behind 42 other than "between 40 and 50"


Not even that.

DNA was astounded to learn that astronomers are finding a use for 42. When
he chose it, it was for no other reason than that it was the sort of number
you would be happy to take home and introduce to your parents.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 16 '06 #16
My bad for not stating that my program is just a little piece of an
embedded system, a router.
I know that a lot of factors may cause the crash. But it does work well
after I swapped the position of "c" and "d".
I heard that problems occur when you try to fetch a member which
locates across boundaries, but I don't know what the problem can be,
crash? Maybe...
btw, the CPU is IXP425 for my router. I know that the "packed" option
can prohibit padding, which is dangerous for packets constructing or
parsing. But my code is runtime data structure, which can be paded at
one's wish.
Then why the CPU didn't pad it? Is it sleeping?... Oh, god!

Duncan Muirhead wrote:
On Mon, 15 May 2006 06:58:15 -0700, Thomas.Chang wrote:
Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries.
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
}s;
s s1;
... func(..., &s1.d, ...){...}
But I always got crash when calling func(). I interchanged the
position of "c" and "d", anything went ok.
As the compiler will pad it right. Why I cannot fetch the address of
"d"?
Although the position of "c" and "d" is swapped, "d" is still not on
4 byte boundary. But it works well, why?

Thomas


Hard to say without more details. For example this:
#include <stdio.h>
typedef struct
{ char a;
char b;
char c;
short int d;
int e;
} s;
static void hmm( s* s, short int* d)
{ printf( "hmm %d -> %d\n", (char*)d-(char*)s, *d);
}
int main( int argc, char** argv)
{
s s1 = { 1, 2, 3, 444, 5};
hmm( &s1, &s1.d);
return 0;
}
compiled with "gcc -Wall -pedantic -ansi -o pad pad.c" (gcc 3.4.5) doesn't
show any problems -- abd prints hmm "4 -> 444" as one would expect.

Duncan


May 16 '06 #17
yes, the u stands for unsigned.
I just want to emphasize the length of storage for each member.

Andrew Poelstra wrote:
On 2006-05-15, Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-05-15, Robert Gamble <rg*******@gmail.com> wrote:
Andrew Poelstra wrote:
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:
> Hi,
>
> As we know, the compiler will pad structure to make it align on 4
> byte boundaries.
No, that's not guaranteed at all. Where did you hear that?

> I got a structure like this.
> typedef struct{
> uint8 a;
> uint8 b;
> uint8 c;
Why not just use chars?

unsigned char would be the correct recommendation in this case.

Is a uint8 unsigned by default? I honestly don't know.

Wait... that's what the u is for, isn't it? I've really got
to go to bed.


May 16 '06 #18


Nelu wrote On 05/15/06 23:40,:
Rafael Almeida wrote:
On Mon, 15 May 2006 15:16:17 +0100
Flash Gordon <sp**@flash-gordon.me.uk> wrote:

You've got an error on line 42 of func.

In other words, if you don't show us your code how are we expected to
know what you have done wrong? Post a small complete compilable
program that exhibits the problem.
http://clc-wiki.net/wiki/Intro_to_clc


Why people say that it's an "error on line 42"? Why specifically line
42? I looked for it on c-faq.com and that wiki but there's no entries
for it.

It's related to the cosmological constant (between 40 and 50, if it's
really a constant - Hubble diagrams help). The Hitchhiker's Guide...
(book, movie) makes it 42 and sets it as the answer to everything.
There's no theory behind 42 [...]


<off-topic>

I beg to differ: There *is* a theory behind 42. It's
not only the Ultimate Answer to the Question of Life, the
Universe, and Everything, but it's also the answer to the
specific question

What do you get if you multiply six by nine?

.... which I think should be read with an emphasis on the
second "you."

</off-topic>

--
Er*********@sun.com

May 16 '06 #19
Eric Sosman <Er*********@sun.com> writes:
[...]
<off-topic>

I beg to differ: There *is* a theory behind 42. It's
not only the Ultimate Answer to the Question of Life, the
Universe, and Everything, but it's also the answer to the
specific question

What do you get if you multiply six by nine?

... which I think should be read with an emphasis on the
second "you."

</off-topic>


#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 19 '06 #20

Keith Thompson wrote:
Eric Sosman <Er*********@sun.com> writes:
[...]
<off-topic>

I beg to differ: There *is* a theory behind 42. It's
not only the Ultimate Answer to the Question of Life, the
Universe, and Everything, but it's also the answer to the
specific question

What do you get if you multiply six by nine?

... which I think should be read with an emphasis on the
second "you."

</off-topic>


#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}


I didn't know this one. Funny :-).
Thank you.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

May 19 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniele Varrazzo | last post by:
Hi everybody, i'm experiencing crashing of the apache process when a mod_python handler access the properties "boundary" and "allowed_xmethods" of the request argument. This prevents the use of...
27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
14
by: Singleton | last post by:
Can some one guide me what is word boundary? google is no good for me for this thanks in advance
1
by: Devrobcom | last post by:
Hi I have read somwhere that this problem is handled by the Intel cpu's (firmware), but what will happen on other types of cpu's. struct { char aa; // even boundary 0xaa00 char bb; ...
19
by: James Harris | last post by:
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields the number of BYTES required to store an object of the type of its operand. What happens if C is running on a machine that...
11
by: Taran | last post by:
Hi all, I was wondering how does address alignment to x byte boundary is done. For example, if I say "adjust the requested size to be on a 4-byte boundary" or for that matter 8 byte boundary....
8
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while...
12
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses...
2
by: kgeorge2 | last post by:
A library which i use, has an array class class LibArray { public: LibArray():_array(NULL),_len(0); LibArray(int n, float val):_array( new float),_len(n){} /** more complete definition...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.