c++ - Dynamic Allocation not creating array -


i'm doing assignment school , we're doing memory management. far we're tasked create list of students + id's , we're dynamically.

i'm supposed overload delete/new operators, i've done. when testing out program crash, possibly not creating array allocate information.

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];  (int = 0; < 5; ++i) {     std::cout << "fill in first name student." << std::endl;     std::cin >> studentlist[i].firstname;     std::cout << "fill in last name student." << std::endl;     std::cin >> studentlist[i].lastname;     studentlist[i].studentid = (rand() % (9999 - 999)) + 999;     studentlist[i].occupied = 1; }  return 0; } 

edited current version

welp, assume you've either solved it, or spent enough time on , moved on else. either way, going update answer reflect new details gave me , provide simple memory pool example.

#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;    ( int = 0; < bufsize; ++i )    {       if ( !buffer[ ].occupied )       {          buffer[ ].occupied = true;          ret = &buffer[ ];          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 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;     ( int = 0; < num_students; ++i )    {       student* s = students[ ];       bool* occupied = &( s->occupied );       int slot = std::distance( buffer, s );        std::cout << "student addr: " << s << std::endl;        // slot should occupied       std::cout << "slot " << slot << " 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 deleted" << std::endl;        // exercise delete operator       delete s;        // slot should vacant       std::cout << "slot " << slot << " being occupied: " << std::boolalpha          << *occupied << std::endl;    }     return 0; } 

running produces:

address of memory pool: 0x100407000 student addr: 0x100407000 slot 0 being occupied: true occupying student (42) = jack daniels student deleted slot 0 being occupied: false student addr: 0x100407018 slot 1 being occupied: true occupying student (283) = johnny walker student deleted slot 1 being occupied: false student addr: 0x100407030 slot 2 being occupied: true occupying student (111) = jim bean student deleted slot 2 being occupied: false 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -