grobartn
grobartn

Reputation: 3590

Get a pointer to structure in a map C++

Ok so I have struct like this

typedef struct
{
float x;
float y;
char name[];
} pTip;

And another struc

typdef struct
{
    float xx;
    float yy;
    pTip *tip;
}finalTip;

I create and populate a map<string, pTip> maps

That works fine. I am now trying to generate vector of finalTips

I do:

map<string, pTip>::const_iterator iter = maps.find(p_name);

So it works great my iterator now has what I need and I can extract info with

(iter->second).x

But I want to now using that iterator save it in my finalTip struc obj final So I tried:

finalTip final;
final.tip = iter->second;

And for this case I get error:

error: cannot convert 'const pTip' to 'pTip*' in assignment

So I fixed by:

*final.tip = iter->second;

Was this correct fix or am I doing it wrong. This seems to work but I want to make sure I am doing it right

Upvotes: 0

Views: 7724

Answers (4)

Jonathan Graehl
Jonathan Graehl

Reputation: 9301

You want

final.tip = &iter->second;

Since iter is a map<string, pTip> iterator, iter->second is a reference to pTip. Take its address with & to get a pointer.

Unfortunately, since you have a const_iterator, &iter->second will be a (const pTip *)

So, either get a non-const iterator, or make the .tip member a const pTip *, or if you're desperate, cast away the const:

final.tip = const_cast<pTip*>(&iter->second);

Final note: you may prefer pTip const* to const pTip * - they mean the same thing.

Upvotes: 4

Omnifarious
Omnifarious

Reputation: 56048

This code:

*final.tip = iter->second

is quite likely to eventually cause your program to start behaving erratically. You are stomping on memory. final.tip is an uninitialized pointer and it could be pointing anywhere. You are causing wherever it's pointing to be overwritten with the contents of iter->second.

I'm not completely sure what you want to accomplish here. The answer to your question is highly dependent on this.

The char name[] construct in pTip is also interesting and problematic. It looks like you're trying to use a stretchy buffer at the end of your structure, which is a common C technique. That is unlikely to work well when mixed with STL containers.

My suspicion is that you intend your map to map to pointers instead of struct copies. I suspect you meant something more like:

typedef struct
{
float x;
float y;
char name[];
} *pTip;

Upvotes: 0

Franci Penov
Franci Penov

Reputation: 76001

I am surprised this works. Unless you have a code that initializes final.tip that you have shown here, this would write the value of iter->second in a random place in the memory, which can lead to unpredictable results.

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 881635

Had you initialized final.tip to point to an "empty" tip...? Otherwise you're copying into uninitialized memory. Maybe you mean final.tip = new pTip(iter->second), i.e., a copy constructor into newly allocated storage? Also be careful about that char name[] construct -- without a length there, an automatically generated copy constructor may be in trouble doing things right, so you may need to explicitly code the copy ctor you need!

Upvotes: 0

Related Questions