Re: Using STL(map) inside of C data structure. How?
Andrew Trapani a ?crit :
#include <map>
#include anything else necessary
typedef struct {
std::map<const char*, int> m;
} sType;
int main(int argc, char **argv) {
numberOfStructs = 5;
sType s = (sType*) malloc(sizeof(sType) * numberOfStructs);
s.m.empty(); /* seg faults here */
}
Is this possible? Thanks.
If I understand correctly, you want to place a std::map into a memory area.
To that end, there should be the following steps:
1. Provide an allocator to map in order to perform allocation into
your memory area. That is a bit tricky but it should look like a pool
allocator.
template<void* mem_alloc,size_t mem_size>
typedef std::map<const char*, int,
std::less<const char*>,
my_pool_allocator<std::pair<const char*,int>,
mem_alloc,mem_size>
> map_t;
2. Use placement new to locate your map into the memory area
void * memory=malloc( /* enough size */);
map_t* s=new(memory) map_t<memory+sizeof(map_t),size>();
I don't known about the possibility of the allocator. If a malloc call
in the pool_allocator is acceptable it would be easier.
Michael
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]