I wonder how visual c++ std::string is populated in memory.
For example if I declare std::string container[10]
in a shared memory region, then can I insert any length of string into this array at run-time ?
Since it is known that shared memory region can not expand at run-time, then how can we insert varying size of strings into this array ?
Asked
Active
Viewed 1,243 times
1
-
1Are you talking about memory shared between multiple processes or something else? If you are, it won't work without a custom allocator at the very least. `std::string` will perform heap allocations (which won't be in the shared area by default) so any process trying to access the strings (other than the one that constructed them) will fail. – nobody May 08 '14 at 05:52
-
Thanks for your points. Yes about memory shared between 2 processes. Now it is more clear. Just to be sure that I got the point, I wanna ask that, when a shared memory region created with a field std::string sample[10], then how much memory is allocated for this purpose. For example, it would be clear if it was array of char, int, float etc. Because their sizes are already known at compile time. I wonder the case with std::string. – Carlos May 08 '14 at 06:04
-
Allocators will allow you to put strings in shared-memory.. However, the size of the shared memory MUST be known before hand.. Otherwise you need to make the allocator create a new set, copy the old memory into the new one.. Example allocator: http://stackoverflow.com/questions/23459142/interprocess-communication-binary-data-serialized-objects – Brandon May 08 '14 at 06:24
-
@Brandon: I understand how the custom allocator allows to place an allocation into shared memory. But for a string, you really have two "items". The string object, and the memory allocated by the string to hold the characters. Say you manage to place both of these in the shared memory. Then you map the shared memory into two processes, which generally get two different virtual addresses that point to the shared memory. What happens when both access the string, and the string then accesses its pointer to the characters? The two processes have the characters at different virtual addresses. – Reto Koradi May 08 '14 at 07:16
-
... The only way I can see this working is if you force the shared memory to be mapped to the same virtual address in both processes. Or am I missing something? – Reto Koradi May 08 '14 at 07:17
-
@RetoKoradi Really a very good point. But too much overhead for a simple task (or it seems as simple but it is not). Maybe better never to use STL in shared memory. I don't know. I am more confused now. – Carlos May 08 '14 at 07:29
-
@Carlos: The simple answer IMHO is really: Don't do it! Only use simple types/structs in shared memory. I was just curious how it could potentially be done. More for "academic" reasons, than ever wanting to apply it in real life. – Reto Koradi May 08 '14 at 07:40
-
@RetoKoradi it shouldn't matter whether the string has two `items`. I get what you mean. It actually works for strings though. The entire string's contents are placed in the memory and not the address or pointers. Anything that `std::string` and any stl container allocates uses an allocator: http://stackoverflow.com/questions/21812175/boost-pool-experience-requested-is-it-useful-as-allocator-with-preallocation/ The allocator returns the address of where the container will place its contents. In the other process, you just use `reserve` so it isn't overwritten. I tested it all. – Brandon May 08 '14 at 15:38
1 Answers
1
That sounds like a really bad idea. std::string involves dynamic memory allocation, pointers, etc. If you have two processes that run in separate address spaces accessing it, I don't see how that could work.

Reto Koradi
- 53,228
- 8
- 93
- 133