C++ - 结构体指针数组

3

我有一个小程序,其中有两个结构体: 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

为什么它会将所有新条目添加到数组的开头,同时保持其余部分为空? 为什么它没有检测到21已经被添加。

我一直在努力想弄清楚这个问题。非常感谢任何帮助。 感谢社区。

编辑 我已经修复了它,使它将元素添加到数组中并识别出id是否存在。

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

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

通过修改 add 方法。

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] = &p;?我没有时间写一个适当的答案,但你在编程职业生涯中还有很长的路要走... - nonsensickle
谢谢。我知道。我仍然只是一个本科生。 - this is a long display name
抱歉如果我表现得不够尊重或冒犯了您。看到新人开始学习编程是好事,很抱歉我没有时间来提供适当的帮助。欢迎加入我们! - nonsensickle
请阅读这篇博客文章,学习如何使用调试器。它们在未来将对您非常宝贵。 - nonsensickle
1个回答

4
Person p(a);
ppls[current] = &p;

这是一个问题。您正在存储指向本地变量的指针。您的程序可能会出现未定义行为。

请使用

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

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

改进建议

不清楚您的程序目标是什么,但使用以下方法可以使您的生活更加简单:

std::vector<Person> ppls;

代替
Person *ppls[MAX];

感谢您的帮助。我不能使用向量的原因,以及这个程序没有明确的目标的原因是因为我编写这个程序是为了理解一些我遇到困难的概念。在我的学校里,我们被教导使用结构体,并且我正在尝试一个练习。 - this is a long display name
我在修正错误后运行了程序。但是出现了分段错误,请参考我上面的编辑。 - this is a long display name
@thisisalongdisplayname,由于main函数中的最后一行代码中people.ppls[3]NULL,导致了段错误。 - R Sahu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接