[英]Passing pointer array to function
i have the following situation. 我有以下情况。 the following program although it compiles just fine when i run it, it stops working.
以下程序虽然在我运行时可以正常编译,但是它停止工作。 can anyone please help me to find the problem?
谁能帮我找到问题吗? i think i'm using the wrong pointer into the function but i don't know how to fix it and make it work
我认为我在函数中使用了错误的指针,但我不知道如何修复它并使它起作用
#include <fstream>
//some other includes
using namespace std;
struct Book{
string id;
string title;
string authorName;
string authorSurname;
};
int load(Book* booksInfo)
{
int count = 0;
ifstream fin;
fin.open("myfile.txt");
if (!fin.is_open())
{
cout << "Unable to open myfile.txt file\n";
exit(1);
}
while (fin.good())
{
getline(fin, booksInfo[count].id, '#');
getline(fin, booksInfo[count].title, '#');
getline(fin, booksInfo[count].authorName, '#');
getline(fin, booksInfo[count].authorSurname, '#');
count++;
} //end while
fin.close();
return 0;
} //end load()
//some other functions here
int main()
{
Book * bookInfo;
bookInfo = (Book*) malloc(sizeof(Book)*100);
//some code here
load(bookInfo);
//some code here
return 0;
} //end main
It is UB to use malloc
to allocate non POD types, in your case book instances will contain some garbage in strings, because there was no std::string
constructor called. 使用
malloc
分配非POD类型是UB,在您的情况下,书实例将在字符串中包含一些垃圾,因为没有调用std::string
构造函数。 And it won't be just garbage strings, it will be most likely garbage pointer pointing to some random locations. 它不只是垃圾字符串,更可能是指向某些随机位置的垃圾指针。
You should use std::vector
or at least new
if you really need to allocate memory manually, to create new Book
instances in a heap. 如果确实需要手动分配内存,则应使用
std::vector
或至少使用new
,以在堆中创建新的Book
实例。
And if you really, really must use malloc
, you can use placement new
to create valid std::string
s in raw memory you have allocated somehow (by malloc
in your case). 如果确实需要使用
malloc
,则可以使用placement new
在以某种方式分配的原始内存中创建有效的std::string
s(在您的情况下,通过malloc
)。
Use std::vector
to store your list of books: 使用
std::vector
来存储您的书籍清单:
#include <fstream>
#include <vector>
//some other includes
using namespace std;
struct Book{
string id;
string title;
string authorName;
string authorSurname;
};
vector<Book> load()
{
ifstream fin;
Book book;
vector<Book> books;
fin.open("myfile.txt");
if (!fin.is_open())
{
cout << "Unable to open myfile.txt file\n";
return books;
}
while (fin.good())
{
getline(fin, book.id, '#');
getline(fin, book.title, '#');
getline(fin, book.authorName, '#');
getline(fin, book.authorSurname, '#');
books.push_back(book);
} //end while
fin.close();
return books;
} //end load()
//some other functions here
int main()
{
vector<Book> books = load();
return 0;
} //end main
You need to use 您需要使用
Book* bookInfo = new Book[100];
instead. 代替。 This is because, in C++, a
struct
is an object (just like a class
), and calling malloc on anything other than plain old data is undefined behaviour . 这是因为,在C ++中,
struct
是一个对象(就像class
),并且对除普通旧数据以外的任何其他对象调用malloc都是未定义的行为 。
Remember to free your memory using delete[] bookInfo;
请记住使用
delete[] bookInfo;
释放内存delete[] bookInfo;
(note carefully the square brackets). (请注意方括号)。 If you use
delete
on it's own, that's a little more undefined behaviour . 如果您自己使用
delete
,那将是未定义的行为 。
Also make sure that you don't read more than 100 lines; 另外,请确保阅读的内容不超过100行; or you'll overflow the array: yet more undefined behaviour .
否则您将溢出数组:还有更多未定义的行为 。
Finally, consider using a standard template library container like std::vector
. 最后,考虑使用标准模板库容器,如
std::vector
。
What about: 关于什么:
Book bookInfo[100];
This avoids heap allocation altogether and should serve your purposes. 这完全避免了堆分配,应该可以满足您的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.