dynamic memory and arrays

This is a discussion on dynamic memory and arrays within the C++ Programming forums, part of the General Programming Boards category; I am learning c++ and a found an example on pointers and arrays: .. int n; cout << "Give an ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    dynamic memory and arrays

    I am learning c++
    and a found an example on pointers and arrays:

    ..
    int n;
    cout << "Give an integer: \n" ;
    cin >>n;

    int *a;
    a = new int[n];

    ..

    My question is if it is possible to create a 2 dimensional array with pointers in order to create a board. I've tried:

    ... int h, w;

    cout << "Give the high of the board \n";
    cin >> h;
    cout << "Give the width of the board \n";
    cin >> l;

    int *a;
    a = new int[h][w];
    ...

    But doing it like this gives me a lot of errors. What I'am trying to do is create a board of h by w with dynamic memory.

    Does anybody knows how to do this?

    Thanks anyway

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    int h, w;
    
    cout << "Give the high of the board \n";
    cin >> h;
    cout << "Give the width of the board \n";
    cin >> l; //Could be the fact that you're using l instead of w
    
    int *a;
    a = new int[h][w];
    If that's not the problem, then try using cin.ignore() after your first cin statement.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    dynamic memory

    I am sorry that was typed wrong, what I meant was:

    Code:
    int h, w;
    cout << "Give the high of the board \n";
    cin >> h;
    cout << "Give the width of the board \n";
    cin >> w; 
    
    int *a;
    a = new int[h][w];
    And I just cheked by compiling this and I still get the same errors.

    Any suggestions?

    Thanks,

    Jonathan

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    sorry, i didn't see what you were doing.

    There are many threads about dynamic 2D arrays. Do a search and you will find many.

    Here's some code for a 2D int array:
    Code:
    int **array;
    
    int h, w, i;
    cout << "Give the high of the board \n";
    cin >> h;
    cout << "Give the width of the board \n";
    cin >> w; 
    
    array = new int*[h];
    
    for( i = 0; i < h; i++ )
    {
         array[i] = new int[w];
    }
    
    //...
    //Here you would use it
    //...
    
    for( i = 0; i < h; i++ )
    {
         delete [] array[i];
    }
    
    delete [] array;

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    thanks

    Thanks that was what I was looking for!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Dynamic Memory and Arrays
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2006, 05:09 AM
  3. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 02:26 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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