tags:

views:

40

answers:

3

Hi,

  1. Declared one structure STRUCT_ABC in a header file abc.h
  2. Included abc.h in abc.c file and used STRUCT_ABC in some function inside abc.c.
  3. 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.
  4. Both abc.c & def.c are under same library and control first comes in abc.c during runtime.
  5. 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?

A: 

It certainly won't cause you a runtime error.

The compiler will pick up the definition of the structure it saw when compiling the file that uses the structure, so you shouldn't get any compilation or linker errors either.

Really, though, if you want to use the same structure in 2 places, you're better off defining it in a single header and #includeing it in multiple .c files. It will make maintenance easier (you only need to update the structure once) and you'll know for sure which definition you're using (since there's only one).

chrisbtoo
A: 

If my memory is correct, this is compiler dependent (depends on how much decoration is applied to the definitions of the struct), but it would usually work (though we'd need more specifics to be sure). As long as the two units of code don't know about the conflicting declaration in the other unit, the compiler isn't really using the same name for each struct, and eventually it compiles down to an unnamed memory block in any event. Don't go passing the struct from abc to def and expect it to work (it will probably error on compile if you try), but as long as they aren't stepping on each others' toes it should be fine.

ShadowRanger
Responding to chrisbtoo (I can't figure out how to add the comment to his post; am I missing something obvious?): His struct has the same name, but different contents. So a single common definition wouldn't help. This sounds more like an accidental overlap in naming, or a common idiom expressed differently depending on the code using it (say, two Point structs, one in integer space and one in real/double space).
ShadowRanger
A: 

Like the other posters stated, if you are strictly defining the structs in both places, you should be okay. However, you're asking for trouble, particular if def.c ever needs to include abc.h.

From your description, it isn't 100% clear to me whether STRUCT_ABC is the struct name or an instance name. If you are defining instances of the structs in both files (outside of a function), and the instances are named the same you would have a compile problem unless you declare one or both instances static.

The errors should be compile errors, unless you trick a function expecting one STRUCT_ABC into using the other other (ie through a pointer).

Chris Morlier
As of now, i wont be including abc.h inside def.c.Also, i have two different structures, one in header file and another in def.c with different contents, but with same name.Eg.struct LOCAL_UTILS_struct_{ int i; char [] str1;};typedef struct LOCAL_UTILS_struct_ LOCAL_UTILS_STRUCT_TYPE;struct LOCAL_UTILS_struct_{ float f1; long i;};typedef struct LOCAL_UTILS_struct_ LOCAL_UTILS_STRUCT_TYPE;Note : This code worked fine, but recently it started giving segmentation fault at structure variable declaration in def.c
Rajneesh
As a simple experiment, I would try renaming one of the `LOCAL_UTILS_struct_` declarations (and usages). This will easily allow you to eliminate the name conflict as the problem, and might highlight the real one.If its not the problem then you can revert.
Chris Morlier