Structure declaration

This is a discussion on Structure declaration within the C Programming forums, part of the General Programming Boards category; Hi, Is the below mentioned coding practice valid? i. Declared one structure STRUCT_ABC in a header file abc.h ii. Included ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    12

    Structure declaration

    Hi,
    Is the below mentioned coding practice valid?

    i. Declared one structure STRUCT_ABC in a header file abc.h
    ii. Included abc.h in abc.c file and used STRUCT_ABC in some function inside abc.c.
    ii. Another file def.c does not include abc.h. But in def.c, i again defined a structure with same name, i.e. STRUCT_ABC, but with different contents.
    iv. Both abc.c & def.c are under same library and control first comes in abc.c during runtime.
    v. Control goes from abc.c to def.c and comes back, say multiple times.

    Can this give a runtime error always, or sometimes this might work?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    As long as these structures are not passed to/form abc.c to def.c, there should be no problem. I guess.
    Last edited by Bayint Naung; 06-22-2010 at 07:37 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,677
    Wrong, Bayint.

    The scenario described is a violation of the one-definition rule, as a struct declaration (other than a forward declaration) is a struct definition. Violations of the one-definition rule yield undefined behaviour.

    With undefined behaviour, the program may give a runtime error, it may not, or it may do so intermittently. Any set of behaviours is permitted - that is the definition in the standard of "undefined".

    It doesn't matter whether the two functions pass control to each other or not. The fact that the two definitions exist within one executable is problematic.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    What does the standard say about typedefs and defines? Are typedefs and defines internal to a file, or do they get exported by default into a global namespace (used by the compiler during compilation)? GCC does not complain when I create duplicate typedef names (of different types) in two different files, but then again it doesn't complain about duplicate symbols either when I define two variables with the same name (which it should).

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It'd be good if grumpy could point out where standard says so?

    Code:
    void foo(void)
    {
       struct foo_struct {
        int a;
       char *s;
       };   // this struct def is local to function
      typedef char *String;  // typedef is local to function scope;
      
    }
    
    int main(void)
    {
       struct foo_struct {
          char  a[100];
       };  // this is local to main
      String s; // error! 
    return 0;
    }
    Last edited by Bayint Naung; 06-22-2010 at 08:33 AM.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    12

    Small correction

    As per my query, the structure is defined not inside any function or main function.
    Its specific to C source file and not any function.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes, I understand that. I'm not sure for file but I guess it should be the same. But I cannot find anything related to C with the google keyword One-definition rule.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    12
    Thanks Grumpy for the info.

    But how come it's working fine sometime, and throwing segmentation fault some other time. I suppose if its a standard violation, then it should always throw error or create problem during runtime.

    To be specific, this type of code is working here at our end in production from nearly last 1 year. Recently only it showed issues.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It may be illegal in C++. But for C, you might want to read Duplicate struct definition in different source file - Stack Overflow.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    12
    Quote Originally Posted by Bayint Naung View Post
    It may be illegal in C++. But for C, you might want to read Duplicate struct definition in different source file - Stack Overflow.
    So standard wise its fine.
    Also, as per the example quoted in your reply, the structure with same name are present in both the c sources.
    In my case, one of the instance of structure is defined in header file and another in one of the c sources. Also, library of this C source and the source which includes that header file is same.

    So now in this context, probably the storage of structure definition during runtime from header file vs C source might create some issue??

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    abc.h
    
    struct STRUCT_ABC {
      int a;
    };
    
    abc.c
    
    #include "abc.h"
    
    void abc(struct STRUCT_ABC *a)
    {
    }
    
    def.c
    
    struct STRUCT_ABC {
      char *s; 
      double x;
    };
    
    void def(struct STRUCT_ABC *a)   // if you export def() function, 
    problem since compile will use struct STRUCT_ABC defined in abc.h
    {
    }
    
    main.c
    
    #include "abc.h"  //include struct def
    
    int main(void)
    {
      struct STRUCT_ABC foo;
       def(&foo);                    // problem!!
       abc(&foo);                   // OK
       return 0;
    }
    You can not export any function defined in def.c that needs struct STRUCT_ABC. (i.e functions that take STRUCT_ABC as para or return STRUCT_ABC).
    Last edited by Bayint Naung; 06-22-2010 at 10:02 AM.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    12

    Small modification in your program

    Quote Originally Posted by Bayint Naung View Post
    Code:
    abc.h
    
    struct STRUCT_ABC {
      int a;
    };
    
    abc.c
    
    #include "abc.h"
    
    void abc(struct STRUCT_ABC *a)
    {
    }
    
    def.c
    
    struct STRUCT_ABC {
      char *s; 
      double x;
    };
    
    void def(struct STRUCT_ABC *a)   // if you export def() function, 
    problem since compile will use struct STRUCT_ABC defined in abc.h
    {
    }
    
    main.c
    
    #include "abc.h"  //include struct def
    
    int main(void)
    {
      struct STRUCT_ABC foo;
       def(&foo);                    // problem!!
       abc(&foo);                   // OK
       return 0;
    }
    You can not export any function defined in def.c that needs struct STRUCT_ABC. (i.e functions that take STRUCT_ABC as para or return STRUCT_ABC).
    What if flow goes as below:

    def.c

    struct STRUCT_ABC {
    char *s;
    double x;
    };

    void def() // Fine
    {
    struct STRUCT_ABC foo;
    <local usage of foo valid here>
    <return to main>
    }

    int main(void)
    {
    struct STRUCT_ABC foo;
    abc(&foo); // OK
    def(); //def called after abc
    return 0;
    }

    This is the actual replica of code which i'm working currently with. abc.c & header file same as the way what you coded.
    Additionally control goes in def after abc.
    Also, in my case not necessary that both the functions will be called from same common function. Only common thing is that both come in the same flow.

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int main(void)
    {
    struct STRUCT_ABC foo;  // i assume you include "abc.h" 
    abc(&foo); // OK  
    def(); /* def called after abc  , it should be absolutely fine. 
    def() uses its own definition of STRUCT_ABC defined in def.c  */
    return 0;
    }
    Last edited by Bayint Naung; 06-22-2010 at 10:22 AM.

  14. #14
    Registered User
    Join Date
    Jun 2010
    Posts
    12
    Quote Originally Posted by Bayint Naung View Post
    Code:
    int main(void)
    {
    struct STRUCT_ABC foo;  // i assume you include "abc.h" 
    abc(&foo); // OK  
    def(); /* def called after abc  , it should be absolutely fine. 
    def() uses its own definition of STRUCT_ABC defined in def.c  */
    return 0;
    }
    Yes correct.
    Now current this type of coding in our case was working fine since last say 1 year. But recently it started giving segmentation fault exactly at the point of structure variable declaration as in def.c
    void def() // Fine
    {
    struct STRUCT_ABC foo; ==> <This line gave segmentation fault>

    So as grumpy quoted earlier, this issue wont occur everytime, but sometimes it might occur. If this is correct, then what can be the probable reason of issue occuring sometimes and not everytime?

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It should be easy to change STRUCT_ABC to other name in def.c. (probably use script)
    And see whether problem exists.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21