Re: 2/4 bytes boundary problem
- From: "Thomas.Chang" <MingZhang2004@xxxxxxxxx>
- Date: 16 May 2006 05:31:47 -0700
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
.
- References:
- 2/4 bytes boundary problem
- From: Thomas.Chang
- Re: 2/4 bytes boundary problem
- From: Duncan Muirhead
- 2/4 bytes boundary problem
- Prev by Date: Re: help with types needed
- Next by Date: Re: computing t = pow(-11.5, .333)
- Previous by thread: Re: 2/4 bytes boundary problem
- Next by thread: Introduce urself
- Index(es):
Relevant Pages
|