下面是我试图开始工作的代码。我想创建一个 BankTeller 对象数组。据我了解,当调用 new BankTeller() 时,它会创建一个指向该对象的指针,我将其保存到出纳员中。然后我尝试插入到那个 psotion,但是当我使用 gdb 单步执行并且程序崩溃时出现段错误。
BankTeller **tellers;
BankModel(int count){
srand (time(NULL)); //ignore this
*tellers = new BankTeller[count];
for(int i=0; i <= count; i++){
tellers[i] = new BankTeller();
}
return;
}
最佳答案
我认为你的代码比它可能需要的更复杂。我会考虑使用 std
容器,例如 std::vector
或更适合您需求的任何东西。通常,除非确实有必要,否则您应该避免使用多级间接寻址,而在本例中似乎没有必要。
理解指针
您首先声明 BankTeller **tellers
, 这是指向 BankTeller 的指针。在您的代码中导致段错误的行是 *tellers = new BankTeller[count];
.此行返回指向 BankTeller
数组的指针对象,但您的声明带有双 **
说它应该得到一个数组指针到BankTeller
对象。分配的值仍然被解释为一个地址(它不是)并最终尝试访问一个无效的内存位置,这会触发段错误。
它应该是 *tellers = new BankTeller*[count];
.注意 *
在左括号之前。此行为您提供指向 BankTeller
的指针数组。对象。
简单示例
为了说明,忘记 BankTeller
s 让我们回到原语。
#include <iostream>
using namespace std;
int main()
{
const size_t max = 3;
int **nums;
cout << "Creating arrays...";
nums = new int*[max]; // <<---- not: nums = new int[max];
for(size_t i = 0; i < max; ++i)
nums[i] = new int(i);
cout << "done" << endl;
cout << "Contents: ";
for(size_t i = 0; i < max; ++i)
cout << *nums[i] << ' '; // <<---- dereferenced twice to reach actual value
cout << endl;
cout << "Deleting arrays...";
for(size_t i = 0; i < max; ++i)
delete nums[i];
delete[] nums;
cout << "done" << endl;
return 0;
}
请注意,这与之前描述的情况相同。要运行它,请将代码放入名为 test.cpp
的文件中并使用这个 (GNU/Linux) 命令:
➜ /tmp g++ test.cpp -o test && ./test
Creating arrays...done
Contents: 0 1 2
Deleting arrays...done
➜ /tmp
如果您想在调试器中检查它,请添加 -ggdb
到 g++
上面的命令以确保将调试符号添加到二进制文件中。然后您可以使用 b <linenumber>
(例如 b 10
)设置断点和 p <variable_name>
, (例如 p nums
、 p *nums
等)打印地址和值。
但同样,您不需要像这样使用原始指针。您可以而且应该使用标准模板库中的容器。
重构的代码
我使用 std::vector
重写了下面的示例代码而不是双重指针。
#include <iostream>
#include <vector>
using namespace std;
class BankTeller
{
public:
BankTeller() {
cout << "Created BankTeller\n";
}
~BankTeller() {
cout << "Destroyed BankTeller\n";
}
};
class BankModel
{
public:
BankModel(size_t count) {
// remember to throw exception if count <= 0
for(size_t i = 0; i < count; ++i)
_tellers.push_back(new BankTeller());
cout << "Created BankModel\n";
}
~BankModel() {
// consider using iterators
for(size_t i = 0; i < _tellers.size(); ++i) {
delete _tellers[i];
_tellers[i] = 0;
}
_tellers.clear();
cout << "Destroyed BankModel\n";
}
private:
vector<BankTeller*> _tellers;
};
int main() {
BankModel *model = new BankModel(5);
delete model;
return 0;
}
在我的系统 (GNU/Linux) 中构建并运行它如下所示:
➜ /tmp g++ tellers.cpp -o tellers
➜ /tmp ./tellers
Created BankTeller
Created BankTeller
Created BankTeller
Created BankTeller
Created BankTeller
Created BankModel
Destroyed BankTeller
Destroyed BankTeller
Destroyed BankTeller
Destroyed BankTeller
Destroyed BankTeller
Destroyed BankModel
希望这能帮助您理解指针和使用 STL 的好处。
关于c++ - 创建对象数组时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33046638/