Dynamic double array

This is a discussion on Dynamic double array within the C++ Programming forums, part of the General Programming Boards category; Using pointers, how would you declare a dynamic double array to the effect of: int matrix = new int[var][var]; where ...

  1. #1
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Dynamic double array

    Using pointers, how would you declare a dynamic double array to the effect of:

    int matrix = new int[var][var];

    where var is the size of the array.

    ??
    "The mind, like a parachute, only functions when open."

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    It's quite similar to what you do to unidimensional arrays. But with a slight "problem":

    Code:
    int* matrix=0;
    //.. get the dimension from user input or processing (dim1)
    if(!(matrix = new int[dim1][const]))
       {
          cout << endl
               << "Memory allocation failed.";
          exit(1);               
       }
    The problem as you can see is you can only specify the first dimension as a variable. All subsequent dimensions must be constants.

    Also don't forget that no matter the number of dimensions, the delete statement only has one pair of square brackets:

    delete [] matrix;
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Yes... that was the problem I was facing. For anyone who's interested in the solution the answer is here:

    http://www.cprogramming.com/cboard/s...c+double+array
    "The mind, like a parachute, only functions when open."

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    He! I was finding it strange someone like you making such a simple question.
    Sorry for misreading your question
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    No prob Mario, you helped me realize that conventional methods weren't going to work. My thanks.
    "The mind, like a parachute, only functions when open."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array malloc()
    By el_chupacabra in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 06:46 AM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  3. Replies: 4
    Last Post: 11-02-2006, 10:41 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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