Structures and sizeof

R

Ronald A. Andersen

********** header ****************
struct articleFile2
{
char CR;
int intStampDate;

};
*********** source ***************

struct articleFile2 *ptrArticle2 = NULL;


ptrArticle2 = (articleFile2 *)malloc(sizeof(articleFile2));
memset(ptrArticle2, 0, sizeof(articleFile2));
printf("sizeof(struct articleFile2) %d\n", sizeof(struct
articleFile2));

************ Questions************

1) The sizeof function returns 8. Why didn't return 5(i.e. an int is
four bytes and a char is one)?
 
R

Richard Heathfield

Ronald said:
********** header ****************
struct articleFile2
{
char CR;
int intStampDate;

};
*********** source ***************

struct articleFile2 *ptrArticle2 = NULL;


ptrArticle2 = (articleFile2 *)malloc(sizeof(articleFile2));

Don't you find

ptrArticle2 = malloc(sizeof *ptrArticle2)

easier to read?
memset(ptrArticle2, 0, sizeof(articleFile2));

Before you do that, check that malloc didn't return NULL.
printf("sizeof(struct articleFile2) %d\n", sizeof(struct
articleFile2));

Since sizeof returns size_t, not int, and since you can't portably tell what
type size_t is except that it's some kind of unsigned integer type, I
recommend that you cast here:

printf("sizeof(struct articleFile2) %d\n", (int)sizeof(struct
articleFile2));
************ Questions************

1) The sizeof function returns 8. Why didn't return 5(i.e. an int is
four bytes and a char is one)?

Any implementation is allowed to insert "padding bytes" after any struct
member. In your case, the implementation appears to be inserting three
padding bytes after the char, so that the next field, an int, lines up
nicely on an appropriate address boundary. This can give you efficiency
gains.
 
Z

Zoran Cutura

Ronald A. Andersen said:
********** header ****************
struct articleFile2
{
char CR;
int intStampDate;

};
*********** source ***************

struct articleFile2 *ptrArticle2 = NULL;


ptrArticle2 = (articleFile2 *)malloc(sizeof(articleFile2));

First of all, there is no need to cast the return value of malloc.
malloc returns a so called generic pointer which can be assigned to any
object pointer type without applying a cast.

Second, articleFile2 is not a type (unless you are compiling with a C++
compiler, which suggests that you learn C++ and post to a C++
newsgroup). The type you meant to use in you sizeof expression is
"struct articeFile2". But than I'd suggest to write this as

ptrArticle2 = malloc(sizeof *ptrArticle2);

where sizeof is applied to an object rather than a type (that is also
the reason we can lose the parentheses).
memset(ptrArticle2, 0, sizeof(articleFile2));

as above apply sizeof to the object rather than its type.
printf("sizeof(struct articleFile2) %d\n", sizeof(struct
articleFile2));

************ Questions************

1) The sizeof function returns 8. Why didn't return 5(i.e. an int is
four bytes and a char is one)?

For one, sizeof isn't a function its a operator and it is usually
applicable at compile time, so that it normally does not consume any
processing time when running the program.

To answer your question. Some platforms require memory accesses to be
aligned. That usually means ints can only be accessed at addresses
that are multiples of sizeof(int) and doubles can only be accessed at
addresses that are multiples of sizeof(double). So your compiler will
try its best to achieve that alignment requirements of your system are
met. The standard allows it to insert so called padding bytes between
two members (or at the end of a struct, but not at the beginning) and
that exactly is the reason why you see sizeof evaluate to 8 rather than
5 on your system.
 

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

No members online now.

Forum statistics

Threads
474,124
Messages
2,570,747
Members
47,301
Latest member
SusannaCgx

Latest Threads

Top