[英]Thread 1: EXC_BAD_ACCESS ( code=2,address=0x8) Error c++
I'm developing c++ application . 我正在开发c ++应用程序。 I have allocated memory but im getting the Error
Thread 1: EXC_BAD_ACCESS ( code=2,address=0x8)
in superfile.cpp. 我已经分配了内存,但是我在
Thread 1: EXC_BAD_ACCESS ( code=2,address=0x8)
得到了错误Thread 1: EXC_BAD_ACCESS ( code=2,address=0x8)
。 Here is my code : 这是我的代码:
superfile.h superfile.h
struct Node{
Voxel *data;
Node *next;
};
superfile.cpp superfile.cpp
int* cnt =(int*)calloc(_width*_height,sizeof(int));
Voxel *temp =(Voxel *)calloc(_width*_height,sizeof(Voxel));
Node *list=(Node *)calloc(_width*_height*2,sizeof(Node));
list[(_width*_height)+l].next = list[_width*yy + xx].next->next; // Thread 1: EXC_BAD_ACCESS ( code=2,address=0x8) Error c++
after debugging the values of the variables are: 调试后,变量的值是:
_width=60
_height=45
l=3
yy=4096
xx=-3345
Any idea what is going on ? 知道发生了什么吗? Thank you
谢谢
You're allocating not enough memory. 您分配的内存不足。 Here, the size of
list
is 60*45*2=5400 elements. 在此,
list
的大小为60 * 45 * 2 = 5400个元素。 You're trying to access the 60*4096-3345=242415th element. 您正在尝试访问60 * 4096-3345 = 242415th元素。
That's access to memory that doesn't belong to the memory associated with list
. 这是对不属于与
list
关联的内存的内存的访问。 The 242415th element doesn't exist . 第242415个元素不存在 。 That's SegmentationFault.
那就是SegmentationFault。
You'll need to use something like calloc(_width*_height*100,sizeof(...));
您将需要使用
calloc(_width*_height*100,sizeof(...));
to handle this. 处理这个。 However, you'll waste lots of memory then.
但是,那会浪费大量的内存。
Also, you never allocate memory for next
and next->next
. 另外,您永远不会为
next
和next->next
分配内存。 Try this 尝试这个
list[_width*yy + xx].next=calloc(50, sizeof(...));
list[_width*yy + xx].next->next=calloc(50, sizeof(...));
list[(_width*_height)+l].next = list[_width*yy + xx].next->next;
Here 50
is just random number, I'm not sure how much space does your struct
consume. 这里的
50
只是随机数,我不确定您的struct
会消耗多少空间。
You're dereferencing a null pointer here: 您在这里取消引用空指针:
list[(_width*_height)+l].next = list[_width*yy + xx].next->next;
^^^^^^
The value at list[_width*yy + xx].next
is 0
, as initialized by calloc
. list[_width*yy + xx].next
是0
,由calloc
初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.