struct Data {
int a;
std::string b;
float c;
};
std::string* allocateDataAndGetString() {
Data* dataPtr(someAllocator.allocate<Data>());
return &dataPtr.b;
}
Data* getBaseDataPtrFromString(std::string* mStringMember) {
// ???
}
int main() {
std::string* stringPtr(allocateDataAndGetString());
Data* dataPtr(getBaseDataPtrFromString
}
I have a Data
instance allocated on the heap, and a pointer to its std::string b;
member. How do I get the base address of the Data
instance the string is a member of, taking into account offsets and padding, in a standard way?
I've tried subtracting sizeof(int)
and std::offsetof(Data, std::string)
from the std::string*
pointer, but I couldn't get it to work.