Thread: Aliasing and/or other non-conformant behaviour

  1. #1
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787

    Aliasing and/or other non-conformant behaviour

    Hello,

    Hodor here.

    In the code fragment below is there any behaviour that is not defined?

    Code:
    #include <stdio.h>
    
    struct foo {
        int a, b;
    };
    
    struct bar {
        struct foo coord;
        const char *description;
    };
    
    void init(struct foo *p)
    {
        p->a = 42;
        p->b = 84;
    }
    
    int main(void)
    {
        struct bar obj;
        struct foo *p;
        struct bar *p2;
        
        init((struct foo *) &obj);
        obj.description = "test";
        
        p = (struct foo *) &obj;
        p2 = (struct bar *)p;
        
        printf("%d %d %s\n", p2->coord.a, p2->coord.b, p2->description);
        
        return 0;
    }
    Edit: I'm not asking whether or not there is any "dangerous" behaviour by the way, I'm only interested in conformance (or not) with the C Standard.
    Last edited by Hodor; 11-19-2015 at 11:36 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Quote Originally Posted by draft c99
    6.7.2.1 Structure and union specifiers
    12 Within a structure object, the non-bit-field members and the units in which bit-fields
    reside have addresses that increase in the order in which they are declared. A pointer to a
    structure object, suitably converted, points to its initial member (or if that member is a
    bit-field, then to the unit in which it resides), and vice versa. There may be unnamed
    padding within a structure object, but not at its beginning.
    It seems a pointer to a struct and a pointer to the first member would always be the same.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    It seems a pointer to a struct and a pointer to the first member would always be the same.
    That's how I'm reading it as well. E.g. (struct foo *) &obj == &obj.coord and therefore aliasing rules are not applicable?

    Edit: I'm asking about strict-aliasing because someone mentioned that this code snippet might not be conforming due to the aliasing rules. But I am reading/interpreting the standard differently (and think that it's fine) and we could not come to a consensus.

    Edit 2: gcc (with -fstrict-aliasing -Wall -Wextra -Wpedantic) says nothing (although that doesn't mean much in itself) but splint reports no applicable errors or warnings either, so the only way forward seems to be the C Standard (C99 or higher) and discussion

    Edit 3: My argument is that the types are compatible because of the section you referenced and the equivalence I used as an example. I.e. They are a pointer to the same type (even though in the first instance there needs to be an explicit cast)
    Last edited by Hodor; 11-20-2015 at 12:45 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    A pointer (to the whole struct) and a pointer to the first member can never be the same type.
    They are however the same value.

    Code:
    #include <stdio.h>
    
    struct foo {
      int bar;
    };
    
    int main ( ) {
      struct foo f;
      struct foo *fp = &f;
      int *ip = &f.bar;
      
      // There is no aliasing here
      *ip = 42;
      fp->bar = 99;
    
      printf("%p %p\n", fp, ip );
      return 0;
    }
    fp and ip point to the same memory location, but we haven't coerced any pointers into a different type.
    Both assignments refer to the same int, as an int.

    Understanding Strict Aliasing - CellPerformance
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strict aliasing
    By MOS-6581 in forum C Programming
    Replies: 4
    Last Post: 05-17-2014, 05:18 PM
  2. about aliasing
    By Kempelen in forum C Programming
    Replies: 1
    Last Post: 11-11-2009, 08:39 AM
  3. Anti aliasing
    By arjunajay in forum Windows Programming
    Replies: 7
    Last Post: 08-15-2006, 08:24 PM
  4. pointer aliasing
    By moi in forum C Programming
    Replies: 2
    Last Post: 02-21-2006, 11:08 AM
  5. anti aliasing
    By muttski in forum Game Programming
    Replies: 2
    Last Post: 03-16-2002, 06:55 AM