修复:访问冲突读取位置(指向字符串数组的指针)
FIXED: Access Violation Reading Location (pointer to string array)
已修复:http://pastebin.com/71QxqGk5
第一post/question.
所以这是 C++,我正在尝试打印一个单词数组。
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cctype>
#include <ctime>
using namespace std;
//structs
struct Input
{
int size;
string* word;
bool is_palindrome[];
};
//prototypes
bool openInputFile(ifstream &ifs);
void File_to_Array(string* word, int &size);
void PrintArray(string* word, int size);
//main
int main()
{
Input myInput = { 0, nullptr, false };
File_to_Array(myInput.word, myInput.size);//copy arr and get size
cout << myInput.word; //this outputs 00000000
cout << *myInput.word; //this breaks and throws exception as commented below
//Exception thrown at 0x0098BB6B in Project1.exe: 0xC0000005: Access violation reading location 0x00000014.
PrintArray(myInput.word, myInput.size);//print array of strings
system("PAUSE");
return 0;
}
//functions
bool openInputFile(ifstream &ifs)
{
string filename;
cout << "Enter the input filename: " << endl;
getline(cin, filename);
ifs.open(filename.c_str());
return ifs.is_open();
}
void File_to_Array(string* word, int &size)//copies file to dyn arr and assigns size from first elem
{
ifstream myFile;
while (!openInputFile(myFile))
cout << "Could not open file" << endl;
string tempstr = "";
getline(myFile, tempstr);//first line is size of dyn arr
size = stoi(tempstr);//now we have max size of dyn arr of strings
word = new string [size];//now we have the array of strings, *word[index] = string1
int i;
for (i = 0; getline(myFile, word[i]) && i < size; ++i);//for each line
//copy line of string from file to string arr within "bool" test, second param of for loop //copying done
size = i;
myFile.close();//done with file, no need, close it
}
void PrintArray(string* word, int size)
{
//for (int i = 0; i < size; ++i)
//cout used to be here, but now its in main, for debugging
}
所以我想知道我的问题是否与传递结构成员有关,以及我是否应该将整个结构类型 "myInput" 传递给函数并使用 -> 运算符访问myInput 的成员。
下面是一个文本文件的例子
5
month
Runner
NEON
digit
ferret
nothing
5 是动态分配数组的大小,其余是字符串,如您所见,有 6 个字符串,所以我在 for 循环中测试文件是否仍在将字符串传输到数组。
File_to_Array
的这一部分导致了问题:
word = new string [size];
您认为您正在将 myInput
对象的指针设置为指向字符串数组,但您不是。当您将指针传递给此处的函数时:
File_to_Array(myInput.word, myInput.size)
^^^^^^^^^^^^
您实际上传递的是指针的副本。所以在File_to_Array
里面,这个副本被重新指向了新创建的字符串数组,但是myInput
里面真正的指针并没有改变。您应该改为传递对指针的引用:
void File_to_Array(string*& word, int &size)
\___________/
^--reference to a pointer
我还建议您改用 vector[string]
。最后,您的 bool is_palindrome[];
成员及其初始化看起来很奇怪,但很难进一步评论,因为它们从未在代码中使用过。
已修复:http://pastebin.com/71QxqGk5
第一post/question.
所以这是 C++,我正在尝试打印一个单词数组。
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cctype>
#include <ctime>
using namespace std;
//structs
struct Input
{
int size;
string* word;
bool is_palindrome[];
};
//prototypes
bool openInputFile(ifstream &ifs);
void File_to_Array(string* word, int &size);
void PrintArray(string* word, int size);
//main
int main()
{
Input myInput = { 0, nullptr, false };
File_to_Array(myInput.word, myInput.size);//copy arr and get size
cout << myInput.word; //this outputs 00000000
cout << *myInput.word; //this breaks and throws exception as commented below
//Exception thrown at 0x0098BB6B in Project1.exe: 0xC0000005: Access violation reading location 0x00000014.
PrintArray(myInput.word, myInput.size);//print array of strings
system("PAUSE");
return 0;
}
//functions
bool openInputFile(ifstream &ifs)
{
string filename;
cout << "Enter the input filename: " << endl;
getline(cin, filename);
ifs.open(filename.c_str());
return ifs.is_open();
}
void File_to_Array(string* word, int &size)//copies file to dyn arr and assigns size from first elem
{
ifstream myFile;
while (!openInputFile(myFile))
cout << "Could not open file" << endl;
string tempstr = "";
getline(myFile, tempstr);//first line is size of dyn arr
size = stoi(tempstr);//now we have max size of dyn arr of strings
word = new string [size];//now we have the array of strings, *word[index] = string1
int i;
for (i = 0; getline(myFile, word[i]) && i < size; ++i);//for each line
//copy line of string from file to string arr within "bool" test, second param of for loop //copying done
size = i;
myFile.close();//done with file, no need, close it
}
void PrintArray(string* word, int size)
{
//for (int i = 0; i < size; ++i)
//cout used to be here, but now its in main, for debugging
}
所以我想知道我的问题是否与传递结构成员有关,以及我是否应该将整个结构类型 "myInput" 传递给函数并使用 -> 运算符访问myInput 的成员。
下面是一个文本文件的例子
5
month
Runner
NEON
digit
ferret
nothing
5 是动态分配数组的大小,其余是字符串,如您所见,有 6 个字符串,所以我在 for 循环中测试文件是否仍在将字符串传输到数组。
File_to_Array
的这一部分导致了问题:
word = new string [size];
您认为您正在将 myInput
对象的指针设置为指向字符串数组,但您不是。当您将指针传递给此处的函数时:
File_to_Array(myInput.word, myInput.size)
^^^^^^^^^^^^
您实际上传递的是指针的副本。所以在File_to_Array
里面,这个副本被重新指向了新创建的字符串数组,但是myInput
里面真正的指针并没有改变。您应该改为传递对指针的引用:
void File_to_Array(string*& word, int &size)
\___________/
^--reference to a pointer
我还建议您改用 vector[string]
。最后,您的 bool is_palindrome[];
成员及其初始化看起来很奇怪,但很难进一步评论,因为它们从未在代码中使用过。