在共享内存中共享 std::string
Sharing std::string across shared memory
我希望使用共享内存跨进程共享 std::string
。但是,我关心的是字符串对象分配到哪里,是在栈上,还是在堆上,因为这会影响共享。
参考这个,MSDN Forum
...它说:
please note that in new STL (Visual Studio 2003 and 2005), the
std::string class uses a combined variant of allocating strings. If
the length is long then the string is allocated in heap area, but if
is short, it is stored in a preallocated area of the class
我不知道字符串可能有多长...我不想为其分配任何固定内存。
我原本打算做的是...
wstring somestring;
somestring.sppend(someOtherString); //several times
我想 wstring somestring[256]
会在堆栈上,所以我可以轻松地分享它。但是,如果我不想分配大小怎么办?如果大小超过阈值会发生什么?
I wish to share a std::string across processes using shared memory.
您不能跨进程边界共享非 POD 类型,尤其是可能在内部分配内存的类型。不能保证其他进程使用相同版本的 STL,如果它们完全使用 STL。即使他们这样做了,他们也会使用不同的内存管理器。
您可以分配一个固定长度的 char[]
数组作为共享内存,并将 std::string
的字符内容复制到其中。
I suppose wstring somestring[256]
will be on the stack and so I can share that easily.
不,你不能。 somestring
本身会在堆栈上,但它是一个 std::wstring
对象的数组,并且 std::wstring
不能共享。
But what if I don't wish to have a size allocated? And what would happen if the size grows beyond a threshold?
共享内存不能动态调整大小。
我希望使用共享内存跨进程共享 std::string
。但是,我关心的是字符串对象分配到哪里,是在栈上,还是在堆上,因为这会影响共享。
参考这个,MSDN Forum
...它说:
please note that in new STL (Visual Studio 2003 and 2005), the std::string class uses a combined variant of allocating strings. If the length is long then the string is allocated in heap area, but if is short, it is stored in a preallocated area of the class
我不知道字符串可能有多长...我不想为其分配任何固定内存。
我原本打算做的是...
wstring somestring;
somestring.sppend(someOtherString); //several times
我想 wstring somestring[256]
会在堆栈上,所以我可以轻松地分享它。但是,如果我不想分配大小怎么办?如果大小超过阈值会发生什么?
I wish to share a std::string across processes using shared memory.
您不能跨进程边界共享非 POD 类型,尤其是可能在内部分配内存的类型。不能保证其他进程使用相同版本的 STL,如果它们完全使用 STL。即使他们这样做了,他们也会使用不同的内存管理器。
您可以分配一个固定长度的 char[]
数组作为共享内存,并将 std::string
的字符内容复制到其中。
I suppose
wstring somestring[256]
will be on the stack and so I can share that easily.
不,你不能。 somestring
本身会在堆栈上,但它是一个 std::wstring
对象的数组,并且 std::wstring
不能共享。
But what if I don't wish to have a size allocated? And what would happen if the size grows beyond a threshold?
共享内存不能动态调整大小。