Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

465
Views
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.

over 4 years ago · Santiago Trujillo
5 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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));
}
over 4 years ago · Santiago Trujillo Report

0

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); }    
};
over 4 years ago · Santiago Trujillo Report

0

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*".

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!