Jump to content

What is the size of this C structure

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
What is the size of the C structure below on a 32-bit system? On a 64-bit?

struct foo {

        char a;

        char* b;

};

Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#2
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts
You can use C's/C++'s sizeof() operator to calculate the size of any variable, structure etc. in bytes.


struct foo

{

   char a;

   char *b;

};


int nSize = sizeof(foo);

printf("Size of foo is %i bytes!\n", nSize);


Greets,
artificial
Sometimes words ain't enough to express something. That's why computer scientists use double words.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It depends somewhat on both the compiler and the OS.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
So, it's not as straightforward as 3 bytes (1 for char and 2 for char*) for 16bit and 5 bytes (1 for char and 4 for char*) for 32bit?
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#5
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Nope. Probably the most annoying part about C is that there are so many things that are implementation dependent. Heck, the number of bits in a byte can't even be assumed in the C language.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
some compilers such as Microsoft has a packing pragma that let you change how the compiler aligns the data in the structure or c++ class.

#pragma pack(1) // byte align the data

struct foo

{

   char a;

   char *b;

};

With byte alignment the compiler does not put holes in the structure, so sizeof(struct foo) will be 5


#pragma pack(2) // word align the data

struct foo

{

   char a;

   char *b;

};

With word alignment the compiler will align the pointer in an even byte boundry (or address) and sizeof(struct foo) will be 6.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
I see.. Does that mean it can be 3 or 4 bytes in 16bit and 5 or 6 bytes in 32bit OS?
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

Roger said:

So, it's not as straightforward as 3 bytes (1 for char and 2 for char*) for 16bit and 5 bytes (1 for char and 4 for char*) for 32bit?

More or less (pointer wise), on 16, 32 and 64-bit arches, pointer sizes are 2, 4 and 8 respectively.

It's not safe to assume padding, but natural padding of a char resulting in 4 octets will often be the case, yes. For x86, fields are usually 32-bit aligned. The reason for this is to increase the system's performance at the cost of memory usage.

Similarly, for x64, fields are usually 64-bit/8-byte aligned, so sizeof(foo) would be 16.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
So, if x86:

4 bytes for 16-bit OS
8 bytes for 32-bit OS
16 bytes for 64-bit OS

??
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

Roger said:

So, if x86:

x86 refers to the 8086 family of instruction set architectures, but you would imply 32-bit if you would say just "x86". x86-16 implies 16-bit and x64 implies x86-64.

As for the rest of your quote, pointers are 16 bits in x86-16, so you'd be correct to assume it would be what you wrote.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
Okay, thanks!
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users