I had a deeper look at that and I think I was a little bit wrong in my first answer.
First, the purpose of having structure alignment is for optimizing the speed. It is faster to get the data at once rather (so, contained in the same 8 bytes block starting at an index multiple of 8) than needing to get it in two operations (get the first part that is contained in the previous memory block from the second part).
Thus, for 32 bits systems, memory blocks are accessed in 8 bytes blocks. Thus, the member will be contained completely in a 8 bytes block.
So, let's take your example:
First, you put your char: 1 byte.
Then you have your integer (4 bytes): there is enough space to put it there so you have 5 bytes
Then, your float (4 bytes): there is not enough space left in the previous memory block so make it start in a new block (and add 3 padding bytes in the previous block). That makes 12 bytes.
The double now (8 bytes): there is not enough in the previous block so padd it with 4 bytes and put the double in a new memory block.
So, in total, that makes 24 bytes.
So, I was wrong in my first post: your member struct alignment is set on 8-bytes boundary (and not 4). And that is more logical because it is the default value.
I am not fully satisfied with the clarification you gave in the previous update.
Consider only 3 variables declared char(1 byte),int(4 bytes) and float(4 bytes). As you said in a 32-bit system the memory block size would be 8 bytes. In that case, the first block of 8 bytes is first allocated as soon as char variable in declared. Now, the int variable fits in and so totally 5 out of 8 bytes have been filled. The float variable cannot be accomodated in this remaining first block as only 3 bytes are remaining(since float also takes 4 bytes). So, these 3 bytes in the first block are padded(as per your information). For the float variable, another 8 block memory is allocated and only 4 of them ll be occupied. So finally, the total size should be shown as 16 bytes (even though only 12 was occupied), but it shows the value as 12 bytes only.
Could you please clarify me on the same.
I got this doubt because, assume a scenario where you have declared only an char and an int. Now, if you c the total size of the object/class it shows as 8 bytes rather than showing as 5. So, from this I concluded that even the unallocated blocks ll be accounted by the sizeof operator. what us ur opinion on this??
I have to set the size such that it should get same or greater than the largest(never less) member size
What do you mean, it all depends on how the structure need to be alligned.
If you don't use this compile option (set it to default), the allignment will be calculated
on the greatest type in the structure. Is this what you need ?.
For instance I needed to send binary data (all data is layed out in structures) to
another program this -embeded- program was written/compiled on 2 byte alignment
(meaning an int would allign on a 2 byte boundary) while my default allignment causes
an int to be alligned on a 4 byte boundary. I could set the compile option to allign on
2 byte, but -there is always a but - another embeded program used the 4 byte
allignment for its binary data. So I needed to implement both. I left my default alligment
on 4 bytes, and used #pragma pack to set the alligment to 2 bytes for a few structures.
So to answer your question, this is simple: as there is no variable after your last float, there is no need to padd the structure with empty bytes. Remember that this is done only to keep variable in a single block. Thus, there is no need to padd bytes after your last variable because there is nothing after it.
i have a CStringList instance and i dynamically create CString objects and add them to the CStringList Object , now here is my question:
when i want to destroy the CStringList object i first have to 'delete' all the CString objects thati have inserted dynamically!for this i need to get the pointer to the CString....but using the RemoveTail() of the CStringList object i am able to get back only the LPCTSTR of the CString objects ,SO HOW do i GET THE CSTRING object pointer from the CStringList to delete the dynamically created CString objects?????
May I know which return value(LPCTSTR) are you talking about. Because none of the functions that I mentioned 'Find' and 'RemoveAt' returns LPCTSTR. So I am confused as what return value you are talking about.
see,itz not abt deleting from the list(yes that is also one requirement)...but what i need to know is how to "delete" teh allocated memory!(by just removing from teh list, the string only get removed from teh list but it does not free the memory associated with teh CString object that i had dynamically created!...that is why i need teh address of teh CString object!...)
class CStringList : public CObject
The CStringList class supports lists of CString objects. All comparisons are done by value, meaning that the characters in the string are compared instead of the addresses of the strings.
The member functions of CStringList are similar to the member functions of class CObList. Because of this similarity, you can use the CObList reference documentation for member function specifics. Wherever you see a CObject pointer as a return value, substitute a CString (not a CString pointer). Wherever you see a CObject pointer as a function parameter, substitute an LPCTSTR.