I have a weird problem. I have a vector that I would like to push objects on to like so:
vector<DEMData>* dems = new vector<DEMData>();
DEMData* demData = new DEMData();
// Build DEMDATA
dems->push_back(*demData);
There will be a few hundred DEMData objects in the vector. The problem is when this code is finished, all items are equal to the last item “pushed back” to the vector?
Why are the other objects being overridden in the vector?
edit:
The DemData class is simple, just a data structure with setters and getters:
class DEMData
{
private:
int bitFldPos;
int bytFldPos;
const char* byteOrder;
const char* desS;
const char* engUnit;
const char* oTag;
const char* valType;
int index;
public:
void SetIndex(int index);
int GetIndex() const;
void SetValType(const char* valType);
const char* GetValType() const;
void SetOTag(const char* oTag);
const char* GetOTag() const;
void SetEngUnit(const char* engUnit);
const char* GetEngUnit() const;
void SetDesS(const char* desS);
const char* GetDesS() const;
void SetByteOrder(const char* byteOrder);
const char* GetByteOrder() const;
void SetBytFldPos(int bytFldPos);
int GetBytFldPos() const;
void SetBitFldPos(int bitFldPos);
int GetBitFldPos() const;
friend std::ostream &operator<<(std::ostream &stream, DEMData d);
};
EDIT:
I am reading an XML file and building DEMData objects based on the attributes of each xml element:
DEMData demData;
for (i = 0; attr[i]; i += 2)
{
if(strcmp(attr[i],"BitFldPos") == 0)
{
demData.SetBitFldPos(*attr[i + 1] - '0');
}
else if(strcmp(attr[i],"BytFldPos") == 0)
{
char* pEnd;
int tmp = strtol(attr[i + 1],&pEnd,10);
demData.SetBytFldPos(tmp);
}
else if(strcmp(attr[i],"ByteOrder") == 0)
{
demData.SetByteOrder(attr[i + 1]);
}
else if(strcmp(attr[i],"DesS") == 0)
{
demData.SetDesS(attr[i + 1]);
}
else if(strcmp(attr[i],"EngUnit") == 0)
{
demData.SetEngUnit(attr[i + 1]);
}
else if(strcmp(attr[i],"OTag") == 0)
{
demData.SetOTag(attr[i + 1]);
}
else if(strcmp(attr[i],"ValTyp") == 0)
{
demData.SetValType(attr[i + 1]);
}
else if(strcmp(attr[i],"idx") == 0)
{
char* pEnd;
int tmp = strtol(attr[i + 1],&pEnd,10);
demData.SetIndex(tmp);
}
//printf(" %s='%s'", attr[i], attr[i + 1]);
}
// Insert the data in the vector.
dems.push_back(demData);
Why are you allocating the vector with new? Why are you allocating your temporary
DEMDataobject with new?A
vectorstores a copy of what you pass to it, not that data itself, so unless you’re deleting the DEMData object you allocated with new, you’re leaking memory ever time you push an item onto the vector. Likewise, you’re setting yourself up for memory leak problems by allocating the vector dynamically.As to why the objects in the vector all seem to contain the same data, chances are pretty good that you have more of the same — probably use of pointers combined with an incorrect copy ctor that ends up doing shallow copies a few places it shouldn’t — but since you haven’t shown us that code, that’s only a guess.
Edit: Now that you’ve added the code for your DEMData class, it looks very much like the guess above was correct — you have pointers and no user-defined copy ctor, so you’re getting a shallow copy.
As a first step, I’d get rid of all the pointer char members, and replace them with
std::strings. Theintmembers should be all right — copying them will copy the values.Edit2: Given what you’re doing with these member variables, it looks a lot like you should consider using two
std::maps — one for thestd::stringvariables, and one for theintvariables.