2D array in dynamic memory

This is a discussion on 2D array in dynamic memory within the C++ Programming forums, part of the General Programming Boards category; Hello, I am attempting to create and access a 2d array in dynamic memory. I can easily create the array ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    2D array in dynamic memory

    Hello,

    I am attempting to create and access a 2d array in dynamic memory. I can easily create the array in stack memory with the following code:

    Code:
    double d[5][5];
    My question is how to do this with dynamic memory? I have an integer variable entitled vertNum to represent the number of vertices.

    How do I create an array in dynamic memory using the vertNum variable?

    So far I have the code below. How do I set the column of each and access the column?

    Code:
    double ** row = new double * [vertNum];
    double * col = new double [vertNum];
    *row = col;
    Last edited by VanJay011379; 03-15-2003 at 10:45 AM.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    double **d = new double*[5];
    
    for( int i = 0; i < 5; i++ ) {
    
    	d[i] = new double[5];
    
    }
    And to delete the memory used...

    Code:
    for( int j = 0; j < 5; j++ ) {
    
    	delete[] d[j];
    
    }
    
    delete[] d;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Thanks,

    One more thing.

    How would I pass the 2D array to a function?

    For instance, if I have a function called foo, how would it get passed and received by another function foo?

    Like the following:

    Code:
    //call the function
    foo(twoDimArray);
    Code:
    //call the function
    void foo(double **twoDimArray);
    Would I have access to all element twoDimArray in foo?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Would I have access to all element twoDimArray in foo?
    Yes.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic 2D array allocation
    By deprekate in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 03:25 AM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. creating a dynamic 2d array?
    By der dom in forum C Programming
    Replies: 11
    Last Post: 09-28-2004, 05:16 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

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