检查下列代码:#include<stdio.h> #include<string> #include<cstdlib> #include<ctime> #include <iostream> using namespace std; #define stepLength 3 //定义学生信息长度的增长步长 struct student {//简单起见只简单包含学生名字(字符串)和年龄(整型) char name[20];//姓名 int age;//年龄 }; void add(string, int, student*&, int&, int&); int main() { int max = 5; //定义当前学生信息的最大长度 int counter = 0; //当前仓库中的学生信息数目 student* storage; //定义学生信息的存储仓库 storage = new student[max];;//初始化仓库,申请能存储max个学生信息结构体的空间 srand(time(NULL)); for (int i = 0; i <= 9; i++) {//添加10个学生信息 string name = "姓名" + to_string(i);//姓名为“姓名”+“i”的形式,使用to_string(i),将i转换为字符。 int age = rand() % 5 + 17;//年龄使用生成一个17到21之间的随机整数。 add(name, age,storage,counter,max);//调用函数add,进行信息的添加。 } //打印学生的信息: cout << "+--------------学生信息列表 ------------+" << endl; for (int i = 0;i < counter;i++) { cout << "姓名:" << storage[i].name << ",年龄:" << storage[i].age << endl; } delete[] storage;//释放申请的空间。 return 0; } void add(string name, int age,student*&storage,int & counter,int & max) { if (counter < max) { //判断当前学生信息的数目,若小于仓库大小,说明还有空间,则直接添加学生信息 student s; strcpy(s.name, name.c_str()); s.age = age; storage[counter] = s; counter++;//学生信息数加1. cout << "学生-->name: " << name << ", 年龄: " << age << "添加完毕..." << endl; } else {//否则,说明空间不够了,添加不进去了 cout << "空间不足,正在申请空间..." << endl; max += stepLength;//增大空间容量,首先将max增加个stepLength的长度。 //通过,max申请更大的新空间 student* newStorage = new student[max]; for (int i = 0; i < counter; i++) {//将原来的数据复制到新申请的空间中 newStorage[i] = storage[i]; } delete[] storage;//释放原来的空间,否则将造成内存泄漏 storage = newStorage;//将仓库storage指向新申请的空间 cout << "空间申请完毕,正在添加学生信息..." << endl; add(name, age,storage,counter,max);//再添加刚刚添加不进去的学生信息 } }
时间: 2024-03-03 11:47:58 浏览: 75
该代码存在两个问题:
1. 头文件引用不正确。应该将头文件<cstdlib>和<ctime>改为<stdlib.h>和<time.h>。
2. 在函数add的参数中,应该将student*& storage改为student** storage,否则在函数中对storage进行内存重新分配后,外部的storage仍然指向旧的内存地址,导致后续访问时出现错误。
以下是修改后的代码:
```
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
#define stepLength 3 //定义学生信息长度的增长步长
//定义学生结构体
struct student {
char name[20]; //姓名
int age; //年龄
};
//函数声明
void add(string, int, student**, int&, int&);
int main() {
int max = 5; //定义当前学生信息的最大长度
int counter = 0; //当前仓库中的学生信息数目
student** storage; //定义学生信息的存储仓库
storage = new student*[max];//初始化仓库,申请能存储max个学生信息结构体的空间
srand(time(NULL));
for (int i = 0; i <= 9; i++) {//添加10个学生信息
string name = "姓名" + to_string(i);//姓名为“姓名”+“i”的形式,使用to_string(i),将i转换为字符。
int age = rand() % 5 + 17;//年龄使用生成一个17到21之间的随机整数。
add(name, age, storage, counter, max);//调用函数add,进行信息的添加。
}
//打印学生的信息:
cout << "+--------------学生信息列表 ------------+" << endl;
for (int i = 0;i < counter;i++) {
cout << "姓名:" << storage[i]->name << ",年龄:" << storage[i]->age << endl;
}
//释放申请的空间。
for (int i = 0;i < counter;i++) {
delete storage[i];
}
delete[] storage;
return 0;
}
void add(string name, int age, student**& storage, int& counter, int& max) {
if (counter < max) {//判断当前学生信息的数目,若小于仓库大小,说明还有空间,则直接添加学生信息
student* s = new student;
strcpy(s->name, name.c_str());
s->age = age;
storage[counter] = s;
counter++;//学生信息数加1.
cout << "学生-->name: " << name << ", 年龄: " << age << "添加完毕..." << endl;
}
else {//否则,说明空间不够了,添加不进去了
cout << "空间不足,正在申请空间..." << endl;
max += stepLength;//增大空间容量,首先将max增加个stepLength的长度。
//通过,max申请更大的新空间
student** newStorage = new student*[max];
for (int i = 0; i < counter; i++) {//将原来的数据复制到新申请的空间中
newStorage[i] = storage[i];
}
delete[] storage;//释放原来的空间,否则将造成内存泄漏
storage = newStorage;//将仓库storage指向新申请的空间
cout << "空间申请完毕,正在添加学生信息..." << endl;
add(name, age, storage, counter, max);//再添加刚刚添加不进去的学生信息
}
}
```
阅读全文
相关推荐


















