Segfault on vector <struct>

I created a structure to store some data, and then declared a vector to store this structure.

But when I do push_back, I get a damn segfault, and I have no idea why!

My structure is defined 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*/ }; 

Initially, the structure did not have a copy, constructor, or destructor. I added them later when I read this post below.

Seg error after item is placed in STL container

but the end result is the same.

There is one that bothers me like hell! When I first insert some data into the vector, everything goes well. Later in the code, when I try to insert some more data into the vector, my application is just segfaults!

Vector Announced

 vector<Group> Groups 

and is a global variable in the file where I use it. No external sources elsewhere, etc.

I can trace the error:

 _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 do not need to do anything with the copy constructor, since a shallow copy is sufficient for this. I don’t even allocate any space (but I made a reserve for the string to try).

I have no idea what the problem is!

I am running this code on OpenSuse 10.2 using gcc 4.1.2

I don't really want to update gcc due to backward compatibility issues ...

This code worked fine on my windows machine. I compiled it with gcc 3.4.5 mingw without any problems ...

help!

---... ---

: EDIT:

I output 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); 
+7
c ++ linux struct vector
source share
4 answers

Well...

Valgrind to the rescue! What caused me in valgrind magazine was this thing.

 Invalid write of size 4 ==4639== at 0x805BDC0: ChangeGroups() (articles.cpp:405) ==4639== by 0x80AC008: GeneralConfigChange() (config.cpp:4474) ==4639== by 0x80EE28C: teste() (main.cpp:2259) ==4639== by 0x80EEBB3: main (main.cpp:2516) 

At this point in the file I was doing this

 Groups[oldselected].subpage=SL.selected_code(); 

and what if oldselected was outside the bounds of the vector?

In this case, what was happening was that oldselected could be -1 ... and although at that moment it was not a failure, he wrote something else ...

I should probably start using the at () operator and check for an exception, or just check if there are "oldselected"> 0 and <Groups.size () [preferred solution].

So it came in handy to John and Josh for reminding me of courage.

I used it before, but I never needed anything significant (fortunately: D).

Interestingly, in windows I do not get this segfault. The problem was the same ... I suppose this has something to do with memory management and the compiler ... it really eludes me.

Thank you all for entering;)

Greetings

0
source share

As Neil said, you don't need a default constructor, copy constructor, or destructor:

  • std::string is cleared after itself, so your destructor is never needed.
  • The shallow copy constructor created by the compiler will work fine.
  • std::string::reserve not required, since std::string will dynamically allocate memory as needed, but this can provide performance.

The code you posted looks correct (and looks very simple and straightforward, so it's hard to figure out where the error might occur). Therefore, I suspect that you damaged memory elsewhere in your code and that vector<Group> is just a victim.

Try installing Valgrind (OpenSuse should provide a package for it) and run the application through it (from the command line, just run valgrind my-app ) to see if Valgrind can catch memory corruption.

+6
source share

You must definitely remove the destructor. C ++ will automatically call the destructor of all data members, and doing things to members who already have such a destructor is not necessary and may be unsafe.

But I do not see anything wrong with your code. You will have to post a little more. Try expanding the section (...) - show us all the code that includes the vector.

+1
source share

Memory errors like this can be caused by deleting the same memory twice or deleting memory that you did not receive from the new one. Mistakes often occur long before the fact in such places. As mentioned by DeadMG, install valgrind and look for other seemingly unrelated memory issues.

+1
source share

All Articles