Register | Login | |||||
Main
| Memberlist
| Active users
| ACS
| Commons
| Calendar
| Online users Ranks | FAQ | Color Chart | Photo album | IRC Chat |
| |
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. |
| ||
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 |
| ||
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:
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. |
| ||
d'oh, I should have thought it would deal with pointer arithmetic. thanks Disch. IN VC++ 6.0 the code generates 1500 (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 | | | |