creating a dynamic multi dim array in c++?

Discussion in 'OT Technology' started by D1G1T4L, Mar 8, 2005.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    i was just wondering how would you create a dynamic 2d array
    char *userInput = new char[NUM_ROWS][NUM_COLS];

    ??

    and then to clear the memory would u still do

    delete [] userInput;

    or delete [][] userInput;
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    char (*userInput)[NUM_COLS] = new char[NUM_ROWS][NUM_COLS];

    delete [] userInput;
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    why is it char (*userInput)[NUM_COLS]

    and not char (*userInput)[NUM_ROWS]
     
  4. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    it really doesnt make a difference as long as you are aware what does what. myself i prefer [ROWS][COLS] but in the end, as long as you keep the same format it makes sence
     
  5. SL1200MK4

    SL1200MK4 New Member

    Joined:
    Sep 27, 2003
    Messages:
    1,552
    Likes Received:
    0
    Yes and no... Depends on how you will access your data, it might make a difference in terms of performance.

    Specifically we are talking about row major order vs column major order. This differs from languages to languges [or compilers to compilers if the languages does not stipulate for a row major or column major layout].

    Also the among of memory used might be different from contigous allocation vs row pointers. But that's another issue all together.
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The compiler needs to know the size of an individual element of an array to correctly do pointer math. Any array, no matter how many dimensions, is allocated as a logically contiguous chunk of memory. By attaching the element size to the pointer declaration, the compiler knows that the address of userInput[1] is NUM_COLS greater than userInput[0].
     
  7. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    so wait, which one of you is right? one says it doesnt matter and skinjob says it does matter? me go crazy!!!
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    It matters because multidimensional arrays in C/C++ are stored in row-major order, which means elements in a 2D array are arranged as an array of rows, not an array of columns. When declaring a pointer to a 2D array, only the size of the row is important. The number of rows is arbitatry since the pointer can point to any number of NUM_COLS size rows.

    If your intention is to have an array that can store 3 strings, each up to 4 bytes long, the code would look like:

    char (*userInput)[4] = new char[3][4];

    Another way to look at it is, userInput is a pointer to a 1D array, but each element in the array happens to be a 4-byte char array.

    Later in your code you could do something like:

    char (*newArray)[4] = new char[1000][4];
    userInput = newArray;

    The compiler will be perfectly happy since userInput and newArray are pointers to 4-byte char arrays.
     
  9. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    and don't forget to "delete the memory pointed to by userInput" before reassigning it with the new array.

    :hsugh:
     
  10. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    no need to get so technical for a newbie. im well aware of the memory and speed. at his current moment, unless he is programming for optimization, i wouldn't worry about it.

    in Java, it can be upto if not 2x as expensive to access 2 a 2D array vs 1D
    O(n) vs O(n*m)
     
  11. beez

    beez New Member

    Joined:
    Jun 3, 2004
    Messages:
    19,143
    Likes Received:
    0
    Location:
    Queens
    More than 2x . . . more like exponentially more expensive :)
     
  12. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    :fawk: :hs: :wtc:
     
  13. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    check my O() before talking
     
  14. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Does java implement arrays as linked lists? If so, why would access be greater than O(n)?
     

Share This Page