【问题标题】:C++ - Array of struct pointersC++ - 结构指针数组
【发布时间】:2015-11-04 02:06:19
【问题描述】:

我有一个小程序,其中有两个结构: Person - 由 id 和基本方法组成 Ppl - 由一组人组成,他们有一些操作数组的方法。

struct Person {
 const int id;
 Person();
 Person(int a);
};

Person::Person(int a) : id(a) {}

这是 Person 结构及其方法。

const int MAX = 5;

设置数组长度限制

struct Ppl {
 static int current;   //Keeps track of current position in array
 Person *ppls[MAX];    //Creates an array of Person structure pointers
 void add(int a);      //Adds a person with id to the next available position
 //void remove(int b);
 int searchid(int c);  //Searches ppls for an id c.
 Ppl();                //Constructor
};

int Ppl::current = 0;  //Initializing static variable

void Ppl::add(int a) {
 int ret = searchid(a);          //Determine if id a already exists in ppls
 //cout << "Ret: " << ret << endl;
 if (ret > MAX) {                //If value returned is > MAX, value exists
  cout << "User " << a << " already exists" << endl;
 } else {                        //else, add it to the next available spot
  Person p(a);
  ppls[current] = &p;
  cout << "Added user: " << a << " at index: " << current << endl;
  current++;
 }
}

Ppl::Ppl() {
 current = 0;
 int i = 0;
 while (i < MAX) {    //Sets all Person pointers to NULL on initialization
  ppls[i] = NULL;
  cout << "ppls[" << i << "] is NULL" << endl;
  i++;
 }
}

int Ppl::searchid(int c) {
 int i = 0;
 bool found = false;
 while(i < MAX) {
  if (ppls[i] == NULL) {  //If NULL, then c wasn't found in array because
   break;                 //there is no NULL between available spots.
  } else {
    if (ppls[i]->id == c) {
     found = true;
   }
  }
  i++;
 }
 if (found == true) {
  return 10;     //A number higher than MAX
 } else {
  return 1;      //A number lower than MAX
 }
}

主要功能是:

int main() {
 Ppl people;
 people.add(21);
 cout << people.ppls[0]->id << endl;
 people.add(7);
 cout << people.ppls[0]->id << " ";
 cout << people.ppls[1]->id << endl;
 people.add(42);
 cout << people.ppls[0]->id << " ";
 cout << people.ppls[1]->id << " ";
 cout << people.ppls[2]->id << endl;
 people.add(21);
 cout << people.ppls[0]->id << " ";
 cout << people.ppls[1]->id << " ";
 cout << people.ppls[2]->id << " ";
 cout << people.ppls[3]->id << endl;
}

我得到的输出是:

ppls[0] is NULL
ppls[1] is NULL
ppls[2] is NULL
ppls[3] is NULL
ppls[4] is NULL
Added user: 21 at index: 0
21
Added user: 7 at index: 1
7 0
Added user: 42 at index: 2
42 0 0
Added user: 21 at index: 3
21 0 0 0

为什么要将所有新条目添加到数组的开头,而将其余条目保持为 NULL? 为什么没有检测到已经添加了 21。

我一直在疯狂地试图弄清楚这一点。任何帮助将非常感激。 非常感谢社区。​​p>

编辑 我已对其进行了修复,以便它将元素添加到数组中并识别 id 何时存在。

我通过添加析构函数对 Ppl 结构进行了更改:

Ppl::~Ppl() {
 int i = 0;
 while (i < MAX) {
  delete ppls[i];
  i++;
 }
}

并通过更改添加方法。

void Ppl::add(int a) {
 int ret = searchid(a);
 //cout << "Ret: " << ret << endl;
 if (ret > MAX) {
  cout << "User " << a << " already exists" << endl;
 } else {
  **Person *p = new Person(a);
  ppls[current] = p;**
  cout << "Added user: " << a << " at index: " << current << endl;
  current++;
 }
}

所以现在的输出是

ppls[0] is NULL
ppls[1] is NULL
ppls[2] is NULL
ppls[3] is NULL
ppls[4] is NULL
Added user: 21 at index: 0
21
Added user: 7 at index: 1
21 7
Added user: 42 at index: 2
21 7 42
User 21 already exists
Segmentation fault (core dumped)

什么是分段错误,我该如何解决? 再次感谢

【问题讨论】:

  • 您将堆栈变量的地址分配给指针数组...总是一个坏主意。 Person p(a); 然后ppls[current] = &amp;p;?我没有时间写一个正确的答案,但你的编程生涯还有很长的路要走......
  • 谢谢。而且我知道。我还只是个本科生
  • 对不起,如果我不屑一顾或侮辱了我。很高兴看到新人开始编程,很抱歉我没有时间提供适当的帮助。欢迎加入俱乐部!
  • 请阅读this blog post,学习how to use a debugger。它们在未来对您来说将是无价之宝。

标签: c++ arrays pointers struct


【解决方案1】:
Person p(a);
ppls[current] = &p;

是个问题。您正在存储指向局部变量的指针。您的程序会受到未定义行为的影响。

使用

Person* p = new Person(a);
ppls[current] = p;

确保删除Ppl的析构函数中的对象。

改进建议

尚不清楚您使用此程序的目标是什么,但您可以通过使用使您的生活变得更简单

std::vector<Person> ppls;

而不是

Person *ppls[MAX];

【讨论】:

  • 感谢您的帮助。我不能使用向量以及这个程序没有明确目标的原因是因为我编写这个程序是为了理解一些我遇到问题的概念。我们在学校被教导使用结构,我正在尝试一个练习。
  • 我在修复错误后运行了程序。我遇到分段错误请参阅我上面的编辑。
  • @thisisalongdisplayname,由于people.ppls[3]NULL,所以由于main 的最后一行导致分段错误。
猜你喜欢
  • 2019-05-09
  • 2018-04-21
  • 2013-04-18
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多