Re: 2/4 bytes boundary problem



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

.



Relevant Pages

  • Re: WLAN im Hotel
    ... dhcp_message_type (uint8): ACK 0x5 ... router: ...
    (de.comp.sys.mac.internet)
  • Re: Eagle: via inside a pad, is it possible?
    ... I'm not sure if it's a principal problem, and vias are just ... >not expected to be inside the pads or I'm just not setting the router ... on the schematic to both the SMT pad and the via. ... Please do not send unsolicited bulk e-mail or unsolicited ...
    (sci.electronics.cad)