我不认为我正在破坏动态记忆

时间:2016-04-15 21:40:40

标签: c++ arrays memory dynamic destructor

我目前正在尝试创建学生结构的动态数组。唯一的问题是,当我正在尝试编译时,我遇到了很多错误,太多无法计算。我以为我正在删除我正在使用的所有内存。

该计划的输入是多少学生和多少年级,然后我根据他们想要的等级和有多少学生动态创建一个数组。

输出应该只是打印每个学生和年级,但我不认为我应该有麻烦,动态记忆是我不理解的部分。

// Filename: pointers.cpp
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;


struct Student
{
  string name;
  int id;
  int* mark;
  ~Student()
 {
   delete [] mark;
   mark = NULL;
 };
};

void initStudent(Student* ptr, int markNum, int studentNum );   // function prototype for initialization
void sayStudent(Student* ptr, int markNum);     // function prototype for printing

//*********************** Main Function ************************//
int main ()
{
  int mark, studentNum;
  Student stu;           // instantiating an STUDENT object
  Student*  stuPtr = &stu;  // defining a pointer for the object
  cout << "How many marks are there? ";
  cin >>  mark;
  cout << "How many students are there?";
  cin >> studentNum;
  Student* students = new Student[studentNum];

  initStudent(&stu,mark,studentNum);       // initializing the object
  sayStudent(&stu,mark,studentNum);       // printing the object
  delete [] students;

return 0;

} // end main

//-----------------Start of functions----------------------------//

void initStudent(Student* ptr, int markNum, int studentNum)
{  ptr -> mark = new int[markNum];
   cout << "Enter Student Name :";
   cin >> ptr -> name;
   cout << "Enter Student ID Number :";
   cin >> ptr -> id;
   for (int i = 1; i <= markNum; i++)
     {
       cout << "Please enter a mark :";
       cin >> ptr -> mark[i-1];
     }
 }

void sayStudent(Student* ptr, int markNum)
 {
   cout << "Student info:"<< endl ;
   cout << "Name: " << ptr -> name << endl;
   cout << "Id:" << ptr -> id << endl;
    for (int i = 0; i < markNum; i++)
    {
      cout << "Mark " << i  << ": " << ptr -> mark[i] << endl;

     }

 }

1 个答案:

答案 0 :(得分:1)

正如上面的评论所说,你得到的例外不是因为你有不一致的记忆。我所知道的所有C ++编译器都不会抛出内存泄漏异常,您需要使用命令行选项(在某些编译器中)启用它,或者使用其他工具(例如Valgrind)。您可能只是遇到语法错误。以下是代码的清理和修复副本(未现代化):

// Filename: pointers.cpp
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;


struct Student
{

    string name;
    int id;
    int* mark;
    int markNums;
    Student(){

        cout << "Enter Student Name :";
        cin >> name;
        cout << "Enter Student ID Number :";
        cin >> this -> id;
        cout<<"Enter number of marks :";
        cin>>markNums;

        mark = new int[markNums];
        for (int i = 0; i <= (markNums-1); i++)
        {
            cout << "Please enter a mark :";
            cin >> mark[i];
        }



    }
    ~Student()
    {
        delete [] mark;
        mark = nullptr;
    };
    void say()
    {
        cout << "Student info:"<< endl ;
        cout << "Name: " << this -> name << endl;
        cout << "Id:" << this -> id << endl;

        for (int i = 0; i <(markNums); i++)
        {
            cout << "Mark " << i  << ": " << this -> mark[i] << endl;

        }

    }

};



//*********************** Main Function ************************//
int main ()
{
    int studentNum;

    cout << "How many students are there?";
    cin >> studentNum;
    Student * stuPtr;
    stuPtr=new Student[studentNum];// initializing the object
    for (int i=0; i<=(studentNum);i++){
        (*(stuPtr+i)).say();

    }
    delete [] stuPtr;

    return 0;

} // end main

据我所知,只要您输入正确的类型,此程序中就没有内存泄漏/其他错误。任何错误的类型都会触发内存泄漏和流量问题。我希望这能解决你的问题。您仍然应该使用STL构造对此代码进行现代化,这将使您的程序更加安全和有效。