pointer to map

Hello

I want to create an array of map structures.
i am using the bellow code but it seems that I doesn't work
/////
typedef map <double, double> mapdata;
mapdata ** point;
point =(mapdata **)malloc(20);
(*point[1])[1]=1;
/////

The problem seems to be at the initialization (*point[1])[1]=1; but I'm not sure
Does anybody has a good example of how to write an array of map?

Thanks,
Bogdan
In your code you allocate not 20 objects, but 20 bytes. Those bytes are 5 mapdata* that don't point to anything. even if you then did point[0] = (mapdata*)malloc(sizeof(mapdata)); it would still not work, since malloc won't call map's constructor. For dynamic memory use new. Also, you don't need a ** at all.
Never use malloc in C++. It's not a generally true statement, but a good guideline for beginners. malloc is not the same as new.

One way to do what you want to do is:
1
2
3
4
5
6
7
8
9
typedef std::map<double, double> mapdata;

std::vector<mapdata*> points;

// add an entry
points.push_back(new mapdata);

// add a value to the first map in the collection
points[0]->insert(std::make_pair(2.0, 3.0));
Hello,

Thanks for the malloc tip.
Well.. I first wanted an array of structures but a vector will do as well.

Thanks
1
2
3
mapdata** point = new mapdata*[5];
point[0] = new mapdata;
points[0]->insert( ... );
Is fine too. Just don't forget delete everything. Though using a vector is usually better.
Topic archived. No new replies allowed.