gpt4 book ai didi

c++ - 结构指针数组,出现段错误

转载 作者:行者123 更新时间:2023-11-30 05:44:32 25 4
gpt4 key购买 nike

我得到以下段错误:

我的类.h

class myclass
{
struct stuSomething
{
...
stuSomething(){...}
};

public:
static myclass* Instance()
{
if (!instance)
new myclass();
return instance;
}
void myclass:someFun();

private:
static myclass *instance;
myclass();
stuSomething *stuStack[SAMPLE_QUANTITY];
};

我的类.cpp

myclass* myclass::instance;
myclass::myclass()
{
for(int i = 0; i < SAMPLE_QUANTITY; i++)
this->stuStack[i] = NULL;
instance = this;
}

myclass::someFun()
{
for(int i = 0; i < SAMPLE_QUANTITY; i++)
if(this->stuStack[i] != NULL) // I get segfault here! for i = 0
...
}

但奇怪的是,当我把

for(int i = 0; i < SAMPLE_QUANTITY; i++)
if(this->stuStack[i] != NULL)
...

在我填写 stuStack 后立即在构造函数中,我没有遇到段错误。

我觉得我明显遗漏了什么。是什么导致了问题?

最佳答案

检查this指针,它可能无效。

关于c++ - 结构指针数组,出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com