调用“new”时出现分段错误在c++?
当尝试调用“new”来创建指针并将其推入向量时,我遇到了段错误。我将元素推送到向量中的代码是:
queue->push_back(new Box(gen_id, Interval(x_mid, x_end), Interval(y_mid-y_halfwidth, y_mid+y_halfwidth)));
基本上 Box 是一个类,构造函数只接受 3 个参数,Generation_id 和 2
Intervals 。我打印出了这个“push”之前和之后的向量内容, 之前:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.30908203125, -0.3087158203125 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.9510498046875, -0.95068359375 ]
之后:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ 8.9039208750109844342e-243, 6.7903818933216500424e-173 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.95123291015625, -0.95086669921875 ]
我不知道为什么会发生这种情况,但显然,有一个元素被损坏了。除了“push”之外,这两组输出之间没有其他代码,我使用 gdb 来确认这一点。另外,我检查了这 2 个 Intervals 变量,它们都给了我一个有意义的结果。
我的问题是:在什么情况下“新”会出现段错误?还是我的问题是由其他原因引起的?谢谢。
I got segfault when trying to call "new" to create a pointer and push it into a vector. The code that I push the element in the vector is:
queue->push_back(new Box(gen_id, Interval(x_mid, x_end), Interval(y_mid-y_halfwidth, y_mid+y_halfwidth)));
Basically Box is a Class and the constructor just take 3 arguments, generation_id
, and 2 Intervals
. I printed out the content in vector before and after this "push",
before:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.30908203125, -0.3087158203125 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.9510498046875, -0.95068359375 ]
after:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ 8.9039208750109844342e-243, 6.7903818933216500424e-173 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.95123291015625, -0.95086669921875 ]
I have no clue why does this happen, but apparently, there's one element got corrupted. There's no other codes between these two sets of output except that "push", and I used gdb to confirm that. Also, I checked those 2 Intervals
variables, both give me a result that make sense.
My questions is: in what situation does "new" get segfault? Or is my problem caused because of other stuff? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设它确实是
new
生成段错误,最常见的原因是堆损坏,通常是覆盖您不拥有的内存和/或双重删除的结果。如果您可以在 Linux 系统上运行,Valgrind 将是您的朋友。
Assuming it really is
new
generating the segfault, the most common cause would be a corrupted heap, typically a result of overwriting memory you don't own and/or a double delete.Valgrind will be your friend if you can run on a Linux system.
我怀疑
new
本身会给你带来段错误;问题可能出在其中一个构造函数中。尝试拆分那条巨大的线,并放入一些打印语句以准确查看问题出在哪里。I doubt that
new
itself is giving you the segfault; the problem is probably in one of the constructors. Try splitting up that giant line, and put in some print statements to see exactly where the problem is.