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 7927713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:28:00+00:00 2026-06-03T19:28:00+00:00

The addStudent function and mainMenu function are the two that are important. #include <iostream>

  • 0

The addStudent function and mainMenu function are the two that are important.

#include <iostream>
#include <string>

using namespace std;

struct Student
{
   string name;
   string major;
   Student *p_next;
};

Student *addStudent(Student *p_students)
{
   Student *p_new_student = new Student;
   cout << "Student name: \n";
   cin >> p_new_student->name;
   cout << p_new_student->name << "'s major: \n";
   cin >> p_new_student->major;

   Student *p_place_holder = new Student;
   Student *p_all_students = new Student;
   p_all_students = p_students;
   p_place_holder = NULL;

   if (p_students == NULL) // Adds the first element to the linked list which was initialized to NULL
   {
      p_new_student->p_next = p_students;
      delete p_place_holder;
      delete p_all_students;
      return p_new_student;
   }

   else // Adds elements beyond the first element to the linked list
   {
      while (p_all_students != NULL)
      {
         if (p_new_student->name.compare(p_all_students->name) <= 0)
         {
            if (p_place_holder == NULL) /* if p_new_student->name is before
            p_all_students->name and p_all_students is still the first element in the list*/
            {
               p_new_student->p_next = p_all_students;
               p_all_students = p_new_student;
               delete p_place_holder;
               return p_all_students;
            }

            else
            {
               p_new_student->p_next = p_all_students;
               p_place_holder->p_next = p_new_student;
               return p_students;
            }

         }

         else if (p_new_student->name.compare(p_all_students->name) > 0)
         {
            p_place_holder = p_all_students;
            p_all_students = p_all_students->p_next;
         }
      }
   }
}

void mainMenu(Student *p_students)
{
   int response = 0;
   cout << "1. Add Student\n";
   cout << "2. View Students\n";
   cin >> response;

   if (response == 1) // calls addStudent 4 times and then mainMenu
   {
      p_students = addStudent(p_students);
      p_students = addStudent(p_students);
      p_students = addStudent(p_students);
      p_students = addStudent(p_students);
      mainMenu(p_students);
   }

   else if (response == 2) // lists the students and their majors and then exits
   {
      while (p_students != NULL)
      {
         cout << p_students->name << '\n';
         cout << p_students->major << "\n\n";
         p_students = p_students->p_next;
      }
   }

   delete p_students; // hopefully deletes all allocated memory
}

int main()
{
   Student *p_students = new Student;
   p_students = NULL;

   mainMenu(p_students);
}

Basically wondering if “delete p_students;” in the mainMenu function will properly delete all allocated memory so that the program doesn’t leak memory. Any help is appreciated, thanks a lot.

  • 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-06-03T19:28:01+00:00Added an answer on June 3, 2026 at 7:28 pm

    These lines will leak an instance of Student:

    Student *p_place_holder = new Student;
    Student *p_all_students = new Student;
    p_all_students = p_students;
    p_place_holder = NULL;
    

    You allocate a Student, assign it to p_place_holder, then later overwrite the pointer with NULL without deleting the instance.

    You also have:

    p_students = addStudent(p_students);
    p_students = addStudent(p_students);
    p_students = addStudent(p_students);
    p_students = addStudent(p_students);
    

    which will overwrite p_students three times without trying to free the memory in the old value.

    The larger problem is you end with delete p_students, but that won’t examine the Student structure to find the pointer within it, delete it, etc, recursively, as you are hoping. You are leaking almost all of the memory you’ve allocated.

    I think you are misunderstanding how new and delete pair together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <string> using namespace std; class PersonList { private: char aName[7]; int aBribe; PersonList
I have a method AddStudent() which looks for a student with the same name
Consider the following example where Student inherits from Person : function Person(name) { this.name
I have a problem using the STL containers in c++ function 1; void addStudent(const
I have a Hashmap that stores a student name as the key and an
My form action is set to a php file like this: <form name=addstudent action=includes/data.php?act=enquiry
It's a program that reads student data from a text file and displys it
Here is the code I'm using to create a new Student. public class StudentRepository
I'm having an error trying to send mail with PHP using the mail() function.
I have a addstudent.ctp file to add students echo $this->Form->create('Student'); echo $this->Form->input('FirstName'); echo $this->Form->input('LastName');

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.