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.
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.
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.
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.
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):
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*"
.
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));
}
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.
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); }
};
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With