Can you guys spot what's wrong with my code? I've managed to single out in the program where exactly the program crashes but I can't figure out why it crashes... Maybe there was something I missed whenever I was studying structures?
Before you go and tell me, "gah, you should be using classes and objects!", I'm planning on converting this program to C when I'm done and so I'm trying to refrain from using features only supported by C++... Of course there are some features in there, but they could be replaced relatively quickly. Anyways, here's some code that defines some structures.
/* A label to jump to if vert_list_total is equal to 0 */
AddVertices:
vert_list = (VertexList*) malloc(sizeof(VertexList));
vert_list[0].list = (unsignedchar*) malloc(0x30);
vert_list_total = 1;
printf("here\n");
for (i = 0; i < 3; i++)
{
for (k = 0; k < 0x10; k++)
{
if (i == 0) vert_list[0].list[vert_list[0].len + k] = obj_data[(TEMP_Tri.a * 0x10) + k + vert_offset];
elseif (i == 1) vert_list[0].list[vert_list[0].len + k] = obj_data[(TEMP_Tri.b * 0x10) + k + vert_offset];
elseif (i == 2) vert_list[0].list[vert_list[0].len + k] = obj_data[(TEMP_Tri.c * 0x10) + k + vert_offset];
}
vert_list[0].len += 0x10;
}
printf("wee\n");
return vert_list;
The label is jumped to from the beginning of the function here:
1 2
/* Got to make sure that we can even access things in the vert_list before we try to start grabbing things... */
if (vert_list_total == 0) goto AddVertices;
The reason why the program would print "here" and "wee" is for debugging purposes. I know that the problem happens there because "here" prints but the the program crashes before it is able to print "wee"... Any idea why this might be happening?
I've managed to single out in the program where exactly the program crashes
Please tell us where.
1 2 3 4 5
bool Vertex::operator== (Vertex TEMP)
bool Vertex::operator!= (Vertex TEMP)
//You create a new instance of the passed argument.
//Maybe the original instance's values are not copied over?
//Try passing them as a reference.
@ne555 : You can use classes in C? The pieces of code that I posted are from a program that I friend wanted me to write, but he insisted that it be written in C. I don't actually know C but as far as I can tell the differences between it and C++ are very minimal besides that fact that C++ is based around OOP. Anyways, I was handing him the source code earlier (I'm contributing to a program of his) and he was telling me that C doesn't use classes at all.
@Disch : Hmm, I never knew that about malloc. Now that I think about it, it would make sense seeing as how C doesn't use classes (or does it? ne555 says it can...). And yeah, I was using goto just for some quick testing. I really just want to get the program working correctly first before I clean up all the code.
@Zhuge : I know about that, but for the time being I'm using certain features of C++ just to reduce the amount of text I have to write. Once I'm done fixing everything with the program, I plan on rewriting the parts that would only work in C++.
> "Of course there are some features in there, but they could be replaced relatively quickly."
I apologize for my lack of knowledge about certain things as I have only been programming for about 3 months (I tried learning it awhile ago, but got bored and didn't try to pick it up until a couple of weeks ago), so bear with me if I have stupid questions. :P I thank you all for your courtesy!
ne555 was referring to the fact that you can do OOP without classes. Take a look at C's FILE struct (and fopen/fread/etc functions) for an example of how it's done.