LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


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

Rep: Reputation: 15
sizeof() function error ???


Hi

I am using the size of function to get the size of a struct

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-09-2005, 08:52 PM   #2
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quoting for K & R: Section 6.4 Pointers to Structures.

Quote:
Don't assume, however, that the size of a structure is the sum of the sizes of its members. Because of alignment requirements of different objects, there may be unnamed "holes" in a structure. Thus, for instance, if a char is one byte and an int four byte, the structure

Code:
structure {
               char c;
                int i;
               };
might well require eight bytes, not five. The sizeof operator returns the proper value.

Last edited by Dave Kelly; 03-09-2005 at 08:54 PM.
 
Old 03-09-2005, 09:03 PM   #3
Quest101
LQ Newbie
 
Registered: Nov 2004
Posts: 27

Original Poster
Rep: Reputation: 15
Hey Dave,
That pretty much settles that , thanx for the reference. Dont know if you would have a suggestion on something else i could use to store the values ??

The struct is actual the header for packets that i am sending , i store the header/struct in a buffer which contains the payload and send it off as a packet using winsock.
 
Old 03-09-2005, 09:23 PM   #4
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Store which values?
 
Old 03-09-2005, 09:34 PM   #5
Quest101
LQ Newbie
 
Registered: Nov 2004
Posts: 27

Original Poster
Rep: Reputation: 15
the header structure is suppose to store two values,

u_int32_t timestamp and
u_int16_t sequencenumber
 
Old 03-09-2005, 10:16 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Is 2 bytes really so painful? I mean, it would take more than 500,000 instances of the struct just to take up 1MB. What is it that you're using this for?

If size is really an issue you could do something like:
Code:
char *header = malloc(sizeof(u_int32_t) + sizeof(u_int16_t));
*(long long *)header = timestamp << 16;
*(long long *)header |= sequencenumber & 0x1F;
Now you've got both numbers in a single 6-byte buffer.

Last edited by itsme86; 03-09-2005 at 10:22 PM.
 
Old 03-09-2005, 10:23 PM   #7
Quest101
LQ Newbie
 
Registered: Nov 2004
Posts: 27

Original Poster
Rep: Reputation: 15
Smile

Hey ,
under normal situations i would be thinking the exact samething , why the big deal over 2 Bytes

however this project is about packet header compression , the the smaller i get the the header the , the more successfull my header compression will be .

thanx alot for the sample code , seems like it should work , I will try and implement it now

Regards
 
Old 03-10-2005, 09:28 PM   #8
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
The word 'packet' has creeped into the conversation. That could make this a different animal.

Does the original assignment require you to use a structure?
 
Old 03-10-2005, 09:34 PM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
If you can depend on gcc extensions, you could use gcc's packed attribute. example:
Code:
#include <stdio.h>
#include <stdint.h>

struct foo {
        uint32_t a;
        uint16_t b;
};

struct bar {
        uint32_t a;
        uint16_t b;
} __attribute((packed))__;

int main()
{
        printf("%u %u\n", sizeof(struct foo), sizeof (struct bar));
        return 0;
}
This outputs "8 6".
 
Old 03-10-2005, 09:35 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
but if you do that, be very careful about performing operations on members of the struct. They may fail with something like "bus error", IIRC.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Configure: error: cannot compute sizeof (long double) in glibc pass 2 irfanali Linux From Scratch 29 02-03-2010 06:48 AM
error when installing gift: error: cannot compute sizeof (short), 77 v1pEr Linux - Software 5 11-04-2004 02:43 AM
error recursive function in c shams Programming 3 07-28-2004 08:44 PM
error coding a function in c shams Mandriva 1 07-28-2004 12:09 AM
Assembly/C - error: undefined reference to sizeof jrtayloriv Programming 9 07-13-2004 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration