私は次の構造を持っています:
struct outData{
int a;
float lat, lon;
}
これは、共有メモリを介して IPC に使用されます。今、私はそれを次のように更新したいと思います:
struct outData{
int a;
std::vector<std::pair<std::string, int>> allInts;
std::vector<std::pair<std::string, float>> allfloats;
}
私の共有メモリは利便性のために 4096 バイトの大きさなので、構造体を変更するたびにコードの sizeof(outData) 行を変更する必要はありません。
このような構造体を動的メンバーで作成すると、それらがすべて (int a) の後に作成され、したがって共有メモリ内に作成されることが保証されますか?
ベクトルのベクトルはどうですか?
struct outData{
int a;
std::vector<std::pair<std::string, int>> allInts;
std::vector<std::pair<std::string, float>> allfloats;
std::vector<std::pair<std::string, std::vector<byte>>> allByteMessages;
}
うわー、速い答えをありがとう!あなたの入力に基づいて、私はこの解決策を考え出しました:
struct outData{
int *iarray;
float *farray;
} gtempStruct;
SetSharedMem(std::vector<std::pair<int, float>> &input)
{
void * p_v;
gtempStruct.iarray = new int[input.size()];
gtempStruct.farray = new float[input.size()];
fillStruct(input);
outData *p_oD = (outData *) p_Shm; // p_Shm = pointer to shared memory start
*p_oD = gtempStruct;
p_v = p_oD;
p_v = reinterpret_cast<char*>(p_v) + sizeof(outData) -1;
p_v = reinterpret_cast<void*>(p_v);
memcpy(p_v, gtempStruct.iarray, sizeof(int)*input.size())
p_oD->iarray = (int*) p_v;
.
.
.
}
これは機能しますが、十分にテストされていません。ありがとうございました!