473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Structures and sizeof

********** 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)?
Nov 13 '05 #1
2 4246
Ronald A. Andersen wrote:
********** 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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2
Ronald A. Andersen <ro*********@hotmail.com> wrote:
********** 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.
--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
6
by: Everton da Silva Marques | last post by:
Hi, I need to allocate, using malloc(), a single memory block to hold two structures and a variable length string. Is it safe (portable, alignment-wise) to sum up the sizeof's of those...
6
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.