Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - C struct problem | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
MathOnNapkins

Math n' Hacks
Level: 67

Posts: 1769/2189
EXP: 2495887
For next: 96985

Since: 03-18-04
From: Base Tourian

Since last post: 1 hour
Last activity: 32 min.
Posted on 04-24-05 07:08 AM Link | Quote
I am currently looking at someone else's code, trying to comment it, make sense of it. It's over 20,000 lines long. Thus, it has some complex behavior going on.

*This is C source btw*

I modified one of the structs by grouping like primitive variables towards the top, and windows API variables towards the bottom. e.g.

before:

typedef struct {

int var1;
HWND win;
short index1;
short index2;
RECT rc1;
int var2;
} myStruct;

after:

typedef struct {

int var1;
int var2;

short index1;
short index2;

HWND win;
RECT rc1;

} myStruct;

the above example is fabricated, b/c I can't really release the code since I do not have permission to, and the person who I got it from is rarely online. But the example is roughly equivalent. I did not believe doing something like the above could affect program behavior, but alas, it did. Can anyone explain why struct declarations order might matter? I'm used to thinking in Java, where everything is class encapsulated and order doesn't really matter for the most part.

Here is a list of the types of data present in the struct:
BITMAPINFOHEADER
RGBQUAD
HWND
HPALETTE
RECT

int
short
void*
char
char*

there is also another struct variable type at the top of the this one, but I have not moved it at all, and it was defined outside the scope of this typedef declaration. (above)

edit: typos and clarification.


(edited by MathOnNapkins on 04-23-05 02:11 PM)
(edited by MathOnNapkins on 04-23-05 02:28 PM)
Dish

Spiny
Level: 38

Posts: 354/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-24-05 08:05 AM Link | Quote
Various things could cause a change in behavior is elements of a struct are moved around -- however they all can be avoided by careful coding practices (or really -- just not tremendously sloppy coding practices)

Typically the elements in a struct are arranged in memory in the same order in which they are listed. Occasionally with padding to the nearest 4th byte. Consider the following example:




typedef struct
{
int var1;
int var2;
int var3;
int var4;
} myStruct;

myStruct str;
int* ptr;

void SomeFunction()
{
//point to var1
ptr = &str.var1;

// point to var2
ptr++; /* this will add sizeof(int) to the value of the pointer, making it point to whatever follows var1 in memory. Since var2 follows var1 in the struct, ptr will point to var2 after this line (however this is very sloppy!). Were the struct to be rearranged, and say var3 is put after var1 in the struct, this would no longer point to var2 (changing program's behavior) */

// point back to var1
ptr =(int*)(&str); /* this sets the point to point to the very start of str. Since the first element in the struct is var1, this will point to var1 -- but again were the struct to be rearranged, this could point to a different var. Again this is VERY SLOPPY */
}




Pretty much -- whenever a pointer to the struct or one of its contents is handled less than carefully, the behavior of the program might rely on the order in which the struct is built. This could be due to reading/writing the struct to/from a file (though with HWNDs and pointers in the struct, I doubt this is the case). Or perhaps passing a void pointer to another function which uses a different and mismatching structure definition (or perhaps no structure definition at all -- maybe it just uses the raw bytes or something).

The one real LEGIT reason for the change in behavior would be if the struct is part of a union -- in which case the memory occupied by each var MUST remain the same so that it properly interacts with other union members (that's kind of the point of a union, y'know?)

Anyway -- as far as your commenting project is concerned -- if moving around the struct members gives you trouble -- just don't do it. It's such a trivial thing to dislike the order of struct members -- not worth the headache of rearranging the code.
MathOnNapkins

Math n' Hacks
Level: 67

Posts: 1770/2189
EXP: 2495887
For next: 96985

Since: 03-18-04
From: Base Tourian

Since last post: 1 hour
Last activity: 32 min.
Posted on 04-24-05 08:55 AM Link | Quote
d'oh, I should have thought it would deal with pointer arithmetic. thanks Disch. IN VC++ 6.0 the code generates 1500 errors warnings ( whoops), so I suppose it could be considered somewhat sloppy, but hell it works.


(edited by MathOnNapkins on 04-23-05 03:56 PM)
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - C struct problem | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.012 seconds.