c++ dynamic arrays and pointers -
ok i'm trying create array of pointers point vectors change in size. array of pointers nestled inside class that's inside vector. reason seem having problems memory becoming corrupt. if use vectors run problems stack overflowing caused stuff resizing , calling constructors. here essential layout of i'm gunning for.
maybe little sloppy. end problem of memory being currupted in babyclasses pointers, want access "linked" babyclasses via babyclasses vector of babyclasses it's connected to. clever ideas here?
and before tells me silly way things, isn't type of functionality basis of oo programming?
class baby { public: deque<shared_ptr<baby>> vinputs; int x; int y; int z; baby() { numinputs = 0; isnull = false; wastickled = false; x,y,z = 0; } void addinput(shared_ptr<baby> baby) { if(numinputs == 0) vinputs = deque<shared_ptr<baby>>(0); vinputs.push_back(baby); numinputs++; } void setxyz(int x, int y, int z) { x = x; y = y; z = z; } void tickle() { if(!wastickled) wastickled = true; else return; for(int i=0;i<numinputs;i++) { vinputs[i]->tickle(); } } void setnull(bool isnull) { isnull = isnull; } private: int numinputs; bool isnull; bool wastickled; }; class babylayer { public: int width; int height; babylayer() { width = 0; height = 0; } babylayer(int width, int height) { width = width; height = height; vecbabies = std::deque<deque<baby>>(0); for(int i=0;i<height;i++) { deque<baby> row = deque<baby>(0); for(int i=0;i<width;i++) { row.push_back(baby()); }; vecbabies.push_back(row); } makeconnections(); } baby * getbaby(int x, int y) { baby n = baby(); n.setnull(true); if(x >= width || x <0) return &n; if(y >= height || y < 0) return &n; n.setnull(false); return &vecbabies[y][x]; } ~babylayer(void) { } private: std::deque<deque<baby>> vecbabies; void makeconnections() { for(int y=0;y<height;y++) { for(int x=0;x<width;x++) { //top right if(y > 0 && x < width-1) vecbabies[y][x].addinput(shared_ptr<baby>(&vecbabies[y-1][x+1])); //middle right if(x < width -1) vecbabies[y][x].addinput(shared_ptr<baby>(&vecbabies[y][x+1])); //bottom right if(x < width -1 && y < height-1) vecbabies[y][x].addinput(shared_ptr<baby>(&vecbabies[y+1][x+1])); //bottom middle if(y < height-1) vecbabies[y][x].addinput(shared_ptr<baby>(&vecbabies[y+1][x])); } } } }; class babycube { public: int x; int y; int z; babycube(int x, int y, int z) { x = x; y = y; z = z; layers = deque<babylayer>(); for(int i=0;i<z;i++) { babylayer lay = babylayer(x,y); layers.push_back(lay); } nullbaby = baby(); nullbaby.setnull(true); makeconnections(); } void makeconnections() { int l = layers.size(); if(l == 0 || l == 1) return; for(int layer=0;layer<l;layer++) { babylayer * lay = &layers[layer]; if(layer< l-1) { for(int y=0;y<lay->height;y++) { for(int x=0;x<lay->width;x++) { //top left if(x > 0 && y > 0) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x-1,y-1))); //top middle if(y > 0) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x,y-1))); //top right if(y > 0 && x+1 < lay->width-1) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x+1,y-1))); //middle right if(x+1 < lay->width -1) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x+1,y))); //bottom right if(x+1 < lay->width -1 && y+1 < lay->height-1) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x+1,y+1))); //bottom middle if(y+1 < lay->height-1) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x,y+1))); //bottom left if(x > 0 && y+1 < lay->height-1) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x-1,y+1))); //middle left if(x > 0) layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x-1,y))); //middle middle layers[layer].getbaby(x,y)->addinput(shared_ptr<baby>(layers[layer+1].getbaby(x,y))); } } } } } baby * getbaby(int x, int y, int z) { if(z >= layers.size() || z < 0) return &nullbaby; if(y >= layers[z].height || y < 0) return &nullbaby; if(x >= layers[z].width || x < 0) return &nullbaby; return layers[z].getbaby(x,y); } void update() { } ~babycube(void) { } private: deque<babylayer> layers; baby nullbaby; };
out of morbid curiosity, revisited question see if had deciphered it.
the obvious issue see source code in babylayer::getbaby():
baby n = baby(); n.setnull(true); if(x >= width || x <0) return &n; // bad. if(y >= height || y < 0) return &n; // bad.
you're declaring new baby instance on stack, returning pointer it. baby instance named 'n' gets destructed when getbaby() returns, , returned pointer invalid.
i don't know compiler you're using, visual studio 2010 emits, "warning c4172: returning address of local variable or temporary" on these lines. note code sample incomplete , doesn't anything, had declare babycube instance receive warning.
since can't decipher code supposed do, , can make no sense of operation, can't explain why memory access exceptions thrown.
Comments
Post a Comment