Apparently, structures can't have pointers...

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
struct Vertex
{
    uint16_t x;
    uint16_t y;
    uint16_t z;
    uint16_t u;
    uint16_t v;
    uint8_t r;
    uint8_t g;
    uint8_t b;
    uint8_t a;

    bool operator!= (Vertex);
    bool operator== (Vertex);
};

bool Vertex::operator!= (Vertex TEMP)
{
    if (x != TEMP.x || y != TEMP.y || z != TEMP.z \
        || u != TEMP.u || v != TEMP.v \
        || r != TEMP.r || g != TEMP.g || b != TEMP.b || a != TEMP.a) return true;
    else return  false;
}

bool Vertex::operator== (Vertex TEMP)
{
    if (x == TEMP.x && y == TEMP.y && z == TEMP.z \
        && u == TEMP.u && v == TEMP.v \
        && r == TEMP.r && g == TEMP.g && b == TEMP.b && a == TEMP.a) return true;
    else return  false;
}

struct VertexList
{
    unsigned int len;
    unsigned char * list;

    VertexList()
    {
        len = 0x0000;
        list = NULL;
    }
};

struct Triangle
{
    uint8_t a;
    uint8_t b;
    uint8_t c;
};

unsigned char * vertex_list = NULL;
unsigned int vertex_list_len = 0x0000;
unsigned char * display_list = NULL;


Question continued on the next post...
I would post the function where the problem occurs but it's too long to fit in here. Anyways, here is where I have problems:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    /* A label to jump to if vert_list_total is equal to 0 */
    AddVertices:

        vert_list = (VertexList*) malloc(sizeof(VertexList));
        vert_list[0].list = (unsigned char*) 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];
                    else if (i == 1) vert_list[0].list[vert_list[0].len + k] = obj_data[(TEMP_Tri.b * 0x10) + k + vert_offset];
                    else if (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?
vert_list[0].list[vert_list[0].len + k]

You never initialized vert_list[0].len, therefore you are probably stepping out of bounds of your allocated memory here.

Remember that malloc does not call constructors, which is one reason why you should never use it in C++ code.


Also wtf @ goto.
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. 
I'm trying to refrain from using features only supported by C++

FYI, C doesn't have constructors or operator overloading so you'll want to get rid of those as well.
Learn to use a debugger, it will show you the exact line where your program crashes and allows to observe the state.
Post a test case.

By the way,
1
2
3
struct Vertex{
   int x[3], v[2], colour[4];
};
would simplify your code.

> Before you go and tell me, "gah, you should be using classes and objects!", I'm planning on converting this program to C
You can use OOP in C.
Last edited on
@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!
Last edited on
You can use classes in C?


No. C does not have classes.

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.

Classes just make OOP easier.
Topic archived. No new replies allowed.