0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误

0xC0000005: Access violation writing location 0xCDCDCDCD Dynamic Allocation Error

本文关键字:0xCDCDCDCD 动态分配 错误 位置 访问冲突 0xC0000005      更新时间:2023-10-16

获取0xC0000005:使用以下代码访问违规写入位置0xCDCDCDCD。 我知道我一定没有正确分配指针,但我不确定在哪里。

我试图**scoreSet引用*scores集,*scores是手动输入的。指向数组的names指针工作正常,似乎分配正确。问题是当我尝试模仿相同的scoreSet时,区别在于scoreSet是指向指针数组的指针scores。我觉得我尝试动态分配此指针指向的数组的方式是完全错误的。

基本上试图在用户输入后获得这样的东西: scoreSet0 = {22,33,44} scoreSet1 = {35, 45, 65, 75} scoreSet3 = {10}

#include <iostream>
#include <string>
using namespace std;
int inputData(string*& names, double**& scores);
int main() {
string *names = nullptr;
double *scores = nullptr;
double **scoreSet = &scores;
int size = 0;
size = inputData(names, scoreSet);
for (int i = 0; i < size; i++) {
cout << *(names+i) << endl;
}
}
int inputData(string*& names, double**& scoreSet) {
int numStudents = 0;
cout << "How many students do you have in the system? ";
cin >> numStudents;
while (numStudents <= 0) {
cout << "Invalid number of students. Please enter number of students: ";
cin >> numStudents;
}
names = new string[numStudents];
cin.ignore(10000, 'n');
for (int i = 0; i < numStudents; i++) {
int numTests = 0;
cout << "Enter the student's name: ";
getline(cin,names[i]);
cout << "Enter how many tests " << *(names + i) << " took: ";
cin >> numTests;
*(scoreSet + i)= new double[numTests];                   //Pretty sure this is wrong.
cin.ignore(10000, 'n');
for (int j = 0; j < numTests; j++) {                //This loop is causing the error.
cout << "Enter grade #" << j + 1 << ": ";
cin >> *(scoreSet+i)[j];
}
}
return numStudents;
}

根据PaulMcKenzie的建议,这就是它的滚动方式。使用模板对您来说可能有点多,但如果可以的话......否则,请分别创建名称和评分容器。但是,您需要维护重复的代码。

这个想法是将你所有的东西都保持某种顺序。请注意,现在内存管理在container中处理。

我放弃了处理std::cin和分数,但你应该更容易编码这些东西,而不会有很多绒毛。在那个,没有std::cin的情况下开发,这是浪费时间。你应该写,这样你就可以编辑和运行。

另外,摆脱using namespace std;的习惯 从长远来看,它会得到回报。

#define DEV
template<typename T>
struct container {
size_t size;
T* ar;
container(size_t size) :size(size) {
ar = new T[size];
}
~container() { delete[]ar; }
T& operator [](size_t pos) { return ar[pos]; }
};
using names_c = container<std::string>;
using scores_c = container<double>;
size_t inputData(names_c& names, scores_c& scores);
int main() {
container<std::string> names(2);
container<double> scoreSet(2);
auto size = inputData(names, scoreSet);
for (int i = 0; i < size; i++) {
std::cout << names[i] << endl;
}
}
size_t inputData(names_c& names, scores_c& scores) {
#ifdef DEV
size_t numStudents = 2;
names[0] = "tom";
names[1] = "mary";
#else
//do your std::cin stuff
#endif
return names.size;
}

我不打算去那里,但是。您可以扩展该概念,以便在容器中具有容器。更容易知道什么分数与哪个学生在一起。

struct student_type {
using scores_c = container<double>;
std::string name;
scores_c scores;
};
using student_c = container<student_type>;

我已经接受了您的代码,并对其进行了修改以使其正常工作。我已经删除了您的评论,并在我更改的行上放置了评论。

#include <iostream>
#include <string>
using namespace std;
int inputData( string *&names, double **&scores );
int main() {
string *names = nullptr;
double **scores = nullptr; // Changed to double ** so it's "2D"
// double **scoreSet = &score; // removed, this was unneeded and probably causing problems
int size = 0;
size = inputData( names, scores );
for ( int i = 0; i < size; i++ ) {
cout << *( names + i ) << endl;
}
}
int inputData( string *&names, double **&scoreSet ) {
int numStudents = 0;
cout << "How many students do you have in the system? ";
cin >> numStudents;
while ( numStudents <= 0 ) {
cout << "Invalid number of students. Please enter number of students: ";
cin >> numStudents;
}
names = new string[numStudents];
scoreSet = new double*[numStudents]; // allocate an array of pointers
// cin.ignore( 10000, 'n' ); // Removed from here, placed inside loop
for ( int i = 0; i < numStudents; i++ ) {
cin.ignore( 10000, 'n' ); // placed here so that it always clears before getting the name
int numTests = 0;
cout << "Enter the student's name: ";
getline( cin, names[i] );
cout << "Enter how many tests " << names[i] << " took: "; // simplified
cin >> numTests;
scoreSet[i] = new double[numTests]; // simpliefied left hand side
//cin.ignore( 10000, 'n' ); // not needed
for ( int j = 0; j < numTests; j++ ) {
cout << "Enter grade #" << j + 1 << ": ";
cin >> scoreSet[i][j]; // simplified
}
}
return numStudents;
}