Re: structures, structures and more structures (questions about nested structures)
- From: "Mike Wahler" <mkwahler@xxxxxxxxxxxx>
- Date: Sat, 24 Sep 2005 02:32:40 GMT
"Alfonso Morra" <sweet-science@xxxxxxxxxxxx> wrote in message
news:dh2d7e$g16$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
>
> Mike Wahler wrote:
>
>> "Alfonso Morra" <sweet-science@xxxxxxxxxxxx> wrote in message
>> news:dh29hu$o4i$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>>
>>>Hi,
>>>
>>>I have the ff data types :
>>>
>>>typedef enum {
>>>VAL_LONG ,
>>>VAL_DOUBLE ,
>>>VAL_STRING ,
>>>VAL_DATASET }ValueTypeEnum ;
>>>
>>>typedef union {
>>>long lval ;
>>>double fval ;
>>>char* sval ;
>>>void* ptr ;
>>>} Value ;
>>>
>>>typedef struct {
>>>int magic ;
>>>int version ;
>>>}Header ;
>>>
>>>typedef struct {
>>>char label[20] ;
>>>id int ;
>>>}Key ;
>>>
>>>typedef struct {
>>>Header *hdr ;
>>>char *subject ;
>>>int subject_len ;
>>>Key key ;
>>>ValueTypeEnum type ;
>>>Value value ;
>>>int text_len ;
>>>int size ;
>>>}MotherStruct ;
>>>
>>>
>>>If I have a variable declared as ff:
>>>
>>>MotherStruct *pMS = calloc(1,sizeof(MotherStruct*)) ;
>>>
>>>1). Do I have to allocate memory seperately for each individual nested
>>>pointer in the structure (i.e. hdr and subject?).
>>
>>
>> Yes, if you want those pointers to point to objects of
>> those types.
>>
>>
>>> I guess the answer is yes - but I just need to be sure (see question 3
>>> below).
>>>
>>>2). How do I calculate the size (in bytes) of the structure MotherStruct
>>
>>
>> sizeof(MotherStruct)
>
>
> This is the only answer that suprised me (in fact I don't believe its the
> correct answer).
Yes, it is correct. It will return the number of bytes used
by a type 'MotherStruct' object. This size includes the size
of all members, including pointers. What those pointers point
to (if anything) is not part of the 'MotherStruct' object.
> I used to think this was the correct answer.
It is.
>But if you think about it, it can't be (at least, I can't see how it can
>be). How can sizeof know how much memory you have allocated for the char*
>(for example)?.
It doesn't. That memory is not part of the 'MotherStruct' object,
only the pointer is.
>The number returned by sizeof is independent of the size of the string that
>has been allocated for the member subject
Yes, because that memory is not part of MotherStruct.
>- so I think sizeof can be used for a simple structure with basic data
>types -
Yes. Pointers are such types. Again, what they point to is not
part of the structure.
> whenever you have nested pointers, all bets are off.
Nope.
>I think the correct way of doing this is to navigate the structure and sum
>up (sizeof(data_type) x number of data_type) all the way through all the
>members of the struct and any nested pointers - that makes sense from a
>logical point of view - but it would be great if someone could either
>confirm it or point me to a reference that shows me why my thinking is
>wrong.
Perhaps you asked the wrong question. :-) Yes, if you want to calculate
the size of all memory (including allocated) *associated* with MotherStruct,
that's how to do it.
Your question was:
2). How do I calculate the size (in bytes) of the structure MotherStruct
The correct answer is what I gave:
sizeof(MotherStruct).
>
> But thanks for the other two answers.
You're welcome.
-Mike
.
- Follow-Ups:
- References:
- structures, structures and more structures (questions about nested structures)
- From: Alfonso Morra
- Re: structures, structures and more structures (questions about nested structures)
- From: Alfonso Morra
- structures, structures and more structures (questions about nested structures)
- Prev by Date: Re: English interpretation of this code?
- Next by Date: Re: typedef
- Previous by thread: Re: structures, structures and more structures (questions about nested structures)
- Next by thread: Re: structures, structures and more structures (questions about nested structures)
- Index(es):
Relevant Pages
|