Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:02:12+00:00 2026-05-27T02:02:12+00:00

Currently I’m trying to set up a member function for Student that reads a

  • 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;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:02:13+00:00Added an answer on May 27, 2026 at 2:02 am

    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);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently its /tmp How can I set it to /anythingelse so that all applications
Currently, my ListViews look like this: How can I achieve that Windows 7 native
Currently I am trying to render a .obj model that I loaded into vectors.
Currently I am displaying a UIView that slides down from the top of my
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently, I am developing a product that does fairly intensive calculations using MS SQL
Currently I using the following code to get my JSON output from MySQL. <?php
Currently am developing a program that solves (if possible) any given labyrinth of dimensions
Currently I have some legacy ASP.NET 2.0 code that uses the ASP Xml web
Currently I have a service that uses a UserNamePasswordValidator to authenticate the client user.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.