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:
members
width
,height
used member allocation sizing before contain definite values. therefore value usage indeterminate, , therefore memory allocation exhibits undefined behavior.the allocation being stored local pointer, promptly lost after exiting constructor. never assigned member variable
array
. leaking memory. further , sincearray
(the member) never assigned, value indeterminate after construction, , therefore access (read or write) using address contains undefined behavior.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).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.neither constructor has initializer list. both should.
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.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.
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
Post a Comment