segfault on vector< struct> [英] segfault on vector<struct>

查看:141
本文介绍了segfault on vector< struct>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个结构来保存一些数据,然后声明一个向量来保存该结构。

I created a struct to hold some data and then declared a vector to hold that struct.

但是当我做一个push_back时,我得到了damf segfault,为什么!

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

我的结构体定义为:

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*/
 };

最初,struct没有副本,构造函数或析构函数。我在下面阅读这篇文章时添加了他们。

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.

有一个这是困扰我的地狱!当我第一次将一些数据推入向量时,一切都很好。后来在代码中,当我尝试将一些更多的数据推入向量,我的应用程序只是segfaults!

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!

向量被声明

vector<Group> Groups

并且是我使用它的文件的全局变量。在其他地方没有externs等。

and is a global variable to the file where I am using it. No externs anywhere else, etc...

我可以追踪错误:

_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!

我使用gcc 4.1.2在OpenSuse 10.2上运行此代码

I'm running this code on OpenSuse 10.2 with gcc 4.1.2

我不是真的渴望升级gcc因为向后兼容性问题...

I'm not really to eager to upgrade gcc because of backward compatibility issues...

这个代码在我的Windows机器上工作完美。我用gcc 3.4.5 mingw编译它没有任何问题...

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

help!

--- ... ---

--- ... ---

::: EDIT :::

:::::

我推送数据

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);


推荐答案

好...

valgrind救命!在valgrind日志中给我的是这个。

valgrind to the rescue! What called out to me in the valgrind log was this piece.

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)

此时在文件中我这样做

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

如果oldselected超出向量的范围怎么办?

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

在这种情况下,发生的是oldselected可能是-1 ...虽然这不是崩溃在这一点,它是写的东西在别的地方...

In this case what was happening was that oldselected could be -1... and although this wasn't crashing at this point, it was writing something somewhere else...

我应该可以开始使用at()运算符并检查异常或只是检查oldselected是否> 0和< Groups.size()[首选解决方案]。

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

因此,我们要向John和Josh提醒我valgrind。

So kudos to John and Josh for reminding me of valgrind.

我已经使用它,但从来不需要做任何东西与它的重要(幸运的是:D)。

I've used it before, but never needed to do anything t significant with it (fortunately :D).

有趣的是,在Windows中我不会这个segfault。问题是一样的...我想它与内存管理和编译器有关系...它真的让我失望。

It's interesting that in windows I don't get this segfault. The problem was the same... I guess that it has something to do with memory management and the compiler... it really eludes me.

感谢大家的输入;)

干杯

这篇关于segfault on vector&lt; struct&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆