1

I cannot get this code to work properly. When I try to compile it, one of three things will happen: Either I'll get no errors, but when I run the program, it immediately locks up; or it'll compile fine, but says 'Segmentation fault' and exits when I run it; or it gives warnings when compiled:

"conflicting types for ‘addObjToTree’
previous implicit declaration of ‘addObjToTree’ was here"

but then says 'Segmentation fault' and exits when I try to run it.

I'm on Mac OS X 10.6 using gcc.

game-obj.h:

typedef struct itemPos {
    float x;
    float y;
} itemPos;

typedef struct gameObject {
    itemPos loc;
    int uid;
    int kind;
    int isEmpty;
       ... 
 } gameObject;

internal-routines.h:

void addObjToTree (gameObject *targetObj, gameObject *destTree[]) {
    int i = 0;
    int stop = 1;
    while (stop) {
        if ((*destTree[i]).isEmpty == 0)
            i++;
        else if ((*destTree[i]).isEmpty == 1)
            stop = 0;
        else
            ;/*ERROR*/
            }

    if (stop == 0) {
        destTree[i] = targetObj;
    }
    else
    {
        ;/*ERROR*/
    }

}

/**/

void initFS_LA (gameObject *target, gameObject *tree[], itemPos destination) {

    addObjToTree(target, tree);
    (*target).uid = 12981;
    (*target).kind = 101;
    (*target).isEmpty = 0;
    (*target).maxHealth = 100;
    (*target).absMaxHealth = 200;
    (*target).curHealth = 100;
    (*target).skill = 1;
    (*target).isSolid = 1;
    (*target).factionID = 555;
    (*target).loc.x = destination.x;
    (*target).loc.y = destination.y;

}

main.c:

   #include "game-obj.h"
    #include "internal-routines.h"
    #include <stdio.h>

    int main()
{
    gameObject abc;
    gameObject jkl;
    abc.kind = 101;
    abc.uid = 1000;

    itemPos aloc;
    aloc.x = 10;
    aloc.y = 15;

    gameObject *masterTree[3];
    masterTree[0] = &(abc);

    initFS_LA(&jkl, masterTree, aloc);
    printf("%d\n",jkl.factionID);
    return 0;
}

I don't understand why it doesn't work. I just want addObjToTree(...) to add a pointer to a gameObject in the next free space of masterTree, which is an array of pointers to gameObject structures. even weirder, if I remove the line addObjToTree(target, tree); from initFS_LA(...) it works perfectly. I've already created a function that searches masterTree by uid and that also works fine, even if I initialize a new gameObject with initFS_LA(...) (without the addObjToTree line.) I've tried rearranging the functions within the header file, putting them into separate header files, prototyping them, rearranging the order of #includes, explicitly creating a pointer variable instead of using &jkl, but absolutely nothing works. Any ideas? I appreciate any help

1 Answers1

0

If I see this correctly, then you don't initialize elements 1 and 2 of the masterTree array anywhere. Then, your addObjToTree() function searches the - uninitialized - array for a free element.

Declaring a variable like gameObject *masterTree[3]; in C does not zero-initialize the array. Add some memset (masterTree, 0, sizeof (masterTree)); to initialize.

Note that you're declaring an array of pointers to structs here, not an array of structs (see also here), so you also need to adjust your addObjToTree() to check for a NULL-pointer instead of isEmpty.

It would also be good practice to pass the length of that array to that function to avoid buffer overruns.

If you want an array of structs, then you need to declare it as gameObject masterTree[3]; and the parameter in your addObjToTree() becomes gameObject *tree.

Community
  • 1
  • 1
Martin Baulig
  • 3,010
  • 1
  • 17
  • 22
  • thanks for responding so fast! I added that line but still no luck, though I think I know why. memset initializes an array; is there any such function that can easily initialize structs? Since masterTree points to gameObjects and `addObjToTree()` searches by a gameObject's `isEmpty` member, wouldn't I have to initialize those too? – Prosper Kouassi Nov 14 '12 at 03:46
  • You're actually declaring an array of pointers here, not an array of structs; see http://stackoverflow.com/questions/859634/c-pointer-to-array-array-of-pointers-disambiguation. So memset sets each of these pointers to NULL, but then you also need to change your `addObjToTree()` function to check for a NULL-pointer instead of `isEmpty`. Or declare it as `gameObject masterTree[3];` to actually declare an array of structs. – Martin Baulig Nov 14 '12 at 03:55
  • I converted masterTree into an array of gameObject structs, and it seems to be working with the functions after I rewrote them to reflect the change. Thanks for your help! – Prosper Kouassi Nov 14 '12 at 22:14