Skip to main content
gamedev.net
🔒 Locked

Dynamic Array allocation

Started by CuteBug Apr 25, 2003 at 10:50 AM 0 replies 200+ views
Original Post
CuteBug
CuteBug
I had to write a program where the user enters the cordinates of the vertices of an n-sided polygon. Here value of "n" is not known until runtime. So I created a pointer to CPoint say P. CPoint* P; The user enters the co-ordinates thru a dialog box which contains two edit boxes for x and y co-ordinates and two buttons - Add & Ok. When the user enters a co-ordinate and presses Add, the co-ordinate gets added to the array P. When the user presses Ok the program draws the polygon with the points in the array P as vertices. The problem is that each time the user presses Add button I want to dynamically allocate memory for the new Point with the help of the following statement P[count] = new CPoint(x,y); count++; where count is the number of points entered in the array and x & y are the co-rdinates. But this doesnt seem to be working and the program crashes. Please help me... Thanx a lot. "A byte is a large bit to crunch!"
"There is a Bug in every Code!"
JuNC
JuNC
Simple way, use an std::vector:


  

std::vector<CPoint> P;

P.push_back( CPoint(x,y) );



(that assumes CPoint has correct copy ctor & assignment operator)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.