Problem with struture size

This is a discussion on Problem with struture size within the C Programming forums, part of the General Programming Boards category; My problem: this structure that was suposed to be 10 bytes big, is in fact 12... At least that's what ...

  1. #1
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722

    Question Problem with struture size

    My problem: this structure that was suposed to be 10 bytes big, is in fact 12... At least that's what the printf tells... I really need to resize the structure to exactly 10 bytes. The problem is that in this case 3 + 2 + 1 + 4 = 12.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    typedef struct{
    	char	tagid  [ 3];	//0-2  TAG identifier. It contains of string "ID3" 
    	short	tagver :16 ;	//3-4  TAG version. Can be eg. 03 00 
    	char	flags  : 8 ;	//5		Flags 
    	long	size   :32 ;	//6-9  Size of TAG
    } MP3ID3TAG2;
    
    main(){
    	printf("?: %d\n", sizeof(MP3ID3TAG2));
    	getch();
    }
    What's going on here??
    P.S.: I tried both Visual C++ and GCC, they both do that same thing.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    well if you really need it to be 10 bytes change the long to a short

    [edit]whoops just looked that will mess you up but that would be the only way to size it down[/edit]
    Last edited by prog-bman; 06-18-2004 at 01:01 AM.
    Woop?

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    If you add the sizes well: 3 bytes from the id string, 1 byte from flags, 2 bytes from tag version, and 4 bytes from size, all added up does 10 bytes. But the printf presents a 12 byte size. Plus when doing freads from file data 12 bytes are placed inside the structure but I can't have access those extra 2. I did something raw like
    Code:
    	MP3ID3TAG2 tag;
    	//read file data to structure
    	char *s = (char*)&tag;
    	for(i=0;i<sizeof(MP3ID3TAG2);i++)
    		printf("%2x", s[i]);
    Worked fine. The 12 bytes were there, but that's not the point. The problem is the compiler is assuming I have a 12 byte structure, but I only want 10, forcefully.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's being aligned on word boundaries. In other words, it's being padded by your compiler.

    On an aside, you are only supposed to use the bitfield specifier on integers. That is to say, "unsigned", "unsigned int", "signed int", or just "int". Anything else is undefined. C99 allows bit fields of type _Bool also.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by quzah
    It's being aligned on word boundaries. In other words, it's being padded by your compiler.
    Yes I noticed something like that... So how can I undo that, or prevent it?? Are there any pre-compiler directives I may use??

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read a bit on bitfields. You may also want to look here at the #pragma pack directive.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Bah, pragma pack didn't worked.... but I choped the size field in 2 words, and worked fine. Thanks anyway.
    Cheers

  8. #8
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by xErath
    <...> this structure that was suposed to be 10 bytes big, is in fact 12. <...> I really need to resize the structure to exactly 10 bytes. The problem is that in this case 3 + 2 + 1 + 4 = 12.
    This is a design error. You should never write code based on such an assumption, because there is no way in C to satisfy it in a portable way.

    The good way is to work 'by hand' bytes by bytes with an array of unsigned char, which exactly what a stream is.

    However, the structure can be useful internally, but never as an interface with a stream or the like.

    Note that the padding bytes are not the only problem. You also must consider endianness with objects bigger than one byte holding a numerical value (int, double etc.).
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    2
    the reason will be word allignnent and padding problem.

    if you deffine structure like this it will do the job i.e 10 bytes

    typedef struct{
    char tagid [ 3]; //0-2 TAG identifier. It contains of string "ID3"
    char flags : 8 ; //5 Flags
    short tagver :16 ; //3-4 TAG version. Can be eg. 03 00
    long size :32 ; //6-9 Size of TAG
    } MP3ID3TAG2;

    regards

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Bleh - see my reply to your other thread on the same subject
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. file size problem
    By arjunajay in forum C++ Programming
    Replies: 10
    Last Post: 08-03-2005, 05:26 AM
  3. problem with const int inside class (c2258)
    By talz13 in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 06:34 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 07:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21