Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a C/C++ struct with compatibility

I have a struct defined in a historical library with alas, an unfortunate choice of type: unsigned char * instead of char*.

struct MyStruct {

  unsigned char * myMember;
};

This struct is used by a huge number of C applications and, more and more, by C++ applications. Those C++ applications raise an error when used with strlen, for example, forcing us to cast. Lots of casts.

I would like to remedy that.

But one thing is extremely important: it must be absolutely compatible without modification and have no impact on the existing projects.

I thought about making a union. Alas, my variable can't have the same name.

struct MyStruct {

  union {
    unsigned char * myMember;
    // char * myMember; Obvioulsy, rejected by the compiler.
};

I'm afraid there isn't any obvious solution. Am I wrong?

I can't change the size of the struct because it's mapped in an unchangeable sized shared memory.

like image 746
bitmap kid Avatar asked Dec 17 '20 10:12

bitmap kid


People also ask

Are a and C compatible types?

Now, from what I understand (admittedly from the C99 standard), is that a and c should be compatible types, as they fulfill all the criteria in section 6.2.7, paragraph 1. I've tried compiling with std=c99, to no avail.

What is the use of my struct in C?

struct MyStruct { unsigned char * myMember; }; This struct is used by a huge number of C applications and, more and more, by C++ applications. Those C++ applications raise an error when used with strlen, for example, forcing us to cast.

Are two C files with the same header always compatible?

At a guess it would seem that this guarantees that when two C files include a header that declares a type then instances of that type will be compatible. Interesting point. I was trying to think of an example that would test this theory under GCC, but couldn't think of one that exercises this idea.

Is it possible to use unsigned char* in C++?

Note: You can't solve this in C++, because strict C doesn't like unsigned char* either. Even with lax C compiler settings, you'd get (example with gcc default settings):


5 Answers

Note: You can't solve this in C++, because strict C doesn't like unsigned char* either. Even with lax C compiler settings, you'd get (example with gcc default settings):

pointer targets in assignment differ in signedness [-Wpointer-sign]|

even when doing something basic such as simple assignment ms.myMember="hello";.

Here is a C11 solution.

struct MyStruct {
  union // anonymous union
  {
    unsigned char * myMember;
    char* myCharMember;
  };
};  

#define myMember myCharMember

Test code:

struct MyStruct ms;  
_Generic(ms.myMember, 
         char*:           puts("I'm a char*"), 
         unsigned char*:  puts("I'm an unsigned char*"));

Without the #define and it will tell you "I'm an unsigned char*", but with the #define, "I'm a char*".

like image 170
Lundin Avatar answered Oct 17 '22 09:10

Lundin


There is a simpler remedy. Often, when you are annoyed by having to do something too many times, it can simply be solved by doing that thing once within a function:

std::size_t unsigned_strlen(const unsigned char* str) noexcept
{
    return std::strlen(reinterpret_cast<const char*>(str));
}
like image 8
eerorika Avatar answered Oct 17 '22 09:10

eerorika


There is no way you can make the myMember be a char* or any other type without potentially breaking existing projects, for the simple reason that its type can be deduced

void foo(decltype(MyStruct::myMember));

This will cause ABI issues, the mildest of which is a linker error.

The half measure is to provide a shorthand for the cast

struct MyStruct
{
#ifdef __cplusplus
   char* signed_myMember() { return (char*)myMember; }
#endif
};

Fortunately, char is allowed to alias anything in C++, making this legal.

like image 5
Passer By Avatar answered Oct 17 '22 09:10

Passer By


Since you are allowed to change MyStruct I suggest just removing unsigned which would break the existing ABI and require recompilation of everything using the struct.

If that's not an option for some reason, you could inherit the original struct and provide a getter method for the C++ applications.

// use this in C apps
struct MyStruct {
    unsigned char* myMember;
};

// use this in C++ apps
struct MyStructCpp : MyStruct {
    MyStructCpp(const MyStruct& rhs) : MyStruct(rhs) {}
    char* myMemberAsChar() const { return reinterpret_cast<char*>(myMember); }    
};
like image 2
Ted Lyngmo Avatar answered Oct 17 '22 07:10

Ted Lyngmo


Given your goal is to create an island of perfection and not fix the world, you could create an adapter with an implicit constructor from this old struct and use that adapter in the "island of perfection" code.

struct BetterMyStruct {
  char* myMember;
  
  BetterMyStruct(MyStruct const& x): myMember((char*)x.myMember) {
    // any ugly secret hacks you want or need
  }

};

...

size_t better_strlen(BetterMyStruct const& x) {
  // perfect function
  return std::string_view(x.myMember).size();
}

...

better_strlen(MyStruct(...)); // perfect function invocation on imperfect inputs
like image 2
bobah Avatar answered Oct 17 '22 08:10

bobah