1

I have an obvious question, yet I'm perplexed by the problem.

Ok, first let me overview the situation. I have a structure called ENTITY that is used to hold the attributes for entities in a game. I recently added more members to the structure. The program runs perfect, but when I quit, windows pops up an error screen saying "XXX.exe has stopped working...check online for solution...blah blah".

So, to troubleshoot, I removed a few members from the ENTITY structure and the program runs fine and exits fine. ????

(compiled with Dev-cpp)

The code:

typedef struct _ENTITY
{
    char classname[16];
    int health;
    int vel_x;
    int vel_y;
    int direction;
    int frame;
    int flag;
    SDL_Rect bbox;
    struct _ENTITY *next;
    struct _ENTITY *owner;
    struct _ENTITY *goal;
    void (*think) ();
    float nextthink;
} ENTITY;

The function that allocates memory to ENTITY structures

ENTITY *ENTITY_spawn (void)
{
    ENTITY *node, *old_node;
    int i;

    node = ENTITY_head; // Top of list

    // Find end of list
    for (i = 0; node; i++)
    {
        old_node = node;
        node = node->next;
    }

    // Allocate
    node = (ENTITY*)calloc (1, sizeof (ENTITY));

    if (i)
        old_node->next = node;
    else
        ENTITY_head = node;

    return node;
}

(EDIT 4/8/12) -Used calloc instead of malloc -Inserted void in function parameters -Got rid of NULL_void -Could not get rid of (ENTITY*) cast, the compiler complains that it could not convert type void (because I didn't include stdlib.h?)

Here's how I remove ENTITY(s) when exiting the program:

void ENTITY_cleanup (void)
{
    ENTITY *node, *old_node;

    node = ENTITY_head;

    while (node)
    {
        old_node = node->next;
        free (node);
        node = old_node;
    }
}
Alejandro Galera
  • 3,445
  • 3
  • 24
  • 42
sethb411
  • 11
  • 2
  • What did you remove from Entity? – Rose Kunkel Apr 08 '12 at 03:39
  • 2
    The Windows API has data structures with way more members than your `_ENTITY` structure and compilers seem to have no trouble dealing with them. Can you post an actual [Short, Self Contained, Correct (Compilable), Example](http://sscce.org/) that demonstrates the problem? Also, Dev-C++ is *ancient*. Get a better IDE/compiler combination like Visual C++ Express or Code::Blocks. – In silico Apr 08 '12 at 03:44
  • You probably removed a pointer that happened to be causing that specific problem. – chris Apr 08 '12 at 03:46
  • The above code appears fine, there might be a problem where you use the members of the entity structure(those you removed afterwards) in your code... – bhuwansahni Apr 08 '12 at 03:48
  • 2
    Don't [cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Collin Apr 08 '12 at 03:58
  • I removed the C++ tag since this is a C question. – Mark B Apr 08 '12 at 05:00
  • 1
    For what it's worth, if you replace `malloc` by `calloc` you can remove all that (verbose)zeroing you're doing. – Dave Apr 08 '12 at 05:20
  • give your function a correct prototype. In C empty `()` mean that your function may receive any number of arguments. – Jens Gustedt Apr 08 '12 at 08:41
  • if you have a modern C compiler that supports at least C99 an initialization with a compound literal would be more appropriate, something like `node = (ENTITY){ 0 };`. This would avoid adding an assignment for each field and changing it if your structure changes. – Jens Gustedt Apr 08 '12 at 08:44
  • What is `NULL_void`? `NULL` and `0` are perfect initializers for function pointers. – Jens Gustedt Apr 08 '12 at 08:45
  • **The funny thing is, if I were to comment out any of the members of the structure the program runs fine and exits fine. I tested this by individually commenting out int health;, struct _ENTITY goal;, etc.** – sethb411 Apr 08 '12 at 13:14
  • Whenever your code runs when you remove pointers, then fails when you restore them, look for illegal memory access. – Adam Liss Apr 08 '12 at 13:27
  • Are you sure you are handling the value of `ENTITY_head` correctly? Do you set it to `NULL` in case you are deleting the list´s last element? You are missing this in `ENTITY_cleanup()`. – alk Apr 08 '12 at 16:03
  • **When I test this I run the program and immediately quit, so there's not much going on as far as handling this ENTITY structure.** – sethb411 Apr 08 '12 at 16:08
  • Adding/removing members to/from structures most probably changes the layout of the memory in use, which could reveal issues related to uninitialised variables, or unintentionally overwritten memory. Using a debugger might help. – alk Apr 08 '12 at 16:30
  • **Well, I don't know exactly why, but everything seems to work fine now. What I did was move a function that is called at the beginning of main (), before the game loop, that allocates and initializes the player entity. I moved it inside the game loop basically to the spot where the game actually starts. (There is a menu system preceding the gameplay)** Does anyone know why this would fix the problem? Something with allocating memory too soon? Too obscure to figure out without looking at all the code? – sethb411 Apr 09 '12 at 00:21
  • @user1319845 Just a wild guess, but when you changed the file with main method in it, you forced a recompilation of it. If there is a missing dependency between your ENTITY header and your main source file, the problems you're describing might occur due to different versions of your struct compiled into the different compilation units. – HonkyTonk Apr 11 '12 at 09:04

0 Answers0