Thread: Window sized 2-d array!

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    13

    Window sized 2-d array!

    Hi,

    I'm still getting used to C and I've come across something I'm slightly stuck on:

    I need to make a 2-d array of characters array[I][J] that matches the width and the height of the output window i.e. (I-1)x(J).

    Any ideas of how I can work out the size for varying output screen sizes?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    There is no direct way to dynamically allocate a 2-D array such that you can index by row/col.
    You might as well malloc() for a single dimension array and then index using your own offset calculation row x width + col. Or alternatively you could make an array of pointers, each element being a pointer that's a malloc()'d row. But I'd rather not push my luck with 100s of mallocs and frees.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nonoob View Post
    There is no direct way to dynamically allocate a 2-D array such that you can index by row/col.
    You might as well malloc() for a single dimension array and then index using your own offset calculation row x width + col. Or alternatively you could make an array of pointers, each element being a pointer that's a malloc()'d row. But I'd rather not push my luck with 100s of mallocs and frees.
    I admit I use arrays of pointers to arrays of char's for 2D arrays like this, but I don't see why this can't be done?

    It won't be as space efficient, but if you know the dimensions of the window, I don't see why it can't be malloc'd with the proper sizes. Instead of the first dimension being sized for a pointer, we just size it for a char and Bob's your Uncle.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Just use bitmap style addressing...
    Allocate a single memory buffer that is height*width in size, and address each item using [i*width+j].
    You'll have to give up that [i][j] syntax, but you can always make a function that performs the calculation above and gives you an (i, j) syntax.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you saying that if you know the size and the width you want to malloc for a 2D array, that you can't malloc it as a 2D array?

    Of course, the array would be actually one chunk of contiguous memory, but so is a declared 2D array, right?
    Last edited by Adak; 11-20-2009 at 02:40 AM.

  6. #6
    Registered User lattica's Avatar
    Join Date
    Aug 2008
    Location
    Spacelike Hyperplane
    Posts
    16
    The [i][j] syntax can be recovered by allocating an array of pointers, as well as the linear chunk, and setting each of these to point to each row:

    Code:
    char *outputWindowArray;
    char **outputWindow;
    
    /* malloc contiguous chunk */
    outputWindowArray = (char*) malloc(sizeof(*outputWindowArray) * numRows * numCols);
    /* malloc pointers, these will point to beginning of each row */
    outputWindow = (char**) malloc(sizeof(*outputWindow) * numRows);
    
    /* now set each pointer to row, there is a stride of numCols between each row */
    for(i=0; i<numRows; i++)
    	outputWindow[i] = &outputWindowArray[i * numCols];
    
    /* access row i, column j */
    rowi_colj = outputWindow[i][j];
    
    /* freedom */
    free(outputWindow);
    free(outputWindowArray);

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    /* freedom */
    free(outputWindow);
    You meant to say:
    /* memory leak */
    free(outputWindow);

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User lattica's Avatar
    Join Date
    Aug 2008
    Location
    Spacelike Hyperplane
    Posts
    16
    Quzah, please elaborate.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by lattica View Post
    Quzah, please elaborate.
    You have to free each row you allocate. For every malloc call, you should have a corresponding free call:
    Code:
    char **array;
    size_t rows, cols, x;
    
    rows = 5;
    cols = 6;
    
    array = malloc( rows * sizeof *array ); /* allocate row holders */
    for( x = 0; x < rows; x++ )
        array[ x ] = malloc( cols );  /* allocate rows of columns */
    ...
    
    for( x = 0; x < rows; x++ )
        free( array[ x ] ); /* free each row of columns */
    free( array ); /* free row holders */

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User lattica's Avatar
    Join Date
    Aug 2008
    Location
    Spacelike Hyperplane
    Posts
    16
    I see; you have misread the code snippet I posted above.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by lattica View Post
    I see; you have misread the code snippet I posted above.
    Looks like it.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    Thanks for replys guys! Ill let you know how I get on!

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    I've walked into another problem..I've made a program that generates a matrix of random numbers but everytime I run the program it gives me the same 'random' numbers any ideas to why or how I can change this? I free the arrays once the main part of the program has lead its course I just don't understand why it would keep creating the same random matrix?

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    solved! cheers

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Chrisboggis View Post
    I've walked into another problem..I've made a program that generates a matrix of random numbers but everytime I run the program it gives me the same 'random' numbers any ideas to why or how I can change this? I free the arrays once the main part of the program has lead its course I just don't understand why it would keep creating the same random matrix?
    That's done on purpose, for testing purposes. If you want different random sequences, just srand() the random seeder, with different values (data from the computers timer is frequently used).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM