struct foo {
char a;
char* b;
};
10 replies to this topic
#1
Posted 22 August 2010 - 12:53 PM
What is the size of the C structure below on a 32-bit system? On a 64-bit?
Check out our update Guidelines/FAQ. When posting code, remember to use code tags -
.
.
|
|
|
#2
Posted 22 August 2010 - 02:09 PM
You can use C's/C++'s sizeof() operator to calculate the size of any variable, structure etc. in bytes.
Greets,
artificial
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
Posted 22 August 2010 - 04:23 PM
It depends somewhat on both the compiler and the OS.
#4
Posted 22 August 2010 - 08:00 PM
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 -
.
.
#5
Posted 22 August 2010 - 08:22 PM
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)
Download the new operating system programming kit! (some assembly required)
#6
Posted 22 August 2010 - 08:35 PM
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
Posted 22 August 2010 - 09:28 PM
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 -
.
.
#8
Posted 23 August 2010 - 03:09 AM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#9
Posted 23 August 2010 - 08:37 AM
So, if x86:
4 bytes for 16-bit OS
8 bytes for 32-bit OS
16 bytes for 64-bit OS
??
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 -
.
.
#10
Posted 23 August 2010 - 07:53 PM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#11
Posted 23 August 2010 - 08:20 PM
Okay, thanks!
Check out our update Guidelines/FAQ. When posting code, remember to use code tags -
.
.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top










