size of a variable from a pointer

Discussion in 'C Programming' started by serrand, Jan 17, 2006.

  1. serrand

    serrand Guest

    Hello all,

    i wonder if we can know the size of a variable from a pointer :

    static void *my_free_hook (void *ptr, const void *caller)
    {
    size_t sz;
    /* bla bla bla */
    /* HOW CAN I DETERMINE SIZE OF *ptr ? */
    printf ("size of freed memory : %u", sz);
    }

    sizeof (ptr) is the size of a pointer (4 on my system)... and sizeof(*ptr) is the size of the first byte = 1 ...

    all idea welcome ...

    Xavier
    serrand, Jan 17, 2006
    #1
    1. Advertising

  2. serrand

    Artie Gold Guest

    serrand wrote:
    > Hello all,
    >
    > i wonder if we can know the size of a variable from a pointer :
    >
    > static void *my_free_hook (void *ptr, const void *caller)
    > {
    > size_t sz;
    > /* bla bla bla */
    > /* HOW CAN I DETERMINE SIZE OF *ptr ? */
    > printf ("size of freed memory : %u", sz);
    > }
    >
    > sizeof (ptr) is the size of a pointer (4 on my system)... and
    > sizeof(*ptr) is the size of the first byte = 1 ...
    >
    > all idea welcome ...
    >
    > Xavier


    All right. What are you *really* trying to do?
    ....(and you can't dereference a pointer to void (i.e. void *) in any case...

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Jan 17, 2006
    #2
    1. Advertising

  3. serrand

    Ian Collins Guest

    serrand wrote:
    > Hello all,
    >
    > i wonder if we can know the size of a variable from a pointer :
    >
    > static void *my_free_hook (void *ptr, const void *caller)
    > {
    > size_t sz;
    > /* bla bla bla */
    > /* HOW CAN I DETERMINE SIZE OF *ptr ? */
    > printf ("size of freed memory : %u", sz);
    > }
    >
    > sizeof (ptr) is the size of a pointer (4 on my system)... and
    > sizeof(*ptr) is the size of the first byte = 1 ...
    >
    > all idea welcome ...
    >


    You can't, you gave to pass the size.

    --
    Ian Collins.
    Ian Collins, Jan 17, 2006
    #3
  4. serrand

    pete Guest

    serrand wrote:
    >
    > Hello all,
    >
    > i wonder if we can know the size of a variable from a pointer :
    >
    > static void *my_free_hook (void *ptr, const void *caller)


    static void *my_free_hook (void *ptr, const void *caller, size_t sz);

    --
    pete
    pete, Jan 17, 2006
    #4
  5. serrand <> writes:
    > i wonder if we can know the size of a variable from a pointer :
    >
    > static void *my_free_hook (void *ptr, const void *caller)
    > {
    > size_t sz;
    > /* bla bla bla */
    > /* HOW CAN I DETERMINE SIZE OF *ptr ? */
    > printf ("size of freed memory : %u", sz);
    > }
    >
    > sizeof (ptr) is the size of a pointer (4 on my system)... and
    > sizeof(*ptr) is the size of the first byte = 1 ...


    sizeof(*ptr) is a constraint violation; it's equivalent to
    sizeof(void), and since void is an incomplete type, it has no size.
    (I think gcc allows this as an extension.)

    Given a void* pointer value, there's no direct way to determine the
    size of the object it points to. free() is able to determine the
    number of bytes that need to be released, but it does so in a manner
    that's not specified by the standard, and there's no portable way for
    you to get that information yourself.

    If you want to know this, you need to keep track of it yourself.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 17, 2006
    #5
  6. serrand

    serrand Guest

    pete wrote:
    > serrand wrote:
    >
    >>Hello all,
    >>
    >>i wonder if we can know the size of a variable from a pointer :
    >>
    >>static void *my_free_hook (void *ptr, const void *caller)

    >
    >
    > static void *my_free_hook (void *ptr, const void *caller, size_t sz);
    >


    in this case i can't modify the function call...
    what's more, principle is : I DO NOT KNOW the size : it's what i want ;-)

    i wanted to trace allocation and deallocation in order to check Sum(allocation) - Sum(deallocation) = 0 ...

    Ok... i can modify all my types in order to put size of instance in the first field... not pretty nice...

    thanks for the ideas...

    xavier
    serrand, Jan 17, 2006
    #6
  7. On Wed, 18 Jan 2006 00:07:51 +0100, in comp.lang.c , serrand
    <> wrote:

    >in this case i can't modify the function call...


    >what's more, principle is : I DO NOT KNOW the size : it's what i want ;-)


    theres no way to do this in C, unless your'e lucky enough to know that
    the pointer always points to memory terminated by a specific guard
    byte or series of bytes, eg one or more '\0'.

    Mark McIntyre
    --

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Jan 17, 2006
    #7
  8. serrand wrote:
    >
    > pete wrote:
    > > serrand wrote:
    > >
    > >>Hello all,
    > >>
    > >>i wonder if we can know the size of a variable from a pointer :
    > >>
    > >>static void *my_free_hook (void *ptr, const void *caller)

    > >
    > >
    > > static void *my_free_hook (void *ptr, const void *caller, size_t sz);
    > >

    >
    > in this case i can't modify the function call...
    > what's more, principle is : I DO NOT KNOW the size : it's what i want ;-)
    >
    > i wanted to trace allocation and deallocation in order to check
    > Sum(allocation) - Sum(deallocation) = 0 ...
    >
    > Ok... i can modify all my types in order to put size of instance in the
    > first field... not pretty nice...
    >
    > thanks for the ideas...


    You can modify your my_malloc_hook() function to save the size somewhere.
    Perhaps some not-very-portable-but-probably-will-work-on-your-systems
    method of malloc'ing sz+sizeof(size_t), storing the size at the start of
    the buffer, and returning ptr+sizeof(size_t). Then, my_free_hook() can
    look for the size at ((char *)ptr-sizeof(size_t)), and free that address.

    There is probably a portable solution you could come up with using the
    above as a starting point. (If you could guarantee proper alignment of
    the returned pointer, it might become well-defined behavior.)

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Jan 18, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    669
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    822
    Jordan Abel
    Mar 28, 2006
  3. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,517
    Michael Jung
    May 25, 2008
  4. Replies:
    4
    Views:
    1,225
    Fred Zwarts
    Jul 2, 2009
  5. theshubham

    size of char pointer or int pointer

    theshubham, Sep 10, 2012, in forum: C Programming
    Replies:
    0
    Views:
    302
    theshubham
    Sep 10, 2012
Loading...

Share This Page