简体   繁体   English

线程1:EXC_BAD_ACCESS(code = 2,address = 0x8)错误c ++

[英]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 . 另外,您永远不会为nextnext->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].next0 ,由calloc初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x8 - C++ THREAD 1: EXC_BAD_ACCESS(code=1, address =0x8 线程 1:EXC_BAD_ACCESS(代码=1,地址=0x8) - Thread 1: EXC_BAD_ACCESS (code=1, address=0x8) 迷宫构造函数问题 [线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)] - Maze Constructor Issues [Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)] C ++节点分配错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0) - C++ node assign error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 我有一个线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x8)错误。 我认为这是由于不良的内存管理。 我可以采取什么措施来防止这种情况发生? - I have a Thread 1:EXC_BAD_ACCESS (code =1, address=0x8) error. I think its due bad memory management. What are some steps I can take to prevent this? 线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)错误 - Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error c ++线程:EXC_BAD_ACCESS(Code = 2,address0x - c++ Thread: EXC_BAD_ACCESS(Code=2, address0x C ++:尝试将新节点添加到链接列表会产生“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)”错误 - C++: Attempting to add new node to linked list yields “Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)” error 线程 1:EXC_BAD_ACCESS (code=1) 错误 c++ 项目 - Thread 1: EXC_BAD_ACCESS (code=1) error c++ project 为什么这个递归例程给出 EXC_BAD_ACCESS (code=1, address=0x8)? - Why is this recursive routine giving EXC_BAD_ACCESS (code=1, address=0x8)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM