Arrays on Heap

This is a discussion on Arrays on Heap within the Game Programming forums, part of the General Programming Boards category; I'm making a game and I need a 2-D map allocated on the heap... something like this.. Code: int * ...

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Arrays on Heap

    I'm making a game and I need a 2-D map allocated on the heap... something like this..
    Code:
    int * pInt = new int[2][2];
    but when i try to compile, i get an error that says cannot assign from int[*][2]
    any ideas?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    A double dimensioned array isn't your normal pointer'd beast. It's viewed a little differently in memory, than other things.

    What you can do is allocate your memory and then access it via pointers directly. 2-D is really just x,y dimensions (or height/width)...
    It is not the spoon that bends, it is you who bends around the spoon.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    Sayeh is correct, but if you still wish to use multi-dimensional arrays,

    you must allocate the memory for the (pointers/arrays) individially.

    Code:
    #define PINT_SIZE1 2
    #define PINT_SIZE2 2
    
    
    int * pInt = new int[PINT_SIZE1];
    
    for(int i = 0; i < PINT_SIZE1;i++)
        pInt[i] = new int[PINT_SIZE2];
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    sayeh... can you tell me if i am understanding this right..
    pInt is a pointer to an array of 2 ints on the heap, but you dont store ints in them, you store pointers to ints..?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  5. #5
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Sorry for the delay in response...

    In any array with more than 1 dimension, the first element is a pointer. If it's a single-dimensioned array, then of course it's actual data.

    it is _much_ easier to keep a pointer for the base address of your 2D map, and calculate the address of the data you want to access.

    For example, map size = 10 x 10. So, in code:

    Code:
    #define WIDE 10
    #define HIGH 10
    
    int    *map;
    int    *temp;
    int    data;
    int    x,y;
    
    map = 0L;                     /* init vars to _known_ state */
    temp = 0L;
    
    map = (int*)malloc(sizeof(int) * (WIDE * HIGH));  /* allocate RAM for map */
    if(map)                          /* allocate okay? */
       {
       x = 5;
       y = 9;
    
       temp = (map + ((long)x * (long)y));   /* calculate address of position x,y in map */
       data = *temp;
    
       free(map);                 /* deallocate map buffer */
       map = 0L;
       };
    . . .
    You have a base address (map), a scratchpad address (temp), and your row/column indexes (x,y).
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-10-2007, 11:49 PM
  2. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 02:08 PM
  3. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. Multi-Demensional Arrays on Heap
    By GrNxxDaY in forum C++ Programming
    Replies: 40
    Last Post: 08-15-2002, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21