C++通过赋值复制问题
我似乎在使用以下函数时遇到问题:
void OtherClass::copy_this( int index, MyClass &class_obj)
{
if(index < MAX_index)
class_obj = array_of_MyClass[index];
}
OtherClass 维护 MyClass 对象的数组,我希望此函数将选定的对象从数组复制到提供的 class_obj 中。
当我运行时,程序在到达此函数时出现分段错误。在 gdb 中运行它并查看回溯表明,当它到达赋值行时,执行会向后跳转近 100 行,进入一个完全不同的函数的中间。它跳转到的行是:
temp_obj = array_of_MyClass[other_index]
gdb 回溯的相关输出是:
#0 0x0000003c7ae7256c in memcpy () from /lib64/tls/libc.so.6
#1 0x000000000043264e in MyClass::operator= (this=0x4c0000004c, _ctor_arg=@0x7fbffd8228) at ../location.cpp:156
#2 0x0000000000432569 in OtherClass::copy_this (this=0x7fbffd8220, index=0, section=@0x4c0000004c) at ../location.cpp:254
显然这是相同类型的操作,但到底为什么执行会这样移动?我在程序中的任何地方都没有长跳、跳转等。我也没有用户定义的赋值运算符、复制构造函数等,因此回溯中的“operator=”令人费解。
在有人问之前,不,我不能发布整个代码。 (抱歉!)我意识到这可能会导致无法识别我的问题;如果是这种情况,请告诉我。
提前致谢!
在再次运行它并测试了几个“最简单的用例”之后,似乎问题实际上是在执行的早期某个地方引入的,所以对我来说又回到了绘图板。谢谢大家的帮助!
I seem to be having trouble with the following function:
void OtherClass::copy_this( int index, MyClass &class_obj)
{
if(index < MAX_index)
class_obj = array_of_MyClass[index];
}
OtherClass maintains an array of MyClass objects, and I would like this function to copy a selected object out of the array into the provided class_obj.
When I run, the program has a segmentation fault when it reaches this function. Running it in gdb and looking at the backtrace reveals that when it hits the assignment line, execution jumps backwards almost 100 lines into the middle of a completely different function. The line it jumps to is:
temp_obj = array_of_MyClass[other_index]
And the relevant output from the gdb backtrace is:
#0 0x0000003c7ae7256c in memcpy () from /lib64/tls/libc.so.6
#1 0x000000000043264e in MyClass::operator= (this=0x4c0000004c, _ctor_arg=@0x7fbffd8228) at ../location.cpp:156
#2 0x0000000000432569 in OtherClass::copy_this (this=0x7fbffd8220, index=0, section=@0x4c0000004c) at ../location.cpp:254
Obviously it's the same type of operation, but why on earth would execution move like that? I have no longjumps, gotos, etc. anywhere in the program. I also do not have user-defined assignment operators, copy constructors, etc., so the "operator=" from the backtrace is puzzling.
Before anyone asks, no, I cannot post the whole code. (Sorry!) I realize that may make it impossible to identify my problem; if this is the case, just let me know.
Thanks in advance!
After running through it again and testing a couple of "simplest use" cases, it seems that the problem is actually introduced somewhere earlier in execution, so it's back to the drawing board for me. Thank you all for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该索引是否有效?我看到您与 MAX_index 进行比较,但是您的数组是否包含 MAX_index 初始化元素?我问这个问题是因为,如果您复制无效的对象,则会出现像您所描述的那样的未定义(令人不愉快)的行为。
另一种可能性(如果不是这个)是现在是进行全面重建的时候了。调试信息已损坏,或者生成的代码不完整(由于构建过程中的任何原因)。
Is the index a valid one? I see that you compare with MAX_index, but is your array containing MAX_index, initialized elements? I am asking because, if you copy an invalid object, you have undefined (unpleasant) behavior like you described.
Another possibility (if not this one) is that it is the time to make a full rebuild. Either the debug information is corrupted, or the generated code is incomplete (due to whatever reasons during the build process).
代码是否使用优化器标志编译?优化器可以对执行流程做非常奇怪的事情,包括从一个函数跳转到一个完全不相关的函数,而该函数恰好有一段代码可以执行第一个函数所需的操作。
Is the code compiled with optimiser flags? The optimiser can do very strange things to the flow of execution, including jumping from one function over to a completely unrelated function that just happens to have a snippet of code that does something the first function needs.
您所说的执行向后跳转的行是对 MyClass 类的对象的赋值。这就是为什么您会在回溯中看到
MyClass::operator=
。您说您没有用户定义的赋值运算符或复制构造函数,因此这可以解释回溯中其上方的 memcpy() ,因为这是复制的默认实现(浅复制)。
在这种情况下,我认为这可能会有所帮助实现一个复制构造函数(对于 Myclass)。
关于段错误本身 - 数组是如何初始化的?数组的索引 0 中有什么? (好像在通话中使用过)
the line you say execution jumps backwards from is an assignment to an object of class MyClass. This is why you see
MyClass::operator=
in your backtrace.You say you have no user defined assignment operators or copy constructors, so this would explain the memcpy() right above it in the backtrace, since that's the default implementation of copying (shallow copy.)
In this case I think it might be helpful to implement a copy constructor (for Myclass).
Regarding the segfault itself - how was the array initialized? what was in index 0 of the array? (which seems to have been used in the call)
您没有它们,但编译器无论如何都会生成它们,这就是您在其上使用 = 运算符时所调用的。最有可能的是默认的赋值运算符对于 MyClass 来说是不够的,这就是它不起作用的原因。明确地实施它们,看看这是否可以解决问题。
如果没有更多的源代码,就没有什么可以推荐的了。
You don't have them, but the compiler produces them anyway, which is what you called when ou used the = operator on it. What is most likely is that the default assignment operator is insufficient for MyClass, which is why it isn't working. Implement them explicitly and see if this solves the issue.
Without more source code, there's little more that can be recommended.
应该再注意几帧,我就会看到答案。
我尝试将数据复制到的 MyClass 对象已初始化为 NULL 指针,这就是 memcpy() 中的错误的来源。 (“无法访问地址 0x0 处的内存”——哦!)不敢相信我错过了......
谢谢你的帮助——如果我没有尝试过你的方法,我可能会继续用头撞墙。解决方案。明确地编写赋值运算符使我确信问题出在其他地方。
感谢全体会议!
Should've payed attention to a few more frames back, and I would have seen the answer.
The MyClass object I was trying to copy the data into had been initialized as a
NULL
pointer, which is where the error inmemcpy()
was coming from. ("Cannot access memory at address 0x0" -- d'oh!) Can't believe I missed that...Thanks for your help -- I probably would've continued beating my head against a wall had I not tried out your solutions. Explicitly writing the assignment operator convinced me that the problem was elsewhere.
Gratia plena!