Re: using sizeof
Jon Dowland wrote: Because sizeof is not really the size of the struct, it is the distance between adjacent structs in an array. Alignment forces the extra bytes I'm not quite sure I get what you're saying here. Yes, alignment pads out the structure. But I'm not sure where arrays come into it :- sizeof(struct whatever) is applicable to a single instance of a struct too. What he's saying is that struct FredStruct Fred[2]; ... sizeof(struct Fred[0]) is the same as (void *)(Fred[1])-(void *)(Fred[0]) Structures may get padding at the end precisely for this reason. It is not obvious that this is the case when one talks about a single instance. Mike -- p=p=%c%s%c;main(){printf(p,34,p,34);};main(){printf(p,34,p,34);} Oppose globalization and One World Governments like the UN. This message made from 100% recycled bits. You have found the bank of Larn. I can explain it for you, but I can't understand it for you. I speak only for myself, and I am unanimous in that! -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: using sizeof
On Mon, 14 May 2007 16:24:59 +, J HU wrote: Dear all, Perhaps it's an easy/silly question but I don't understand how it works. (I'm working in a debian) I have declared a structure and I'm using the sizeof to get the size of this structure. After the call I get that the total size is 64Bytes but if I get the size of each field and I add them manually I get that it should be 61Bytes... Anyone knows why the result is not the same? Thanks in advance Javi Because sizeof is not really the size of the struct, it is the distance between adjacent structs in an array. Alignment forces the extra bytes. In my opinion, 'sizeof' should have been called 'spacingof', and there should have been other properties for alignment, such as sizeof, modulusof. and remainderof, so you could do your own storage allocation calculations in a machine-independent way. Something could take sizeof bytes starting at an address that is remainderof bytes past a n address that is a multiple of modulusof. The log2 of modulus would also be useful. -- hendrik -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: Re: using sizeof
Because sizeof is not really the size of the struct, it is the distance between adjacent structs in an array. Alignment forces the extra bytes I'm not quite sure I get what you're saying here. Yes, alignment pads out the structure. But I'm not sure where arrays come into it :- sizeof(struct whatever) is applicable to a single instance of a struct too. -- Jon Dowland -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
using sizeof
Dear all, Perhaps it's an easy/silly question but I don't understand how it works. (I'm working in a debian) I have declared a structure and I'm using the sizeof to get the size of this structure. After the call I get that the total size is 64Bytes but if I get the size of each field and I add them manually I get that it should be 61Bytes... Anyone knows why the result is not the same? Thanks in advance Javi Code: #include stdio.h #include sys/socket.h typedef struct Packet{ //char transportIdentifier; int mode; int idPacket; int idMessage; int fragmentNumber; struct sockaddr source; struct sockaddr destination; int length; int totalLength; int* groupSet; unsigned char data[1]; }; int main (){ Packet infoPacket; printf(Size of mode %d\n,sizeof(infoPacket.mode)); printf(Size of idPacket %d\n,sizeof(infoPacket.idPacket)); printf(Size of idMessage %d\n,sizeof(infoPacket.idMessage)); printf(Size of fragmentNumber %d\n,sizeof(infoPacket.fragmentNumber)); printf(Size of source %d\n,sizeof(infoPacket.source)); printf(Size of destination %d\n,sizeof(infoPacket.destination)); printf(Size of length %d\n,sizeof(infoPacket.length)); printf(Size of totalLength %d\n,sizeof(infoPacket.totalLength)); printf(Size of groupSet %d\n,sizeof(infoPacket.groupSet)); printf(Size of data %d\n,sizeof(infoPacket.data)); printf(Total Size of infoPacket = %d or %d or %d\n,sizeof(infoPacket),sizeof(Packet),sizeof(struct Packet)); } Output: Size of mode 4 Size of idPacket 4 Size of idMessage 4 Size of fragmentNumber 4 Size of source 16 Size of destination 16 Size of length 4 Size of totalLength 4 Size of groupSet 4 Size of data 1 Total Size of infoPacket = 64 or 64 or 64 _ Descarga gratis la Barra de Herramientas de MSN http://www.msn.es/usuario/busqueda/barra?XAPID=2031DI=1055SU=http%3A//www.hotmail.comHL=LINKTAG1OPENINGTEXT_MSNBH -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: using sizeof
J HU [EMAIL PROTECTED] wrote: I have declared a structure and I'm using the sizeof to get the size of this structure. After the call I get that the total size is 64Bytes but if I get the size of each field and I add them manually I get that it should be 61Bytes... Anyone knows why the result is not the same? 61 bytes doesn't fit evenly on a 32-bit boundary; 64 bytes does. Your struct is padded so that your processor can page through them faster. If you really want your struct to take up only the 61 bytes for it's structure, you can use the packed attribute; struct foo { int a; int b; int c; } __attribute__((__packed__)); Kinda odd syntax, but it works. :) Really, it's a lot faster for your processor to deal with structs that start on even 32-bit boundaries, so only pack the structure if you absolutely need it (eg; to speak a particular networking/storage protocol...) - Tyler -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
Re: using sizeof
On 5/14/07, J HU [EMAIL PROTECTED] wrote: Dear all, Perhaps it's an easy/silly question but I don't understand how it works. (I'm working in a debian) I have declared a structure and I'm using the sizeof to get the size of this structure. After the call I get that the total size is 64Bytes but if I get the size of each field and I add them manually I get that it should be 61Bytes... Anyone knows why the result is not the same? I noticed you got an answer. This page can help also: http://en.wikipedia.org/wiki/Data_structure_alignment The effects of alignment in performance (and even stability, if you're writing low level code) depend on the architecture. For instance: A memory access can be much faster on some architectures if the base address for the request is aligned. So, if you can afford the padding space in memory, also remember that you have to be careful with pointer arithmetic. You should not assume that: *((char*)(pointer + sizeof(mystruct) - 1)) Is the last byte of your structure. Regards, Nelson.- -- http://arhuaco.org http://emQbit.com -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]