Hi, I'm wondering how to give a map a fixed size like you can give an array a fixed size by just "byte data[SIZE*DATASIZE]".. or if it's possible anybody know..

struct Table {
map<int, int> table; //
};

struct Table *flightChart;

The reason I have to give this a fixed size, is becuase I have to map this in shared memory :\, so that another process may access it, or is it possible to pass this structure to another process without shared memory :\

Thanks in advance.

Recommended Answers

All 2 Replies

No, associative containers don't support setting the capacity without explicitly adding a range of items.

>I have to map this in shared memory
That's not an issue. The map object itself has a fixed size because it uses dynamic memory by default for the items. Provided you share the structure correctly, none of the processes will step on another's toes. But that has nothing to do with the map, it has to do with sharing memory in general.

The reason I have to give this a fixed size, is becuase I have to map this in shared memory...

just placing the map in shared memory is insufficient; you would need to
a. write a custom allocator (compatible with std::allocator) that can (sub)allocate from the shared memory region for the map.
b. place the map itself in shared memory. requires sizeof( map<int,int,custom_alloator>)
c. ensure that the shared memory is mapped at the same virtual address in every process that uses it (allocators return absolute pointers).
d. you would also need to synchronize access to the map.

it would probably be better to just create a fixed size array of structures (key,value) sorted on the key and do a binary search.
ie. assuming that the keys are not very volatile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.