Thread: Dynamicly allocating an array

  1. #1
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179

    Dynamicly allocating an array

    I'm not a newby, but this has me feeling like one. As long as I have an array with hard coded values I'm fine, but the moment I try to dynamically allocate my array the thing goes tits up.

    I'm trying to translate the old DOS game "Robotfindskitten" to PDCurses (I know it's been done, but it doesn't work for me), and I get the whole thing to work until I change the line:
    Code:
    int screen[80][24];
    to
    Code:
    int screen[COLS][LINES];
    at which point I get the error:
    Code:
    error: variable-size type declared outside of any function
    Okay, so I move the screen variable declaration inside the main function and pass by reference to the only function that needs it. Looks good except for when I run it. Now I'm getting a segfault at the line :

    Code:
    for (c = 0; c <= COLS; c++)
    	{
    		for (c2 = 0; c2 <= LINES; c2++)
    		{
    			screen[c][c2] = EMPTY;
    		}
    	}
    ...when c=0 and c2=11. What?

    I feel like I've delt with this before, but I can't figure out how I fixed it. It used to be that you could declare an int** and use that to pass a 2D array, but that seems to be disabled now. What is going on?
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Where are COLS and LINES defined?

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    They are a part of curses.h, and coincidently, no small part of the problem because while they are macros they are not merely numbers, no, they are complex hellians, which is why I can't use them to define the bounds of screen outside a function.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    COLS and LINES are not given values (at least not useful ones) until curses has been initialized. So don't create your array until you've set curses up.
    for (c = 0; c <= COLS; c++)
    You do not want <= here. If your array has COLS elements then you can only access from 0 to COLS-1. Same goes for your ROWS array.
    It used to be that you could declare an int** and use that to pass a 2D array
    No, this is not true. But you can use an int** to simulate a 2D array, which might be what you're thinking of. Arrays and pointers are different, although they have a relationship that often makes it seem that they are the same thing.

  5. #5
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by cas View Post
    COLS and LINES are not given values (at least not useful ones) until curses has been initialized. So don't create your array until you've set curses up.
    That's the key of knowledge I was missing! It doesn't matter how I initialize the array if I'm using COLS and LINES to do it before initscr.

    Duh on me.

    I'm just going to do the pointer method, only initialize one more than I need so that the <= won't overrun. I don't know why they did it this way, but I'm just converting the original code, not rewriting it entirely.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  2. dynamically allocating a 2dim array
    By agerealm in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:40 PM
  3. allocating memory for char* array
    By creeping_death in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2003, 04:49 AM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM