[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

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=2031&DI=1055&SU=http%3A//www.hotmail.com&HL=LINKTAG1OPENINGTEXT_MSNBH



Reply to: