Thread: Disadvantages of void pointers.

  1. #16
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by whiteflags View Post
    It should seem weird. It isn't normal to try and hack C's type system. But when you get down to how C really works, there isn't much stopping you from assigning to an int by pretending it's a structure.

    C99 requires that objects be represented by a contiguous sequence of bytes.


    Further down, the standard says:

    This makes it possible to do all sorts of wonderful things. For instance, any object can be punned into a sequence of unsigned chars -- (unsigned char*)&obj -- this is the reason that memcpy can work.



    Let's simplify shiroaisu's example for discussion:
    Code:
    struct T {
        int a;
    };
    
    void foo(void *pointer)
    {
        struct T *t = pointer;
        t->a = 10;
    }
    
    int main()
    {
        int x = 1;
        foo(&x);
    }
    Now, does the program exhibit undefined behavior or not? It does. Since the object representations of an integer and a struct T containing an integer do not have to be the same, there are a bunch of question marks around this. The program can appear to work correctly when the compiler decides that the way to represent the structure T is the same way to represent other integers. (After all, where exactly is the difference in bits.) However, it is also valid to allocate space for the integer and then some padding. If you're lucky, the computer would segfault in that case. A standards conforming compiler could try to access the padding, which would probably cause a crash.

    The bad thing about undefined behavior is that tons of programs invoking it appear to work. Crashes are a good thing, a sign something is probably a mistake.

    In the same vein, void pointers/casts are bad, for silently allowing things like this.
    Ok I understand that you can't have the control of the type of pointer. But I didn't understand why it produces Undefined Behavior(the above example) if it has matter. I just understand that using a void pointer it is type unsafe and it can produce Undefined behavior like this example.

  2. #17
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    @whiteflags If I have understood well there are implementations that store the values of x and a integers contiguously in the memory so that the program will work well but others that add padding after the x integer for example and when ask access there we get segm fault so undefined behavior? Did I get the general point?

    Sorry but I am not familiar with very underhood concepts and very technical details of C I just know the basics for example I didn't know about the concept trap representation.Thank you in advance

    p.s I know that when we have a structure compiler can add a "hole" at the end. So for this example maybe the memory layout is : structure member a , hole , integer x. So the t->a would give undefined behavior because t is a pointer to x not to member structure.
    Last edited by Mr.Lnx; 04-12-2015 at 05:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void pointers
    By see the big C in forum C Programming
    Replies: 6
    Last Post: 06-16-2010, 03:32 PM
  2. void* pointers
    By Drogin in forum C Programming
    Replies: 6
    Last Post: 09-04-2009, 11:04 AM
  3. void pointers
    By na_renu in forum C Programming
    Replies: 1
    Last Post: 10-21-2007, 11:34 AM
  4. void pointers
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 08-13-2003, 06:19 PM
  5. void pointers
    By coug2197 in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 02:41 PM