problem with size of

S

seema

Hi all,
I am new to C programming. I have doubt and I want to clarify here is
the program ,

#include <stdio.h>

struct windows
{
int i;
char me;
};
int main(){
struct windows xp;
printf ("%d ",sizeof(xp));

}

I am using Linux. On Linux above program prints 8. Basically sizeof
(int) is 4 and sizeof(char) is 1. Its supposed to print 5 right? Can
some body explain why it prints 8??

Thanks in advance,
Seema Rao
 
C

Captain Winston

seema said:
Hi all,
I am new to C programming. I have doubt and I want to clarify here is
the program ,

#include <stdio.h>

struct windows
{
int i;
char me;
};
int main(){
struct windows xp;
printf ("%d ",sizeof(xp));

}

I am using Linux. On Linux above program prints 8. Basically sizeof
(int) is 4 and sizeof(char) is 1. Its supposed to print 5 right? Can
some body explain why it prints 8??

Alignment in structure, google it in this group, many many
explainations.
 
V

Vladimir Oka

seema said:
Hi all,
I am new to C programming. I have doubt and I want to clarify here is
the program ,

#include <stdio.h>

struct windows
{
int i;
char me;
};
int main(){
struct windows xp;
printf ("%d ",sizeof(xp));

}

I am using Linux. On Linux above program prints 8. Basically sizeof
(int) is 4 and sizeof(char) is 1. Its supposed to print 5 right? Can
some body explain why it prints 8??

Padding (adding dummy fields to your structs).

Compiler is allowed to ad padding to structs. Usually it's done to make
accesses more efficient. The Standard does not allow you to know
whether padding is in effect or not. However, your particular compiler
may document the way it does it, and may provide (non-portable) ways of
controlling padding.
 
I

Ico

seema said:
Hi all,
I am new to C programming. I have doubt and I want to clarify here is
the program ,

#include <stdio.h>

struct windows
{
int i;
char me;
};
int main(){
struct windows xp;
printf ("%d ",sizeof(xp));

}

I am using Linux. On Linux above program prints 8. Basically sizeof
(int) is 4 and sizeof(char) is 1. Its supposed to print 5 right? Can
some body explain why it prints 8??

This is a faq, Question 2.13: Why does sizeof report a larger size than
I expect for a structure type, as if there were padding at the end?

Padding at the end of a structure may be necessary to preserve alignment
when an array of contiguous structures is allocated. Even when the
structure is not part of an array, the padding remains, so that sizeof
can always return a consistent size. See also question 2.12.
 
B

Barry Schwarz

Hi all,
I am new to C programming. I have doubt and I want to clarify here is
the program ,

#include <stdio.h>

struct windows
{
int i;
char me;
};
int main(){
struct windows xp;
printf ("%d ",sizeof(xp));

}

In addition to the other advice you have received, sizeof evaluates to
a size_t which is unsigned but need not be int. If you want to print
it, cast the expression to an known unsigned type (unsigned long seems
a popular choice) and use the correct conversion specification (e.g.,
lu).
I am using Linux. On Linux above program prints 8. Basically sizeof
(int) is 4 and sizeof(char) is 1. Its supposed to print 5 right? Can
some body explain why it prints 8??

Thanks in advance,
Seema Rao


Remove del for email
 
N

newbie

Hi,

I read about this before. The reason why a struct with an int and a
char output 8 is because of the padding. The reason of the padding is
for the machine byte size. It will use up as big as the machine byte
size which in this case will be 8.

Hope thats helps,

Newbie
 
F

Flash Gordon

newbie said:
Hi,

I read about this before.

Read about what before? Please provide context when posting. Usenet is
not Google and it is quite possible that people can't conveniently see
the post you are replying to.
> The reason why a struct with an int and a
char output 8 is because of the padding. The reason of the padding is
for the machine byte size. It will use up as big as the machine byte
size which in this case will be 8.

Hope thats helps,

I doubt it will since you are talking almost complete rubbish. Byte
sizes have nothing to do with it, you could have padding with a byte of
16 bits and an int of 32 bits (2 bytes) but not have padding on a
machine with a char of 8 bits and an int of 32 bits (4 bytes). Padding
is for alignment reasons, and alignment is generally related to the size
of a word and has nothing in general to do with the size of a byte (a
byte always being 1 byte in size).
 
B

Barry Schwarz

Hi,

I read about this before. The reason why a struct with an int and a
char output 8 is because of the padding. The reason of the padding is
for the machine byte size. It will use up as big as the machine byte
size which in this case will be 8.
Not even close.

Most machines require an int to be on a four-byte boundary. If you
have an array of such struct, each element must on a boundary. The
difference between the addresses of two adjacent elements is always
equal to the size of the elements. Therefore, the size of such a
struct must be a multiple of 4. Since the struct must occupy at least
five bytes, the smallest size that allows this is eight.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top