Thread: Dynamic 2d array

  1. #1

    Dynamic 2d array

    Ok, I've already searched and there are a bazillion threads for this but I couldn't seem to get it working (to tired prolly is hangover) ....

    Anyways I'm trying to create a dynamic 2d array of a struct.

    Code:
    typedef struct{
        int id;          // Cell id
        HWND child;      // Cell child (either browse,edit,static or combo)
        LPSTR data;      // Text or num if static
    }MITHTABCELLSTRUCT, *LPMITHTABCELLSTRUCT;
    
    typedef struct{
        int cellHeight;  // The height of the cells
        int cellWidth;   // The width of the cells
        int xOffset;     // x offset where cells begin in parent
        int yOffset;     // y offset Where cells begin in parent
        int padding;     // Padding in cells (higher = smaller text, lower = larger text)
        int spacing;     // Spacing between cells
        int rows;        // Number of cell rows
        int cols;        // Number of cell columns
        MITHTABCELLSTRUCT **cells;    //The cells (2d array defined by int rows,cols;)
    }MITHDATATABSTRUCT, *LPMITHDATATABSTRUCT;
    That's the structs, I'm trying to make MITHTABCELLSTRUCT **cells a 2d array. I gather ** was right because I saw it in all the examples.

    It is supposed to be the size of [rows][cols] as it's going to be used to create a table. I just need to write a function to initialize the array.

    If you could explain to me what I need to do rather than showing me the code because I really need to understand what I'm doing (you don't hear that often do you? )..

    Cheers.

    PS: Sorry for spelling/grammar mistakes, I whent to a 21st yesterday and still drunk.

    [edit:]Highlighted the variable
    Last edited by Mithoric; 12-28-2003 at 11:37 PM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Maybe there's something in the STD. But I'll admit, dynamic 2-d's a $$$$$. Could you do a vector of vectors, maybe?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    A 2d array is the best way for me I think, I could copy/paste code but I don't like using code if I don't know how it works .

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    MITHTABCELLSTRUCT **cells

    1. Allocated memory for the rows
    2. For each Row allocate memory for the column


    Code:
    void InitStruct(MITHDATATABSTRUCT* s)
    {
       int y;
       
       s->cells=(MITHTABCELLSTRUCT**)malloc(sizeof(MITHTABCELLSTRUCT*)*ROWS);
    
       for(y=0;y<ROWS;y++)
          s->cells[y]=(MITHTABCELLSTRUCT*)malloc(sizeof(MITHTABCELLSTRUCT)*COLS);
    }

  5. #5
    Thanks.

  6. #6
    Hrmm... Doesn't seem to work. Using debugger I watched the struct and it got every variable set but the cells[][] variable which remained 0x0.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    works 4 me. Try this example

    Code:
    #include <malloc.h>
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    #define ROWS	5
    #define COLS	5
    
    typedef struct{
        int id;          // Cell id
        HWND child;      // Cell child (either browse,edit,static or combo)
        LPSTR data;      // Text or num if static
    }MITHTABCELLSTRUCT, *LPMITHTABCELLSTRUCT;
    
    typedef struct{
        int cellHeight;  // The height of the cells
        int cellWidth;   // The width of the cells
        int xOffset;     // x offset where cells begin in parent
        int yOffset;     // y offset Where cells begin in parent
        int padding;     // Padding in cells (higher = smaller text, lower = larger text)
        int spacing;     // Spacing between cells
        int rows;        // Number of cell rows
        int cols;        // Number of cell columns
        MITHTABCELLSTRUCT **cells;    //The cells (2d array defined by int rows,cols
    }MITHDATATABSTRUCT, *LPMITHDATATABSTRUCT;
    
    
    void InitStruct(MITHDATATABSTRUCT* s)
    {
       int y;
       int x;
       s-> cells=(MITHTABCELLSTRUCT**)malloc(sizeof(MITHTABCELLSTRUCT*)*ROWS);
    
       for(y=0;y<ROWS;y++)
       {
          s-> cells[y]=(MITHTABCELLSTRUCT*)malloc(sizeof(MITHTABCELLSTRUCT)*COLS);
    	  
    	  for(x=0;x<COLS;x++)
    		  s->cells[y][x].id=y*COLS+x;
       }
    }
    
    
    int main(int argc, char* argv[])
    {
    	MITHDATATABSTRUCT test;
    
    	InitStruct(&test);
    
    	int y,x;
    
    	for(y=0;y<ROWS;y++)
    		for(x=0;x<COLS;x++)
    			cout<<test.cells[y][x].id<<endl;
    
    	return 0;
    }

  8. #8
    My mistake, it is working ... it's actually a windows programming error which is causing the out of array bounds crashing.

    I think it's off to the windows programming forum now.

    Thanks a bunch!

    [edit:] W00t, fixed it in a stroke of genius. So no Windows forum post for me Thanks again!
    Last edited by Mithoric; 12-29-2003 at 09:11 AM.

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    http://cboard.cprogramming.com/showt...threadid=37843

    There's some stuff I'd do differently from that post, as that's probably a year old now (IE use std::size_t instead of unsigned int and having some container typedefs and iterators, etc.). Also, a couple of posts in that thread were deleted so you might miss out on a bit of the discussion. You want to read the next to last post in that thread.

    You can also use boost's multi_array

    Edit: It looks like one of Salem's posts between the last 2 replies is missing. If I remember correctly, he suggested another solution which allocates one contiguous block of memory by allocating more memory than youneed for all of the array data and reinterpretting the first few elements as pointers which refered to the different rows in the array. It's another solution which I'd recommend over your current approach, but the one I mention in that thread I'd recommend over both.
    Last edited by Polymorphic OOP; 12-29-2003 at 09:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. put matrix from file into 2d dynamic array
    By meriororen in forum C Programming
    Replies: 3
    Last Post: 06-08-2009, 07:51 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM