【问题标题】:Dynamic Allocation not creating array动态分配不创建数组
【发布时间】:2016-01-16 06:07:26
【问题描述】:

我正在为学校做作业,我们正在做内存管理。到目前为止,我们的任务基本上只是创建一个学生 + 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;
}

修改为当前版本

【问题讨论】:

标签: c++ arrays dynamic allocation


【解决方案1】:

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

【讨论】:

  • 我已经弄清楚我做错了什么。当我不需要它时,我将它设置为尝试做一个两个暗淡的数组。将其设置为具有多个变量(即,First、Last、Id 等)的一个暗淡数组就可以了。我已将其编辑为: "std::cout
  • 您确定需要使用新展示位置吗?有很多与此相关的棘手业务。 isocpp.org/wiki/faq/dtors#placement-new
  • std::string firstname,默认构造就好了,您可以在构造函数中删除它的所有特殊分配代码。或者您必须为学生重载 new 并为其成员之一进行花哨的内存分配?
  • 因为这是一个分配,所以我遵循以下限制:覆盖运算符 New 和 Operator Delete 是强制性的。您应该拥有以下固定大小的学生对象的全局数组,例如 1024,并将其用作分配和释放内存插槽的后台存储。每个学生对象都有一个名为 Occupied 的标志。您必须使用它来指示插槽当前是否正在使用或空闲。 1)创建一个学生结构如下: struct Student{ char *firstname; char 姓氏; int学生ID; int 占用; }
  • 啊,所以这要求您将缓冲区设置为学生缓冲区[1024];然后 new 需要运行并标记占用 = true 并返回该插槽的内存。这个概念称为内存池,即您有一个“空”学生对象池。新的超载将它们分发给新的学生实例。我认为您目前无法满足您的要求。
猜你喜欢
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
  • 2020-03-06
相关资源
最近更新 更多