LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-09-2005, 08:36 PM   #1
Quest101
LQ Newbie
 
Registered: Nov 2004
Posts: 27

Rep: Reputation: 15
Sizeof returning wrong packet size ??


Hi

I am using the size of function to get the size of a struct, this size assists me in sending packets over a network

struct header
{
u_int32_t a
u_int16_t b

}


now the sizeof operator when returns a size of 8 bytes for the above header

ie sizeof( header ) == 8 Bytes

now if you do the math you will realise that the actual size should be

32bits + 16bits = 48bits which is actually 6 Bytes
****************************************************************

Now when i remove variable b from the struct

struct header
{
u_int32_t a
}

the sizeof operator now returns a correct value of 4 Bytes,




Any help in at least know what causes this error would be greatly appreciated


Thanx in Advance
 
Old 03-10-2005, 08:37 AM   #2
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 93

Rep: Reputation: 17
This is not an error. The compiler ( gcc ? ) will add extra bytes to a struct to make it fill a multiple of 4 bytes. My guess is that this is so that when you have an array of these structs each one begins on a 4 byte boundary. If you are using gcc, you can use pragmas to disable this padding as shown below.



Code:
#include <iostream>

using namespace std;

#pragma pack(push,1)
struct packed
{
    int a;
    short b;
};
#pragma pack(pop)

struct padded
{
    int a;
    short b;
};

main()
{
    cout << "sizeof(packed)=" << sizeof(packed) << endl;
    cout << "sizeof(padded)=" << sizeof(padded) << endl;
    return 0;
}
Output:
sizeof(packed)=6
sizeof(padded)=8
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TCP Packet Size ajkannan83 General 3 11-03-2005 06:46 AM
df returning wrong disk size delta7 Debian 4 08-20-2004 04:10 PM
tcp packet size dellcom1800 Linux - Networking 2 07-28-2004 07:49 AM
cdrecord -scanbus is returning the wrong drive rdluce Linux - Newbie 3 05-28-2004 07:55 PM
sizeof() return wrong size of structure?? Hammo Programming 7 02-21-2004 04:08 PM


All times are GMT -5. The time now is 09:12 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration