Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / array problems

Author
Message
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 26th Jun 2009 07:08
I'm trying to write a program that will take a polygon, and convert it into triangles. I have the algorithm worked out, and i have it coded, but im having some problems compiling it to start debugging, and it's because of a dynamic struct array.

Basically, its an array called triangles, which holds x1,x2,x3,y1,y2, and y3. The problem seems to be when i put in triangles[], to delete the array or find the size. I have no clue what im doing wrong here, because im a newbie to c++. Im gonna post up the entire program because it needs a looking over from someone who knows what they're doing anyways.



In case you wanna know how the algorithm works (for clarity or interest), here it is:


Excluded vertices are red, the current vertices are green, the tris are green, and the polygon is black. Heres how it works:
1. find the leftmost vertex
2. find the two adjacent, non excluded vertices
3. create a triangle
4. find the next leftmost vertex
5. find the two adjacent, non excluded vertices
6. create a triangle
7. repeat


Also, this is only the second usable program i've written in c++, so if theres anything stupid im doing, any bad habits im forming, or anything else, please tell me!

Iuzidal
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location:
Posted: 26th Jun 2009 07:28
Looks like you have a local and a global variable with the same name.
tri* triangles=new tri[1]; is repeated twice.

I would ditch the array and look into using an stl container like a vector which will handle all the memory management for you.

Something like (pseudocode)


I had to turn off the debug runtime lib in 2008 to use STL.
Config Properties-> C++ -> Code Gen -> Runtime Library set to /MT
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 26th Jun 2009 07:57
I don't have the time to look over the whole thing right now, since it's 12:51 AM , but in these lines:



Take out the [] after the triangles variable. The compiler will know it's an array. Also, when calling delete, put the [] after delete instead of after the variable.



Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 28th Jun 2009 01:23
Thanks Iuzidal and bran flakes, i've heard of vectors, and i'll probably try using them because i've had so many problems with dynamic arrays, but i want to figure out what im doing wrong here first.

Heres my slightly changed code. I've taken out the string.h includes because there is a library in there that conflicts with something else, and i've made a couple other modifications. However, i get "Read access violations" and it seems to be coming from my addmem function. The application runs for a bit, and eventually gets an error message.


I still have no idea what i'm doing wrong heree

Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 28th Jun 2009 03:27 Edited at: 28th Jun 2009 06:07
Wrong.

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 28th Jun 2009 05:12
What do you mean by changing it directly?

Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 28th Jun 2009 06:11
Never mind what I said before, I was... Wrong. I debugged your code and saw that memcpy() was the problem, so I replaced it with a simple for loop.



The length of the dynamic array is calculated by dividing the whole by the first part, just to explain.

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
Iuzidal
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location:
Posted: 28th Jun 2009 08:01
As I pointed out earlier, you have an issue with using the name "triangles" twice

Global definition:
tri* triangles=new tri[1];

Location in update_tris
delete [] triangles;
tri* triangles=new tri[1];

The local definition is eclipsing the global one on each iteration. You then access the global one which has been deleted, but no new memory has been allocated.

remove "tri*" from the 2nd allocation.

---

I don't use dynamic arrays much, but isn't sizeof(triangles) just 4 since it's a pointer? sizeof(tri) will give you the size of the struct, but without making a compiler specific call I don't think you have access to the size of dynamic array from just the array pointer. I think your trinum variable tracks that.. just new tri[trinum];

Login to post a reply

Server time is: 2024-10-01 05:45:46
Your offset time is: 2024-10-01 05:45:46