#48173 - jmva16 - Sun Jul 17, 2005 10:35 pm
I have an odd thing happening with the compiler. I have defined a structure as such:
typedef struct
{
short x,y,rot,ability,tags;
} myobject;
When I do sizeof(myobject) it returns 12. I should be 10 as each short is 2 bytes and I defined 5 of them. I do not know why is is adding the additional 2 bytes. I would like the actual size because I am using it to load external data (off the cart) and after I get the first data out and increment my pointer by sizeof(myobject), the data is misaligned. Any thoughts?
Thank you
#48183 - tepples - Mon Jul 18, 2005 12:25 am
Perhaps GCC rounds struct sizes up to the nearest multiple of sizeof(int) because of the target architecture's own addressing requirements. If you can't change the program that generates external data, look in the GCC manual or other documentation for an attribute that specifies packing.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#48186 - Lord Graga - Mon Jul 18, 2005 12:44 am
sizeof() should return the pricise size of a stuct, I am sure, not rounded. It sounds strange, but try this:
Code: |
typedef struct
{
short x;
short y;
short rot;
short ability;
short tags;
} myobject; |
#48191 - MrAdults - Mon Jul 18, 2005 3:02 am
Quote: |
sizeof() should return the pricise size of a stuct, I am sure, not rounded. It sounds strange, but try this: |
tepples is correct, struct data is 4-byte aligned by default, and sizeof returns the actual struct size as opposed to the size of the members you have defined. This is not a requirement for the architecture, but I would recommend allowing it where possible for performance (access) reasons. You can use the packed attribute, or just make sure your data is aligned yourself, e.g.:
Code: |
typedef struct
{
short x;
short y;
short rot;
short ability;
short tags;
short padding;
} myobject;
|
-Rich
#48306 - jmva16 - Mon Jul 18, 2005 11:09 pm
Thank you for the replies. tepples must be right, I have some other structures of varying sizes that I am handling the same way. One has two shorts, therefore a size of 4, and it works fine. Another has 4 shorts and therefore a size of 12, and it works fine. I have another of size 34 and sizeof() returns 36. I have no control over the external data, so for now I have hard coded the sizes in (crappy code practice), but I will look into the attributes tepples mentioned. If I come up with anything I will post the solution.
Thanks again!
#48339 - dafer - Tue Jul 19, 2005 10:36 am
I have a related question.
I have a Oam class that is allocated in oam memory but I wonder if I can be sure that the variables of the class acctually is laid out as it is in the class. When I compile it it does work but can I be sure it allways does?
Code: |
class Oam{
public:
uint16 attribute0;
uint16 attribute1;
uint16 attribute2;
private:
uint16 rotData;
};
|
_________________
/As meningful to you as words written in water.
#48382 - sajiimori - Tue Jul 19, 2005 6:24 pm
OAM is only relevant on GBA/DS and compilers for those platforms are unlikely to align structs to more than 4 bytes. If you ever run into a problem, you could always use the packed attribute.
#48969 - cooky - Sun Jul 24, 2005 10:34 pm
Simply it always returns the actual size because if you say had a function that took a void* as its data then the person wanted to access that as an array of singular bytes, for example, but still accesses the areas as he/she would do using an array then the size of must be the true and correct size of the data else you would get the wrong position in the array.
Example
Code: |
void func(void* arr)
{
//accessing 5th member
*(((u8)arr)+size_of(struc)*5);
} |
It is however not ussually nessesary as you could just type cast it as the struct and use the [] operator to effectively make this code.
Code: |
void func(void* arr)
{
//accessing 5th member
((struc)arr)[5];
} |
Untested ofcourse
Anyway please keep questions about C and C++ in the correct section; [the DS development forum] is the section for DS development specifically
Moved. You're welcome. -- mod