943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 865
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 2nd, 2009
0

C++ arrays

Expand Post »
Please tell me how to create a multi diminsional array using pointer(dynamic array, set size at run time).
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
thilinam is offline Offline
35 posts
since Sep 2008
Jul 2nd, 2009
0

Re: C++ arrays

Declare pointer to pointer variable,
C++ Syntax (Toggle Plain Text)
  1. int **ptr;
How many one dim arrays?
C++ Syntax (Toggle Plain Text)
  1. p=new int*[4];
What is the size of each one array?
C++ Syntax (Toggle Plain Text)
  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?
C++ Syntax (Toggle Plain Text)
  1. delete []p;
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 2nd, 2009
0

Re: C++ arrays

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!

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: C++ arrays

Click to Expand / Collapse  Quote originally posted by adatapost ...
How to freed allocated memory?
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jul 2nd, 2009
0

Re: C++ arrays

Each new[] array requires a delete.
So in that other variation, each branch leg has to be deleted first then the backbone.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: C++ arrays

wildgooe and WaltP,
Thanks for correction.

delete has two forms:
1. delete pointer - deallocates a single element
2. delete []pointer - deallocates arrays of elements.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 2nd, 2009
0

Re: C++ arrays

You could use std::vector's of std::vector's - then you don't have to worry about new() and delete().
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jul 2nd, 2009
0

Re: C++ arrays

I would recommend:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 2nd, 2009
0

Re: C++ arrays

line 11 of that code snippet is wrong. It should be delete[], not delete.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 2nd, 2009
0

Re: C++ arrays

O Sorry, it don't let me edit now -.-
Here:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I'm confused
Next Thread in C++ Forum Timeline: slowly but surely





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC