动态分配未创建数组

时间:2016-01-16 06:07:27

标签: c++ arrays dynamic allocation

我正在为学校做作业,我们正在进行记忆管理。到目前为止,我们的任务基本上只是创建一个学生+ id的列表,我们将动态地进行。

我也应该超载删除/新操作符,我已经完成了。但是,在测试我的程序时它会崩溃,可能不会创建数组来分配信息。

namespace
{
char buffer[1024];
int allocated = 0;
}
struct student
{
int size;
char *firstname;
char lastname;
int studentId;
int occupied;

student::student() : size(0)
{
}

student::student(int s) : size(s)
{
    std::cout << "constructor" << std::endl;
    std::cout << "Allocated: " << allocated << std::endl;
    int currentLoc = allocated;
    allocated += s;
    firstname = new (&buffer[currentLoc]) char[s];
}

void *student::operator new(size_t s)
{
    std::cout << "Operator new allocated: " << allocated << std::endl;
int currentLoc = allocated;
allocated += s;
return &buffer[currentLoc];
}

    void student::operator delete(void *ptr)
{
    std::cout << "Delete called " << std::endl;
    std::free(ptr);
}

student::~student()
{
}
};
int main(int argc, char** argv)
{
student *studentlist = new student[5];

for (int i = 0; i < 5; ++i)
{
    std::cout << "Fill in the first name for the student." << std::endl;
    std::cin >> studentlist[i].firstname;
    std::cout << "Fill in the last name for the student." << std::endl;
    std::cin >> studentlist[i].lastname;
    studentlist[i].studentId = (rand() % (9999 - 999)) + 999;
    studentlist[i].occupied = 1;
}

return 0;
}

编辑为当前版本

1 个答案:

答案 0 :(得分:0)

Welp,我认为你已经解决了这个问题,或者你现在已经花了足够的时间去做其他事情。无论哪种方式,我都会更新我的答案,以反映您给我的新细节,并提供一个简单的内存池示例。

#include <iostream>

struct Student
{
   Student( )
   : firstname( NULL ), lastname( NULL ), id( 0 ), occupied( false )
   { }
   Student( const char* fn, const char* ln, int id_ )
   : firstname( fn ), lastname( ln ), id( id_ )
   {
   }

   static void* operator new(size_t s);
   static void operator delete( void *ptr );

   const char* firstname;
   const char* lastname;
   int id;
   bool occupied;
};

static const int BUFSIZE = 1024;
Student buffer[ BUFSIZE ];

void* Student::operator new(size_t s)
{
   void* ret;
   for ( int i = 0; i < BUFSIZE; ++i )
   {
      if ( !buffer[ i ].occupied )
      {
         buffer[ i ].occupied = true;
         ret = &buffer[ i ];
         break;
      }
   }
   return ret;
}

void Student::operator delete( void *ptr )
{
   reinterpret_cast<Student*>( ptr )->occupied = false;
}

int main(int argc, char *argv[])
{
   static const int NUM_STUDENTS = 3;
   Student* students[ NUM_STUDENTS ];

   // Create the students
   students[ 0 ] = new Student( "Jack",    "Daniels",  42 );
   students[ 1 ] = new Student( "Johnny",  "Walker",   283 );
   students[ 2 ] = new Student( "Jim",     "Bean",     111 );

   std::cout << "Address of memory pool: " << &buffer[ 0 ] << std::endl;

   for ( int i = 0; i < NUM_STUDENTS; ++i )
   {
      Student* s = students[ i ];
      bool* occupied = &( s->occupied );
      int slot = std::distance( buffer, s );

      std::cout << "Student addr: " << s << std::endl;

      // Slot should be occupied
      std::cout << "Slot " << slot << " is being occupied: " << std::boolalpha
         << *occupied << std::endl;

      // Print student
      std::cout << "Occupying Student (" << s->id << ") = " << std::string( s->firstname )
         << " " << std::string( s->lastname ) << "" << std::endl;

      std::cout << "Student is about to be deleted" << std::endl;

      // Exercise your delete operator
      delete s;

      // Slot should be vacant
      std::cout << "Slot " << slot << " is being occupied: " << std::boolalpha
         << *occupied << std::endl;
   }

   return 0;
}

运行它会产生:

Address of memory pool: 0x100407000
Student addr: 0x100407000
Slot 0 is being occupied: true
Occupying Student (42) = Jack Daniels
Student is about to be deleted
Slot 0 is being occupied: false
Student addr: 0x100407018
Slot 1 is being occupied: true
Occupying Student (283) = Johnny Walker
Student is about to be deleted
Slot 1 is being occupied: false
Student addr: 0x100407030
Slot 2 is being occupied: true
Occupying Student (111) = Jim Bean
Student is about to be deleted
Slot 2 is being occupied: false