Thread: Problem using structures (they crash my program)

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    15

    Problem using structures (they crash my program)

    I'm just learning structures, and I'm having some trouble. The following is the structure I delcared globally:

    Code:
    struct creatures
    {
    	int *position;
    };
    and the next is when I declare members of the structure in my main function:

    Code:
    char command;
       struct creatures prey1;
       struct creatures pred;
    Now, if you need to know more I can post more code, but I don't think it's necessary. Basically, my program runs perfectly fine. Only, if I so much as declare the second member (struct creatures pred), the program crashes on startup. If I comment out that line, everything works as intended again. Wtf? I'm not even doing anything with it yet! Any help would be greatly appreciated.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This does not demonstrate the problem. However, one possible solution is that you're allocating too much on the stack.
    Is there more allocation code you aren't showing?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    15
    As far as I'm aware, there is no more allocation code. I don't even know enough to know what that really means. To show more code, here is how I use the first structure element that works:

    Code:
    int preyh(char command, struct creatures *prey)
    {
    
       move(command, prey);
       grid[prey->position[0]][prey->position[1]]=1;
    
       return 0;
    }
    Where it just sends the position array into the "move" function and changes it based on what command I gave (here I'm just moving a character around a 2D array of chars, and displaying - a 1 running around the screen, for example).

    My main function is this:

    Code:
    int main(void)
    
    {
    	char command;
       struct creatures prey1;
       struct creatures pred;
    
       prey1.position[0]= 6;
       prey1.position[1]= 6;
    
      // pred.position[0]=8;
      // pred.position[1]=14;
    
        zerobox();
    
        printf("Please enter a command ('q' to quit).\n");
    
        do{
    
        command=input();
    
        preyh(command, &prey1);
        //predator(&pred, &prey1);
        printbox();
        zerobox();
        }
        while(command != 'q');
    
        gameover();
    
        return 0;
    
    }
    I don't do anything with the structure anywhere else, so this should cover it. I just want to be able to feed the "pred" element into the move function as well, so that it can chase the "prey" around. I just don't understand why declaring it is crashing my program.

    Since I know nothing about memory allocation, maybe the problem is that I haven't been doing it? Can you suggest any resources online for learning more? (I am not taking a class nor do I have a book, I'm just learning because it is a useful skill to help me with my research, so I'm limited to free learning).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is:
    Code:
       prey1.position[0]= 6;
       prey1.position[1]= 6;
    There are great tutorials on the site where you could learn form. For a start.
    You also need to understand how pointers work:
    http://cpwiki.sourceforge.net/Common...llocating_them
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by shadow1515 View Post
    Since I know nothing about memory allocation, maybe the problem is that I haven't been doing it?
    Exactly. You define your structs as holding a pointer, but that pointer doesn't point to any allocated memory. Yet you poke data into it in your program. The fact that it doesn't crash when commenting out certain pieces of code is just a matter of luck. Something like if you shoot someone and don't happen to hit any vital organs, they may live.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    15
    Thanks for the information. I think I'm starting to understand a little better now. I ended up simply declaring the position array as int position[2] because I already knew what size I wanted it to be.

    One question though: had I allocated memory using the malloc function, would I do it inside this

    Code:
    struct creatures
    {
    	int *position;
    };
    or in here somewhere

    Code:
    struct creatures prey1;
    
       prey1.position[0]= 6;
       prey1.position[1]= 6;
    ?

    If I allocated memory in the second piece of code, where I declare the member of the structure, then would that allow me to make the position pointer a different size (maybe different size array, or whatever) for each member if I wanted to?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The struct is a blueprint. Therefore you cannot instruct it what data it contains, only what data it can contain.
    Therefore, calling malloc and free is your responsibility when creating an instance of the struct.
    And a pointer is a pointer, no less, no more. A pointer does not contain information about size, type or anything else such as that. It only contains an address.
    Hopefully that information should answer your question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    15
    It does, thank you, you did a lot to clear up my understanding of structures and pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  2. Problem with structures
    By dereach in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 08:22 AM
  3. How to avoid console program crash when terminated too early
    By Xargo in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2007, 04:43 PM
  4. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  5. Saving vectors of structures problem
    By redeck in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2004, 04:47 PM