Thread: Creating a dynamic array of very large size

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    15

    Creating a dynamic array of very large size

    Hi all, not used this forum in quite a while but am a bit stuck on a programming assignment and would be grateful for a bit of help.

    My assignment it to write a program that implements two kinds of sorting algorithms and test the speed of these algorithms on different length sequences. I'm all done and the program works but only for sequence lengths below a certain size. The sequence length and number of seqeunces is user defined so the array needs to be dynamic. If I go above roughly [6500][10] the program crashes.

    Could someone advise me on how to initialise a large array.

    Many thanks,
    Will

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Use malloc():
    Code:
    int *dyn_array = malloc(sizeof(*dyn_array) * 65000);
    if(!dyn_array)
    {
      fputs("Memory allocation failed. Run for your lives.\n", stderr);
      exit(EXIT_FAILURE);
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What about the 10 for the second dimension?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Adak View Post
    What about the 10 for the second dimension?
    That's a good question. I have no idea what the OP's original non-dynamic array's type was. I do know that he asked how to initialize a large array dynamically though, and that's what I gave him.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Thanks for the help, I thought it would involve malloc().

    As Adak said, do I need to repeat for the second dimension, and also how do i go about accessing the array. I now get 97 "invalid types `int[int]' for array subscript" when trying to access the array:

    int *sequence = (int *)malloc(sizeof(*seq) * 65000);

    for(row=0;row<m;row++)
    {
    for(col=0;col<n;col++)
    sequence[row][col]=rand();
    }
    Edit: The array is a two-dimensional array of ints

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int dim1 = 65000;
    int dim2 = 10;
    int i;
    
    int **array = malloc(sizeof(*array) * dim1);
    
    for(i = 0;i < dim1;++i)
      array[i] = malloc(sizeof(**array) * dim2);
    
    array[5][2] = 9;  // Accessing example
    If you understand what you're doing, you're not learning anything.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    A variant on itsme86's example that requires just two calls to malloc:
    Code:
    int dim1 = 65000;
    int dim2 = 10;
    int i;
    
    int *data = malloc(sizeof(*data) * dim1 * dim2);
    int **array = malloc(sizeof(*array) * dim1);
    
    for (i = 0; i < dim1; ++i)
        array[i] = &data[i * dim2]
    
    array[5][2] = 9;  // Accessing example
    
    /* ... */
    
    free(array);
    free(data);
    Note that in your actual implementation you may wish to check that malloc does not return a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Y'all have some entertaining ways to malloc a 2D array, for sure.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ROWS 80
    #define COLS 10
    
    void printIt(int **array1)
    {
      int i, j;
      
      for (i = 0; i < ROWS; ++i) {
        for (j = 0; j < COLS; ++j) {
          printf(" %5d  ", array1[i][j]);
        }
         putchar('\n');
      }
    }
    
    int main(void) {
      int i, j;
      int **array;
      
      array = malloc(ROWS * sizeof(int*));
    
      for (i = 0; i < ROWS; +++i)
        array[i] = malloc(COLS * sizeof(int));
      
      for (i = 0; i < ROWS; ++i) {
        for (j = 0; j < COLS; ++j) {
          array[i][j] = i + j;
        }
      }
      printIt(array);
    
      for(i=0;i<ROWS;++i) {
        free(array[i]);
      }
      free(array);
      return 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Quote Originally Posted by Adak
    Y'all have some entertaining ways to malloc a 2D array, for sure.
    Your way is as entertaining as itsme86's way
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by laserlight View Post
    Your way is as entertaining as itsme86's way
    Yeah, I was going to say, "And that's different from my method...how?"

    I do like your method more though. Fewer malloc() calls and a less-fragmented heap.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and Using a dynamic array of C strings?
    By swbluto in forum C Programming
    Replies: 11
    Last Post: 11-17-2009, 07:09 PM
  2. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM