how to find the size of a private structure 
Author Message
 how to find the size of a private structure

hi all,
Is there any way to find the size of a structure from its pointer.
The structure is not visible. It is a private one.

warm regrads,
aneesh



Fri, 25 Mar 2005 17:49:15 GMT  
 how to find the size of a private structure

Quote:
> hi all,
> Is there any way to find the size of a structure from its pointer.
> The structure is not visible. It is a private one.

It's very much possible, providing the structure definition is in
scope.

struct foobar *f;
printf("size of struct is %d\n", sizeof *f);
/* or */
printf("size of struct is %d\n", sizeof(struct foobar));

Otherwise, if there is no definition in scope, finding out the size at
compile-time is impossible.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"We're women. We've got double standards to live up to."
   - Ally McBeal



Fri, 25 Mar 2005 17:54:31 GMT  
 how to find the size of a private structure


Quote:

>> hi all,
>> Is there any way to find the size of a structure from its pointer.
>> The structure is not visible. It is a private one.

>It's very much possible, providing the structure definition is in
>scope.

Except it sounds like he's talking about run-time, and possibly not
knowing which structure has been passed.  The only way it can be done at
run-time is if the structure itself contains that information, and at a
fixed offset for all structures (ideally 0).

--
#include <standard.disclaimer>
 _
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up
Per the FCA, this email address may not be added to any commercial mail list



Sat, 26 Mar 2005 04:31:43 GMT  
 how to find the size of a private structure

Quote:

> Is there any way to find the size of a structure from its pointer.
> The structure is not visible. It is a private one.

If the structure definition is not visible, then you cannot find
out its size by anything built into C.  You will have to
reconsider your approach or find something related to whatever
library you're using.

What are you really trying to do?
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield



Sat, 26 Mar 2005 14:18:47 GMT  
 how to find the size of a private structure
On Mon, 7 Oct 2002 09:54:31 UTC, Joona I Palaste

Quote:


> > hi all,
> > Is there any way to find the size of a structure from its pointer.
> > The structure is not visible. It is a private one.

> It's very much possible, providing the structure definition is in
> scope.

> struct foobar *f;

That's an incomplete definition: no size is known! */

Quote:
> printf("size of struct is %d\n", sizeof *f);

Requires compiler diagnostic, becaue only an incloplete type is known.

Quote:
> /* or */
> printf("size of struct is %d\n", sizeof(struct foobar));

same as above

Quote:
> Otherwise, if there is no definition in scope, finding out the size at
> compile-time is impossible.

The complete type must be known. An incompplete definition is not
enough.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de   eComStation Reseller in Germany



Sat, 26 Mar 2005 17:45:03 GMT  
 how to find the size of a private structure

Quote:

>hi all,
>Is there any way to find the size of a structure from its pointer.
>The structure is not visible. It is a private one.

If you have a pointer to the structure, with the correct type (as opposed
to a void pointer that points at the structure), then you can find out,
because the compiler has to know: "sizeof *p" should do it, or you could
fool around with finding the difference in bytes between p and (p + 1),
because the compiler will scale the "+ 1" by the appropriate number of
bytes; depends on what you want to do with this information.

However, if this works, then a definition of the struct must be visible
to the compiler; and thus, given a pointer to the instance of the struct
in question, you can access all its fields.  This is not exactly what
I'd call "private" or "not visible"!

If, instead of a typed pointer to the given struct, what you have is
a void pointer that points to this (unknown) struct, then you can't
find out the size (directly), because the compiler doesn't know.  But
if the compiler doesn't know the definition of the struct, you're not
going to be able to do much with it anyway.  I guess you could still
blindly copy it, or (bitwise) zero the data, etc., given just the size.  
But hopefully, you refer to the first situation.

--Ben

--



Sun, 27 Mar 2005 02:26:09 GMT  
 how to find the size of a private structure

Quote:



>>hi all,
>>Is there any way to find the size of a structure from its pointer.
>>The structure is not visible. It is a private one.

> If you have a pointer to the structure, with the correct type (as opposed
> to a void pointer that points at the structure), then you can find out,
> because the compiler has to know: "sizeof *p" should do it, or you could
> fool around with finding the difference in bytes between p and (p + 1),
> because the compiler will scale the "+ 1" by the appropriate number of
> bytes; depends on what you want to do with this information.

This only applies if the type is complete.  If only an incomplete type
is in scope, sizeof and pointer arithmetic aren't allowed.  I only
mention it because you seem to have discounted this case, but it's
likely what the OP means by a private structure.

Quote:
> However, if this works, then a definition of the struct must be visible
> to the compiler; and thus, given a pointer to the instance of the struct
> in question, you can access all its fields.  This is not exactly what
> I'd call "private" or "not visible"!

> If, instead of a typed pointer to the given struct, what you have is
> a void pointer that points to this (unknown) struct, then you can't
> find out the size (directly), because the compiler doesn't know.  But
> if the compiler doesn't know the definition of the struct, you're not
> going to be able to do much with it anyway.  I guess you could still
> blindly copy it, or (bitwise) zero the data, etc., given just the size.  
> But hopefully, you refer to the first situation.

An pointer to an incomplete structure type is still a typed pointer.
Also, another example of what you could do with such a pointer is pass
it to functions in another translation unit (that does have a complete
type for the struct in scope).

        - Kevin.



Sun, 27 Mar 2005 15:00:43 GMT  
 how to find the size of a private structure


Quote:



>>>hi all,
>>>Is there any way to find the size of a structure from its pointer.
>>>The structure is not visible. It is a private one.

>> If you have a pointer to the structure, with the correct type (as opposed
>> to a void pointer that points at the structure), then you can find out,
>> because the compiler has to know: "sizeof *p" should do it, or you could
>> fool around with finding the difference in bytes between p and (p + 1),
>> because the compiler will scale the "+ 1" by the appropriate number of
>> bytes; depends on what you want to do with this information.

>This only applies if the type is complete.  If only an incomplete type
>is in scope, sizeof and pointer arithmetic aren't allowed.  I only
>mention it because you seem to have discounted this case, but it's
>likely what the OP means by a private structure.

Yes, good point.  I sort of lumped this case in with "void pointer",
as in "not enough information for compiler to do pointer arithmetic",
but as you point out below...

- Show quoted text -

Quote:
>> However, if this works, then a definition of the struct must be visible
>> to the compiler; and thus, given a pointer to the instance of the struct
>> in question, you can access all its fields.  This is not exactly what
>> I'd call "private" or "not visible"!

>> If, instead of a typed pointer to the given struct, what you have is
>> a void pointer that points to this (unknown) struct, then you can't
>> find out the size (directly), because the compiler doesn't know.  But
>> if the compiler doesn't know the definition of the struct, you're not
>> going to be able to do much with it anyway.  I guess you could still
>> blindly copy it, or (bitwise) zero the data, etc., given just the size.  
>> But hopefully, you refer to the first situation.

>An pointer to an incomplete structure type is still a typed pointer.
>Also, another example of what you could do with such a pointer is pass
>it to functions in another translation unit (that does have a complete
>type for the struct in scope).

...it is a materially different case (one would have to use casts to
do the same with a void *).

--Ben

--



Mon, 28 Mar 2005 05:29:57 GMT  
 how to find the size of a private structure

Quote:



>>An pointer to an incomplete structure type is still a typed pointer.
>>Also, another example of what you could do with such a pointer is pass
>>it to functions in another translation unit (that does have a complete
>>type for the struct in scope).

> [...] (one would have to use casts to do the same with a void *).

No, one wouldn't.  It is rarely useful and never absolutely necessary
to cast void pointers in C.

Jeremy.



Mon, 28 Mar 2005 05:58:14 GMT  
 how to find the size of a private structure


Quote:



>>>An pointer to an incomplete structure type is still a typed pointer.
>>>Also, another example of what you could do with such a pointer is pass
>>>it to functions in another translation unit (that does have a complete
>>>type for the struct in scope).

>> [...] (one would have to use casts to do the same with a void *).

>No, one wouldn't.  It is rarely useful and never absolutely necessary
>to cast void pointers in C.

You are right: I should say, one would have to convert the void pointer
to the correct type, which one could do with a cast, or with assignment
(is there any other way I'm overlooking)?

--Ben

--



Mon, 28 Mar 2005 06:16:04 GMT  
 how to find the size of a private structure

Quote:




> >>An pointer to an incomplete structure type is still a typed pointer.
> >>Also, another example of what you could do with such a pointer is pass
> >>it to functions in another translation unit (that does have a complete
> >>type for the struct in scope).

> > [...] (one would have to use casts to do the same with a void *).

> No, one wouldn't.  It is rarely useful and never absolutely necessary
> to cast void pointers in C.

    Contrived counter-example:

        void foo(int, ...) {
            /* variadic arguments should all be `double*' */
            ...
        }
        int main(void) {
            foo (1, malloc(sizeof(double));  /* bzzzt! */
            ...
        }

However, I'd have to admit that the best fix isn't to write
`foo(1, (double*)malloc(sizeof(double))', but to find the
code's author and encourage him or her to consider a career
change.  Bungee jumping in active volcanoes, for preference.

--



Mon, 28 Mar 2005 06:22:54 GMT  
 how to find the size of a private structure

Quote:


> > No, one wouldn't.  It is rarely useful and never absolutely necessary
> > to cast void pointers in C.

>     Contrived counter-example:

>    void foo(int, ...) {
>        /* variadic arguments should all be `double*' */
>        ...
>    }
>    int main(void) {
>        foo (1, malloc(sizeof(double));  /* bzzzt! */

You can avoid a cast by writing this as
        double *d = malloc (sizeof *d);
        foo (1, d);
See, no cast is necessary.

Quote:
>        ...
>    }

--
"Am I missing something?"
--Dan Pop


Mon, 28 Mar 2005 07:04:38 GMT  
 how to find the size of a private structure

Quote:


> > It is rarely useful and never absolutely necessary to cast void
> > pointers in C.

>      Contrived counter-example:

   <variadic function expecting pointers-to-double>

Right: that's exactly why I said 'rarely', rather than 'never' useful.
AFAICS you can always use a temporary to avoid the cast, though.

FWIW, I was thinking of Unix's execl() function, which has a similar
interface (variable number of pointers to char) but uses a null
pointer to signal the last argument rather than passing in the
argument count separately.

Quote:
>  However, I'd have to admit that the best fix isn't to write
>  `foo(1, (double*)malloc(sizeof(double))', but to find the
>  code's author and encourage him or her to consider a career
>  change.

No argument here.

Jeremy.



Mon, 28 Mar 2005 07:15:58 GMT  
 how to find the size of a private structure


Quote:
>On Mon, 7 Oct 2002 09:54:31 UTC, Joona I Palaste


>> > hi all,
>> > Is there any way to find the size of a structure from its pointer.
>> > The structure is not visible. It is a private one.

>> It's very much possible, providing the structure definition is in
>> scope.

>> struct foobar *f;

>That's an incomplete definition: no size is known! */

He specifically said "providing the structure definition is in scope."

-Kevin



Mon, 28 Mar 2005 10:11:10 GMT  
 how to find the size of a private structure

Quote:





>>>>An pointer to an incomplete structure type is still a typed pointer.
>>>>Also, another example of what you could do with such a pointer is pass
>>>>it to functions in another translation unit (that does have a complete
>>>>type for the struct in scope).

>>> [...] (one would have to use casts to do the same with a void *).

>>No, one wouldn't.  It is rarely useful and never absolutely necessary
>>to cast void pointers in C.

>You are right: I should say, one would have to convert the void pointer
>to the correct type, which one could do with a cast,

That's what I thought you meant the first time. There are times when a void
pointer has to be cast to some type (determined in some fashion) in order to use
it.

I work with a large (and old) system which uses a structure to define an item on
a screen. The actual data to be displayed is represented in the struct by "void
* data". Elsewhere in the struct is a variable which tells whether the data is a
char array, integer, float, an index into a table of strings, etc. Sometimes it
is necessary to do pointer arithmetic on the passed data pointer, so we can't
leave it as void *. Sometimes the pointer is assigned, but in simple cases, it
can be cast to (for example) char * without introducing another variable.

I'm not claiming that the design was the best, but it exists, and it's legal
afaik.

Quote:
> or with assignment
>(is there any other way I'm overlooking)?

>--Ben

Al Balmer
Balmer Consulting


Mon, 28 Mar 2005 13:11:01 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Find size of variable size structure?

2. Finding the Size of a Structure.

3. private structure declaration

4. how to copy a structure of record to another structure with type,size in the menber of structure...

5. event for Size structure

6. Structures of indefinite size

7. Structure problem - Size not known???

8. binary file and different size of structures

9. zero sized structures

10. Help Me !! some strange in a structure size

11. Size of structure containing char fields

12. size of structure

 

 
Powered by phpBB® Forum Software