| | |
C++ arrays
Please support our C++ advertiser: Intel Parallel Studio Home
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;
Failure is not fatal, but failure to change might be. - John Wooden
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
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






