views:

83

answers:

5

Say I have a struct defined somewhere deep in low-level code used all over the place in the most crazy and unknown ways:

struct T {
    unsigned short name_len;
    char d_name[LENGTH];
}

With accompanying functions that fill d_name with whatever needs to be put there, like

struct T* fill( somethingOrOther* X)

And I would like to extend the old struct+function to include a new variable:

struct T {
    unsigned short name_len;
    char d_name[LENGTH];
    unsigned short type_len;
    char d_type;
}

and the new version of the function would also fill the d_type variable with useful things.

Would this type of change break the API? Couldn't I just use the new T instead of the old T, and additionally access the new members?

Thanks.

+1  A: 

Yes- when you use an opaque pointer like that, then it is irrelevant of the contents. As long as your users only ever used the opaque pointers, you can muck around with the struct and it's implementation all you like.

DeadMG
So, yes, it won't break the API? (kind of confusing with my two questions ;) )
rubenvb
As long as you recompile all code that uses anything other than just the pointer, you'll be fine.
torhu
+1  A: 

If T really is used all over the place in crazy and unknown ways, then a change like that is likely to break something. Somewhere there will be a piece of code that has a local declaration of T instead of using your header file, or casts a 'mystruct *' to a 'T *', or something equally repugnant.

rettops
+3  A: 

It might make sense to extend your structs like this:

struct newT {
    struct T t;
    int newElement;
    ...
}

Then you can safely use a newT pointer as a T pointer; the C standard guarantees that there is no padding before the first element of a struct.

Jukka Suomela
+1  A: 

As long as the code using this API only obtains T objects as pointers returned by the library, and does not declare them itself, malloc them itself (using sizeof(struct T)), or do anything else that depends on the size of the struct, then it should be fine. If the calling code accessed the contents of the struct, you need to make sure you put new members at the end of the struct.

One possible additional consideration is whether any code depends on d_name being at the end of the structure in order to allocate space for and store larger names if the declared size does not fit. I only bring this up because the names of the members suggest the struct is something like dirent and that's traditional practice for dirent.

R..
dang, I was waiting for someone to figure it out :). i found a patch implementing d_type for mingw, but they too used a wrapper struct containing the old one. I suggested it to the mingw-w64 devs but haven't heard back so far. Thanks for the informative answer.
rubenvb
Well in that case, I think there are some things to consider. From an **API** standpoint, putting the new members before `d_name` is better because it allows future treatment of `d_name` as variable-length. On the other hand, from an **ABI** standpoint, new members have to be added at the end. I doubt much depends on the ABI (afaik this code is static-linked), but it's possible that there are DLLs which use `dirent` and communicate `dirent` objects back with the calling program, in which case there are ABI issues, so preserving ABI is probably worthwhile.
R..
BTW, since you mentioned that you're patching mingw, could you look at my question here: http://stackoverflow.com/questions/3228828/how-to-best-deal-with-windows-16-bit-wchar-t-ugliness I've been working on this UTF-8 patching for a project I work on to better support Windows, but I don't do Windows myself. Would you be interested in trying to integrate and test the code I've written?
R..
A: 

Crazy and unknown is not good. The best bet is to scan through the codebase for struct T and examine how it is used, with the alternative approach of changing it and see if something breaks... If the code only uses opaque pointers, you should be on the safe side. If the code accesses the members, but doesn't do something weird, you should be safe too, with a complete recompilation.

If it does something weird, like the examples of rettops, even Jukka's tip may not help. The code could use a hardcoded value for the sizeof to do pointer arithmetic within an array of these structs...

Secure