Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding members to a C-style struct and backwards compatibility

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?

like image 279
rubenvb Avatar asked Jul 22 '10 13:07

rubenvb


2 Answers

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.

like image 100
R.. GitHub STOP HELPING ICE Avatar answered Oct 01 '22 03:10

R.. GitHub STOP HELPING ICE


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.

like image 40
Jukka Suomela Avatar answered Oct 01 '22 03:10

Jukka Suomela