Thread: struct attributes and typedefs

  1. #1
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    struct attributes and typedefs

    I'm trying to define a struct and typedef it with a single statement. I'm following the example described herehttps://gcc.gnu.org/onlinedocs/gcc-3...ttributes.html but I seems to be having a problem with even the code listed on this page.

    Code:
    #include <stdint.h>
    
    
    typedef int more_aligned_int __attribute__ ((aligned(8)));
    typedef struct TestCase
    {
        uint8_t data;
        char nothings;
        uint64_t not_nothing;
    } wait_status_ptr_t __attribute__((__packed__));
    
    
    int main(int argc, char **argv)
    {
        //wait_status_ptr_t wait;
    
    
        return 0;
    }
    The integer typedef works correctly but with the struct typedef I get.

    Code:
    main.c:9:1: warning: __packed__ attribute ignored [-Wattributes]
     } wait_status_ptr_t __attribute__((__packed__));
     ^

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    If you declare the struct and the typedef for it separately, it makes a bit more sense.

    But you can still do it in one.
    Code:
    #include <stdio.h>
    #include <stdint.h>
    
    
    typedef int more_aligned_int __attribute__ ((aligned(8)));
    typedef struct  __attribute__((__packed__)) TestCase
    {
      uint8_t data;
      char nothings;
      uint64_t not_nothing;
    } wait_status_ptr_t;
    
    
    int main(int argc, char **argv)
    {
      wait_status_ptr_t wait;
      printf("%zd\n",sizeof(wait));
      
      return 0;
    }
    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
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Salem View Post
    If you declare the struct and the typedef for it separately, it makes a bit more sense.

    But you can still do it in one.
    Code:
    #include <stdio.h>
    #include <stdint.h>
    
    
    typedef int more_aligned_int __attribute__ ((aligned(8)));
    typedef struct  __attribute__((__packed__)) TestCase
    {
      uint8_t data;
      char nothings;
      uint64_t not_nothing;
    } wait_status_ptr_t;
    
    
    int main(int argc, char **argv)
    {
      wait_status_ptr_t wait;
      printf("%zd\n",sizeof(wait));
      
      return 0;
    }
    Yes, a definition followed by a typedef makes much more sense but I was trying to follow some coding style.

    The suggested solution works (you're the best!) but doesn't quite match the coding style either so I'll stick to separate statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What would be the best way to name these typedefs?
    By Programmer_P in forum C++ Programming
    Replies: 52
    Last Post: 02-20-2011, 01:11 PM
  2. Using typedefs as friends
    By drrngrvy in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2007, 12:29 PM
  3. Standard typedefs ...
    By twomers in forum C++ Programming
    Replies: 6
    Last Post: 02-16-2006, 10:07 AM
  4. how to use typedefs with structs?
    By Yourhighness in forum C Programming
    Replies: 9
    Last Post: 06-03-2003, 04:43 AM
  5. Using TypeDefs
    By peking1983 in forum C Programming
    Replies: 2
    Last Post: 03-14-2003, 01:26 AM

Tags for this Thread