7 Replies - 246 Views - Last Post: 24 August 2012 - 12:16 AM Rate Topic: -----

#1 camcamcam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-August 12

Structure and sizeof function

Posted 22 August 2012 - 05:59 PM

hi there,

in C a char var uses 1 byte in memory, and a int var uses 4 bytes in memory.
I have a structure with a char field and with a int field, so the sizeof function should return 5, but it returns 8.

#include <stdio.h>
#include <stdlib.h>

struct fileHeader {
       char tipo;
       int asdf;
};

int main()
{
    
    printf("%d", sizeof(struct fileHeader));
    
    printf("\n%d", sizeof(char));
    printf("\n%d", sizeof(int));
    
    /*
    char word[]="\x6d\x6f\x64\x65\x6d"; 
	   
    printf("%s\n", word);
    */
    
    system("pause");
    return 0;
}



There are 3 bytes that should not appear, what is going on?.

Thanks.


ps: i am using dev-c++.

Is This A Good Question/Topic? 0
  • +

Replies To: Structure and sizeof function

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: Structure and sizeof function

Posted 22 August 2012 - 06:11 PM

By default the compiler puts padding to make sure that the structure elements are double word aligned. The CPU works faster when data is aligned at these borders.

If you swap the order of the two members, you may discover that the size may be smaller.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Structure and sizeof function

Posted 22 August 2012 - 06:13 PM

sizeof(int) is not necessarily 4. This is implementation defined.

structs can be padded, which is also implementation defined.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: Structure and sizeof function

Posted 22 August 2012 - 06:13 PM

Depending on your compiler, you can override the default packing borders. For example, with the MSVC compiler #pragma pack is used to change the packing: http://msdn.microsof...(v=vs.100).aspx
Was This Post Helpful? 0
  • +
  • -

#5 camcamcam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-August 12

Re: Structure and sizeof function

Posted 22 August 2012 - 07:13 PM

that explains why the structure uses more space than the members on it, so when i write a structure into a file, i put "garbage" in the file.

Well i won't write structures into a file anymore, probably that is why i can't make a Bitmap fileheader correctly.

thanks a lot for the answers.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: Structure and sizeof function

Posted 22 August 2012 - 09:24 PM

From my quick searches, it looks like #pragma pack is also supported by GCC, so you don't have to abandon bitblting structures to and from files if you don't really want to. You just need to be aware of the packing issues.
Was This Post Helpful? 0
  • +
  • -

#7 camcamcam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-August 12

Re: Structure and sizeof function

Posted 23 August 2012 - 05:15 PM

thank you very much, i am reading about it, and i found the way to use #pragma pack to delete the padding between the members of a structure and now my structure has exactly the size of the members!.

here is a detailed information:
http://www.xbdev.net...s/bmp/index.php
http://tedlogan.com/techblog2.html

thanks.
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Structure and sizeof function

Posted 24 August 2012 - 12:16 AM

Structure packing should be regarded as a temporary fix.

1. http://en.wikipedia....wiki/Endianness
There is no pragma to fix endian issues. If your machine endian is different to the file format, then you're going to have to rearrange the bytes yourself.

2. Performance.
The reason structs have holes to begin with is to provide optimal access to data members. The logical inference of that is if the struct is packed, then member data access is sub-optimal. On architectures which don't allow unaligned memory accesses, the compiler has to generate extra code every time you access an unaligned member in the structure. If you're just doing I/O, then you're OK. But if this is in your performance sensitive code, you might want to think about what you're doing.

3. Data types
If you're trying to match up some external file format, then you should avoid raw types like 'int' or 'short', since these vary in size from one machine to another. Use the stdint.h types, or define your own typedefs if you want C89 portability as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1