segfault on vector<struct>
Posted
by Andre
on Stack Overflow
See other posts from Stack Overflow
or by Andre
Published on 2010-04-29T17:15:48Z
Indexed on
2010/04/29
19:27 UTC
Read the original article
Hit count: 323
Hello,
I created a struct to hold some data and then declared a vector to hold that struct.
But when I do a push_back I get damn segfault and I have no idea why!
My struct is defines as:
typedef struct Group
{
int codigo;
string name;
int deleted;
int printers;
int subpage;
/*included this when it started segfaulting*/
Group(){ name.reserve(MAX_PRODUCT_LONG_NAME); }
~Group(){ name.clear(); }
Group(const Group &b)
{
codigo = b.codigo;
name = b.name;
deleted = b.deleted;
printers = b.printers;
subpage = b.subpage;
}
/*end of new stuff*/
};
Originally, the struct didn't have the copy, constructor or destructor. I added them latter when I read this post below.
http://stackoverflow.com/questions/676575/seg-fault-after-is-item-pushed-onto-stl-container
but the end result is the same.
There is one this that is bothering me as hell! When I first push some data into the vector, everything goes fine. Later on in the code when I try to push some more data into the vector, my app just segfaults!
The vector is declared
vector<Group> Groups
and is a global variable to the file where I am using it. No externs anywhere else, etc...
I can trace the error to:
_M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage- this->_M_impl._M_start);
in vector.tcc when I finish adding/copying the last element to the vector....
As far as I can tell. I shouldn't be needing anything to do with a copy constructor as a shallow copy should be enough for this. I'm not even allocating any space (but I did a reserve for the string to try out).
I have no idea what the problem is!
I'm running this code on OpenSuse 10.2 with gcc 4.1.2
I'm not really to eager to upgrade gcc because of backward compatibility issues...
This code worked "perfectly" on my windows machine. I compiled it with gcc 3.4.5 mingw without any problems...
help!
--- ... ---
:::EDIT:::
I push data
Group tmp_grp;
(...)
tmp_grp.name = "Nova ";
tmp_grp.codigo=GetGroupnextcode();
tmp_grp.deleted=0;
tmp_grp.printers=0;
tmp_grp.subpage=0;
Groups.push_back(tmp_grp);
© Stack Overflow or respective owner