Allocating memory...

This is a discussion on Allocating memory... within the C++ Programming forums, part of the General Programming Boards category; Hi all! I have a problem. I need to be able to modify the size of a multidimensional array, and ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Location
    Melbourne, Australia
    Posts
    92

    Unhappy Allocating memory...

    Hi all!
    I have a problem. I need to be able to modify the size of a multidimensional array, and I made a li'l test doing it with an array that only has one dimension successfully... (i'm using "php" tags rather than code tags cuz well.... it just looks cooler!)
    PHP Code:
    int max 8;           // initial size of aray
    int= new int[max];  // allocated on heap

    for (int j=0j<maxj++)
    {
        
    a[j]=j;
    }

    max max 2;            // halve the previous size
    inttemp = new int[max]; // create new bigger array.
    for (int i=0i<maxi++)
    {
        
    temp[i] = a[i];       // copy values to new array.
    }
    delete [] a;              // free old array memory.
    temp;                 // now a points to new array.
        
    for (j=0j<maxj++)
    {
        
    printf("%d\n"a[j]);
    }

    printf("Done - hit a key you ass!");
    getch(); 
    The problem is, i've heard some scary **** - that all dimensions other than the first must be static values even when allocating memory in this way. Can anyone help me out with a good method of dynamically creating a 2 dimensional array?

    Thanks in advance ppl!
    Last edited by PsychoBrat; 01-17-2003 at 11:08 PM.
    psychobrat at gmail

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    I forget who originally wrote this code...

    Code:
    #include <iostream>
    using namespace std;
    
    int main (void) {
    
      // 2-dimensional dynamic matrix 
      int **dynamicMatrix = NULL;
    
      int numRows    = 0;
      int numColumns = 0;
    
      int row    = 0;
      int column = 0;
    
      cout << "Enter number of rows: ";
      cin >> numRows;
    
      cout << "Enter number of columns: ";
      cin >> numColumns;
    
      dynamicMatrix = new int*[ numRows ];
    
      for( row = 0; row < numRows; row++ ) {
    
        dynamicMatrix[ row ] = new int[ numColumns ];
      }
    
      // set it all to 5
      for( row = 0; row < numRows; row++ ) {
    
        for( column = 0; column < numColumns; column++ ) {
    
          dynamicMatrix[ row ][ column ] = 5;
        }
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Location
    Melbourne, Australia
    Posts
    92
    oooooooooooooh!

    Thankyou soooo much!
    psychobrat at gmail

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 11:48 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  4. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 08:45 AM
  5. Replies: 5
    Last Post: 11-24-2002, 11:33 PM

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