C++ arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 27
Reputation: thilinam is an unknown quantity at this point 
Solved Threads: 0
thilinam thilinam is offline Offline
Light Poster

C++ arrays

 
0
  #1
Jul 2nd, 2009
Please tell me how to create a multi diminsional array using pointer(dynamic array, set size at run time).
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,633
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: C++ arrays

 
0
  #2
Jul 2nd, 2009
Declare pointer to pointer variable,
  1. int **ptr;
How many one dim arrays?
  1. p=new int*[4];
What is the size of each one array?
  1. *(p+0)=new int[3];
  2. *(p+1)=new int[3];
  3. *(p+2)=new int[3];
  4. *(p+3)=new int[3];

How to freed allocated memory?
  1. delete []p;
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: C++ arrays

 
0
  #3
Jul 2nd, 2009
I'd recommend a container class where you pass two axis but gets resolved to a single array allocation.

This is only one example of how to solve this!

  1. class IntArray
  2. {
  3. private:
  4. int *pInt;
  5. unsigned int nWidth;
  6. unsigned int nHeight;
  7.  
  8. public:
  9. IntArray( unsigned int w, unsigned int h );
  10. ~IntArray();
  11. void Set( unsigned int x, unsigned int y, int val );
  12. int Get( unsigned int x, unsigned int y );
  13. };
  14.  
  15. void IntArray::IntArray( unsigned int w, unsigned int h )
  16. {
  17. pInt = new int[ w * h ];
  18. nWidth = w;
  19. nHeight = h;
  20. }
  21.  
  22. IntArray::~IntArray()
  23. {
  24. if (NULL != pInt)
  25. {
  26. delete[] pInt;
  27. }
  28. }
  29.  
  30. int IntArray::Get( unsigned int x, unsigned int y )
  31. {
  32. return pInt[ y * nWidth + x );
  33. }
  34.  
  35. void IntArray::Set( unsigned int x, unsigned int y, int val )
  36. {
  37. pInt[ y * nWidth + x ] = val;
  38. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ arrays

 
0
  #4
Jul 2nd, 2009
Originally Posted by adatapost View Post
How to freed allocated memory?
  1. delete []p;
Don't you also have to delete each p array?

If you aren't sure, the answer is yes, delete each array first before deleting p itself.
Last edited by WaltP; Jul 2nd, 2009 at 3:15 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: C++ arrays

 
0
  #5
Jul 2nd, 2009
Each new[] array requires a delete.
So in that other variation, each branch leg has to be deleted first then the backbone.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,633
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: C++ arrays

 
0
  #6
Jul 2nd, 2009
wildgooe and WaltP,
Thanks for correction.

delete has two forms:
1. delete pointer - deallocates a single element
2. delete []pointer - deallocates arrays of elements.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: C++ arrays

 
0
  #7
Jul 2nd, 2009
You could use std::vector's of std::vector's - then you don't have to worry about new() and delete().
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: C++ arrays

 
0
  #8
Jul 2nd, 2009
I would recommend:
  1. int **array;
  2. int sizeX,sizeY;
  3. sizeX = 5; // set to whatever
  4. sizeY = 5; // set to whatever
  5. array = new int*[sizeY];
  6. for(int i = 0; i < sizeY; i++)
  7. array[i] = new int[sizeX];
  8.  
  9. // Then to delete:
  10. for(int i = 0; i < sizeY; i++)
  11. delete array[i];
  12. delete [] array; // i think you need the [] here but not sure.

---------------------
This is what i used in something i made, but remade it here in 5 mins. No guarantee it works -.- Even though it should...
Last edited by u8sand; Jul 2nd, 2009 at 10:02 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ arrays

 
0
  #9
Jul 2nd, 2009
line 11 of that code snippet is wrong. It should be delete[], not delete.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: C++ arrays

 
0
  #10
Jul 2nd, 2009
O Sorry, it don't let me edit now -.-
Here:
  1. int **array;
  2. int sizeX,sizeY;
  3. sizeX = 5; // set to whatever
  4. sizeY = 5; // set to whatever
  5. array = new int*[sizeY];
  6. for(int i = 0; i < sizeY; i++)
  7. array[i] = new int[sizeX];
  8.  
  9. // Then to delete:
  10. for(int i = 0; i < sizeY; i++)
  11. delete [] array[i];
  12. delete [] array;
Last edited by u8sand; Jul 2nd, 2009 at 10:43 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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