Re: structures, structures and more structures (questions about nested structures)




"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


.



Relevant Pages

  • Re: Question about a struct declaration
    ... typedef struct dummy *dummy; ... If want to use the type as a pointer, then hiding the fact that it is a pointer in a typedef is indeed a pretty useless idea. ... A classic example would be the interface of 'pthreads' library, where 'pthread_t', 'pthread_mutex_t' etc. might easily be pointers. ... work in case when you need to self-refer to the struct type from inside ...
    (comp.lang.c)
  • Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
    ... Somehow I was thinking of pointers as being just pointers, with an otherwise nebulous definition, but yes, pointers are variables containing an address. ... Microvolt is a double, fyi. CTypes.h contains: ... typedef double Hertz; ... itmp = mxMalloc); ...
    (comp.soft-sys.matlab)
  • Re: Function Pointers
    ... If you feel the need for a generic pointer-to-function type, ... should probably create a typedef for it. ... Others have shown you how to typedef a function pointer type. ... I also prefer to see the "pointerlyness" of pointers, ...
    (comp.lang.c)
  • Re: Function Pointers
    ... is that it's better not to do this, because I don't like hiding pointers ... So I would typedef the function type itself: ... Pointers to functions are fundamentally different from pointers to ... object pointers behind a typedef is Evil, ...
    (comp.lang.c)
  • Re: Typdef pointers to structs or not?
    ... (This is straight from memory so it might not even compile.) ... The use case is AST representation which makes heavy use of pointers to ... structs ... modify these structs so either the whole typedef will be in a .h file ...
    (comp.lang.c)