allocating a 2D array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

allocating a 2D array

 
0
  #1
Nov 8th, 2008
How can I allocate a 2D array? (on the heap) It needs to be dynamically allocated because the values of rows and columns aren't known.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: allocating a 2D array

 
0
  #2
Nov 8th, 2008
You can't use vectors?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: allocating a 2D array

 
0
  #3
Nov 8th, 2008
Originally Posted by Dave Sinkula View Post
You can't use vectors?
No, it's an exercise I have to do.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: allocating a 2D array

 
0
  #4
Nov 8th, 2008
Make pointer to pointer **ptr , and allocate n pointers to it.
Then to each of that n pointers *ptr[i] allocate n int's (or whatever).
After that you can use it as regular 2D array: ptr[i][j]
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: allocating a 2D array

 
0
  #5
Nov 8th, 2008
It's not a problem to allocate 2D array or what else in C++. The problem is: how do you want access the allocated storage. You know that it's impossible to declare 2D array variable with run-time defined extents in C and C++.

The most straightforward approach in C style but with templates (w/o argument check):
  1. template <typename T>
  2. T** new2D(size_t m, size_t n)
  3. {
  4. T** pp = new T*[m];
  5. T* p = new T[m*n];
  6. for (size_t i = 0; i < m; ++i, p += n)
  7. pp[i] = p;
  8. return pp;
  9. }
  10. ...
  11. double** pa2d = new2D<double>(m,n);
  12. ... pa2d[i][j]...
  13. // to deallocate:
  14. delete pa2d[0];
  15. delete pa2d;
More elaborated approaches: define your own class encapsulated all mechanics described above, use vector<vector< >> or valarray<valarray< >> templates. Search this forum: it was a very popular theme 1 or 2 month ago.

Search inet: there are lots of Matrix classes which implements 2D dynamic arrays logics.
Last edited by ArkM; Nov 8th, 2008 at 3:44 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC