| | |
C++ arrays
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Declare pointer to pointer variable,
How many one dim arrays?
What is the size of each one array?
How to freed allocated memory?
C++ Syntax (Toggle Plain Text)
int **ptr;
C++ Syntax (Toggle Plain Text)
p=new int*[4];
C++ Syntax (Toggle Plain Text)
*(p+0)=new int[3]; *(p+1)=new int[3]; *(p+2)=new int[3]; *(p+3)=new int[3];
How to freed allocated memory?
C++ Syntax (Toggle Plain Text)
delete []p;
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!
This is only one example of how to solve this!
C++ Syntax (Toggle Plain Text)
class IntArray { private: int *pInt; unsigned int nWidth; unsigned int nHeight; public: IntArray( unsigned int w, unsigned int h ); ~IntArray(); void Set( unsigned int x, unsigned int y, int val ); int Get( unsigned int x, unsigned int y ); }; void IntArray::IntArray( unsigned int w, unsigned int h ) { pInt = new int[ w * h ]; nWidth = w; nHeight = h; } IntArray::~IntArray() { if (NULL != pInt) { delete[] pInt; } } int IntArray::Get( unsigned int x, unsigned int y ) { return pInt[ y * nWidth + x ); } void IntArray::Set( unsigned int x, unsigned int y, int val ) { pInt[ y * nWidth + x ] = val; }
Don't you also have to delete each 
If you aren't sure, the answer is yes, delete each array first before deleting
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
I would recommend:
---------------------
This is what i used in something i made, but remade it here in 5 mins. No guarantee it works -.- Even though it should...
C++ Syntax (Toggle Plain Text)
int **array; int sizeX,sizeY; sizeX = 5; // set to whatever sizeY = 5; // set to whatever array = new int*[sizeY]; for(int i = 0; i < sizeY; i++) array[i] = new int[sizeX]; // Then to delete: for(int i = 0; i < sizeY; i++) delete array[i]; 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.
O Sorry, it don't let me edit now -.-
Here:
Here:
C++ Syntax (Toggle Plain Text)
int **array; int sizeX,sizeY; sizeX = 5; // set to whatever sizeY = 5; // set to whatever array = new int*[sizeY]; for(int i = 0; i < sizeY; i++) array[i] = new int[sizeX]; // Then to delete: for(int i = 0; i < sizeY; i++) delete [] array[i]; delete [] array;
Last edited by u8sand; Jul 2nd, 2009 at 10:43 am.
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: I'm confused
- Next Thread: slowly but surely
Views: 545 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






