views:

62

answers:

5

I have a private attribute in a class that is defined as vector<pair<char *, int> > data;. I add data to this vector with data.push_back(make_pair(p, r));. Later when I go to get the data out of the vector I get bad data for the p value. The data returned is like ��U3. I think this is because a pointer to the char array is being stored. How would I go about storing an actual copy in the vector. If it helps the char array will never exceed 255 chars + 1 for null terminating.

+1  A: 

It looks as though you have a pointer to p (which is defined at the time) placed on the stack. Once the stack frame gets popped off you still have the pointer but the memory it points to may be garbage. These sort of dangling pointer problems can get annoying so I'd recommend using the std::string class that is defined in #include <string> to store the string data.

shuttle87
Doesn't necessarily have anything to do with the stack.
Potatoswatter
@Potatoswatter: True, without code I can't really say with any certainty but it was just a guess as to what I thought was possible.
shuttle87
+5  A: 

Any real reason to using a char*?

Use an std::string, that will make your problems go away.

Idan K
+1  A: 

Since STL is based on copy and value semantics, it is easier if the entire character string, not merely a pointer to it, is stored in the container (here, vector). I said "easier" because it is possible to keep pointers in STL containers, but then the memory pointed to must be managed by user code. From the question, it seems that it is not been done.

So, perhaps you can use string instead of char *, for e.g.:

typedef std::pair< std::string, int > MyPair;
std::vector< MyPair > data;
ArunSaha
+1  A: 

If you have a vector<pair<char *, int> > you're dealing with naked pointers and need to manually manage the lifetime of your strings. That's a major PITA and it is very likely that you're doing something wrong (especially so since you didn't even bother to claim to do this right, which seems to suggest you don't even know you have to do this).

As the others, I'd suggest you use vector<pair<std::string, int> > instead.

sbi
+1  A: 

Are you allocating the string via a stack variable and storing it's address? That would cause the problem you describe when you return from the function you allocate in.

Better to allocate the string on the heap (with the new operator), then store the heap allocated address. e.g.

char* pNext = new char[50];
strcpy(pNext, ...);
data.push_back(make_pair(pNext, r));

You have to do all your own work with char arrays -e.g. You must ensure they're null terminated, or you'll have unexpected output results when you go to print them.

e.g.

pNext[49] = '\0';

Also, remember that when you're finished with a heap allocated string, you must delete them too. char*'s are deleted thus:

delete [] pNext;

As others have said, there are other string implementations that will cause you less headaches.

The easiest to use is probably CString, but incurs the overhead of either including MFC's implementation or ATL's implementation of it. std::string's, as others have mentioned are an alternative.

hope this helps!

freefallr