c++ - Constructor Not Working -


i writing program show conway's game of life in c++. professor has given main function , class describing "universe", must implement functions prototyped in class. issue right getting constructor function. post class, followed have written constructor. using gdb when first line constructor used (universe(width, height, wrap);) following error: libc++abi.dylib: terminate called throwing exception

program received signal sigabrt, aborted. 0x00007fff876fad46 in __kill ()

any appreciated! code below.

    // conways game of life      class universe {              private: int* array;     // pointer [width]x[height] array of cells constitutes       universe                 //    game of life. value of 1 signifies live cell @ location. int width;      // width of universe. int height;     // height of universe int wrap;       // wrap = 1 means universe wraps around on -- right hand                  //   side connects left , top connects bottom.                 //   wrap = 0 means universe ends @ array boundaries. public: universe();         // null constructor: sets array, width, height, , wrap 0. universe(int,int,int);  // constructor allocate space universe of given width (first value)                      //    height (second value) , wrap setting (third value). sets cells 0. void display();     // display live cells in graphics window. void setup();       // display universe allow user interactively modify                      //    cell arrangement using mouse. void operator<<(char*); // read in universe file; first number has width,                             //    second number height,                             //    third number wrap parameter,                             //    1s/0s in 2d integer array represent living/dead cells.    void operator>>(char*); // save universe file specified name (format above). void operator=(universe);   // copy contents of 1 universe another. void operator<<(universe);  // calculate new generation applying rules,                             //    display new generation. int neighbors(int,int);     // returns number of neighbors of cell @ i,j. int value(int,int);        // returns value @ cell i,j (0 or 1). void setvalue(int,int,int); // sets value of cell @ i,j.   void free(); // release memory used store universe. set array = 0.  };  // implementation   universe::universe(){ array =0;                width = 0; height = 0; wrap = 0; }  universe::universe(int width1,int height1,int wrap1){  int i=0, j=0; int* array = new int[width*height-1];        for(i=0;i<width;i++){     for(j=0;j<height;j++){         array[j*width+i] =0;                         }                     } width = width1; height =height1; wrap = wrap1; } 

there many things wrong original code hard rattle off list, can try:

  1. members width , height used member allocation sizing before contain definite values. therefore value usage indeterminate, , therefore memory allocation exhibits undefined behavior.

  2. the allocation being stored local pointer, promptly lost after exiting constructor. never assigned member variable array. leaking memory. further , since array (the member) never assigned, value indeterminate after construction, , therefore access (read or write) using address contains undefined behavior.

  3. you have no class destructor clean memory allocated in constructor or member functions. (assuming fix 3-param constructor , saves memory allocation pointer in array member). therefore leaks memory on destruction (assuming 3-param constructor fixed), or on construction (assuming 3-param constructor not fixed).

  4. you're width , height members can accept negative values, make no sense use, , wreak potential havoc allocation. members not intended explicitly allow negative values should of unsigned types, size_t being common.

  5. neither constructor has initializer list. both should.

  6. class universe dynamically allocates memory local member variable. without establishing virtual destructor, copy-constructor, , assignment operator, compiler-default implementations provided, , most-assuredly cause either memory leaks or memory corruption result. code should practice the rule of three, , not.

  7. your current allocation size logic in 3-param constructor off 1 element. (-1) not belong there, , loops after write 1 element beyond allocated size as-written. undefined behavior.

  8. you're using name of standard library defined class name of locally defined variable/class/member. while not officially "wrong" highly suggested avoid practice.

i strongly advise solid c/c++ book.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -