views:

218

answers:

1

What code have you written in c that is not defined by either c89 or c99 standards?

Specifically, I am looking for techniques like using pointer manipulation to move within a struct instead of using the member operator. This works fine on most compilers, but the compiler is free to add buffer space between struct members, so will not always work correctly.

For example:

struct SampleStruct{
    int member1;
    int member2;
}thisStruct;

and then in main...

struct SampleStructPtr* thisStructPtr = &thisStruct;
*(int*)thisStructPtr = 5;
thisStructPtr = (void*)thisStructPtr + sizeOf(int);
*(int*)thisStructPtr = 7;

This is not guaranteed to work under the standard spec.

This is for a research project, so any help would be appreciated!

+2  A: 

I use to very commonly do things like

struct mystruct{
  char bit1:1;
  char bit2:1;
  char bit3:1;
...
}__packed;

gcc and pcc handled it fine using the non-standard __PACKED__ attribute. If you look real close, there is a really strange problem with that code if you really think about it though. char bit1:1; Char is (on the systems I use) defaulting to a signed type. So because I declared it a signed 1 bit field, there is no room for storing actual data. There is only room for storing the sign-bit. Thus, the only possible values are -1 and 0. Even though no compiler I've yet to see complains about assigning it to 1.

Earlz