Thread: baffled!

  1. #1
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    baffled!

    How would one get the size of pointed-to-data of a void pointer?
    Is this possible in C?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: baffled!

    Originally posted by The Dog
    How would one get the size of pointed-to-data of a void pointer?
    Is this possible in C?
    Through good and thoughtful programming on your part. You have to be careful when writing to ensure you cast the void* to a pointer of the correct type, or maybe you need just "remember" how many bytes the pointer is pointing to, depending on what you're doing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    void *v;
    int i

    v = &i;
    printf("%d", sizeof( (int*)v ) );

    Basicly you just use sizeof like normal. You, when using a void pointer, have to assume that whatever you're being told it actually points to, is in fact what it actually is.

    Think of a void pointer as a union. Everything in the union actually resides in the same block of memory. However, by choosing one member of the union, you are in essence dereferencing the chunk of memory as whatever you want to use it as.

    The same thing goes for void points. You dereference it as whatever you want to use it as. You just have to hope what you want to use it as, is actually what it really points to...

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    i just wanted if it was possible to get the size of pointed-to-data
    without having a clue of what it's pointing to.

    I realize now that it's just a matter of guessing, but then again,
    I doubt i'll take the chance.

    thanx anyways

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie baffled
    By Steve MacD in forum C Programming
    Replies: 8
    Last Post: 02-17-2005, 10:44 AM