为什么这会产生 bad_alloc 错误?

发布于 2024-12-16 23:55:08 字数 800 浏览 0 评论 0原文

目前,我正在尝试为 Student 设置一个成员函数,该函数从 cin 读取字符串,用作该函数的参数,然后使用数据创建一个 Student 对象。但是,它是否给我一个 bad_alloc 错误?我知道该函数正在获取字符串,但在创建新对象后它给出了此错误。

错误:

./a.out

Please insert name for student:
Bob_Russel
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted

构造函数:

Student::Student(string tname){

  name = tname;

}

函数:

Student Student::readStudent(istream &i){

  Student *stud;

  string y;

  i >> y;

  stud = new Student(y);

  return  *stud;

}

testStudent.cpp:

 #include "Student.h"

int main(){

  Student *stud3;
  cout << "\nPlease insert name for student:\n";

  stud3->readStudent(cin);


return 0;

}

Currently I'm trying to set up a member function for Student that reads a string from cin, is used as an argument for this function and then creates a Student object with the data. However, is it giving me a bad_alloc error. I know the function is getting the string but it gives this error after the new object is created.

Error:

./a.out

Please insert name for student:
Bob_Russel
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted

Constructor:

Student::Student(string tname){

  name = tname;

}

Function:

Student Student::readStudent(istream &i){

  Student *stud;

  string y;

  i >> y;

  stud = new Student(y);

  return  *stud;

}

testStudent.cpp:

 #include "Student.h"

int main(){

  Student *stud3;
  cout << "\nPlease insert name for student:\n";

  stud3->readStudent(cin);


return 0;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九命猫 2024-12-23 23:55:08

代码不仅会泄漏内存(在 readStudent 中创建一个永远不会被删除的新 Student),而且在 main 中,您使用了一个未初始化的指针调用readStudent。这可能会损坏您的堆,从而导致对 new 的调用引发 std::bad_alloc

再看一下 C++ 内存管理和对象生命周期。这里实际上根本不需要使用指针。作为起点,您的 main 可以修改为:

int main() {
    Student stud3;
    std::cout << "Please insert name for student:" << std::endl;
    stud3.readStudent(std::cin);
}

如果您在 main 中读取名称(作为 std:: string),然后将名称直接传递给 Student 构造函数:

int main() {
    std::cout << "Please insert name for student:" << std::endl;
    // Read in the name.
    std::string name;
    std::cin >> name;
    // Create the student with the input name.
    Student stud3(name);
 }

Not only does the code leak memory (creating a new Student in readStudent that is never deleted), in main you are using an uninitialized pointer to call readStudent. Possibly this is corrupting your heap such that the call to new throws a std::bad_alloc.

Take another look at C++ memory management and object lifetimes. There is really no need to use pointers at all here. As a starting point, your main could be modified to this:

int main() {
    Student stud3;
    std::cout << "Please insert name for student:" << std::endl;
    stud3.readStudent(std::cin);
}

It would perhaps also be better if you read in the name within main (as a std::string), and then pass the name directly to the Student constructor:

int main() {
    std::cout << "Please insert name for student:" << std::endl;
    // Read in the name.
    std::string name;
    std::cin >> name;
    // Create the student with the input name.
    Student stud3(name);
 }
空名 2024-12-23 23:55:08

看起来您正在尝试实现工厂方法。如果是这种情况,那么您缺少 static 关键字和 readStudent 调用的正确语法。

class Student{
public:
    Student(std::string tname);
    static Student* readStudent(std::istream &i);
private:
    std::string name
};

Student::Student(std::string tname) {
    name = tname;
}

Student* Student::readStudent(std::istream &i){
    std::string y;
    i >> y;
    return new Student(y);
}

int main(int argc, char* argv[]){
    Student *stud3 = NULL;

    std::cout << "\nPlease insert name for student:\n";

    stud3 = Student::readStudent(cin);

    return 0;
}

It looks like you're trying to implement a factory method. If that's the case, then you're missing the static keyword and the correct syntax for the readStudent call.

class Student{
public:
    Student(std::string tname);
    static Student* readStudent(std::istream &i);
private:
    std::string name
};

Student::Student(std::string tname) {
    name = tname;
}

Student* Student::readStudent(std::istream &i){
    std::string y;
    i >> y;
    return new Student(y);
}

int main(int argc, char* argv[]){
    Student *stud3 = NULL;

    std::cout << "\nPlease insert name for student:\n";

    stud3 = Student::readStudent(cin);

    return 0;
}
所谓喜欢 2024-12-23 23:55:08

您使用 new 在堆上分配并且从不释放它,因此您耗尽了内存并得到了 bad_alloc。对于每个都应该有一个删除

这不会抛出 bad_alloc:

Student Student::readStudent(std::istream& i)
{        
   std::string y;    
   i >> y;    
   return Student(y);    
}

You are allocating on the heap using new and never freeing it, thus you run out of memory and get a bad_alloc. For every new there should be a delete.

This will not throw bad_alloc:

Student Student::readStudent(std::istream& i)
{        
   std::string y;    
   i >> y;    
   return Student(y);    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文