C++ 2D arrays

Started by
32 comments, last by Cacks 18 years, 8 months ago
Thanks for the replies guys! I didn't have any idea that I couldn't use multiple
[] operators!
Reject the basic asumption of civialisation especially the importance of material possessions
Advertisement
Quote:Original post by Cacks
Thanks for the replies guys! I didn't have any idea that I couldn't use multiple
[] operators!


Well, you can if you are clever, by using an indexable proxy.

template<class T>class Array2D{   T* data_;   size_t rows_;   size_t cols_;public:   Array2D(size_t rows, size_t cols) :       data_(new T[rows*cols]),      rows_(rows),      cols_(cols)   {}   ~Array2D()   {      delete[] data_;   }   T* operator[](size_t row) { return data_+cols_*row; }private:   // Arrays are non-copyable   Array2D(const Array2D&);   Array2D& operator=(const Array2D&);};


The magic in the class's operator[] is that it takes care of skipping to the correct column - which a simple T* wouldn't do, then, because it returns a pointer, you can still apply another operator[] (to the pointer) and move to the right column.

Array2D<int> arr(25,4);int* arr_10 = arr[10];int arr_10_2 = arr_10[2];// or directlyArray2D<int> arr(25,4);int arr_10_2 = arr[10][2];


If you wanted a 3- or higher-dimensional array, you would have to actually use proxy classes (a 3D array returns a 2D array proxy which returns a 1D array proxy). The pointer only works as a 1D array proxy.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
do this man:

int width=10;
int height=12;
int i=0;
int **array=(int**)malloc(sizeof(int*)*width);
for(i=0;i<width;i++)
array=(int*)malloc(sizeof(int)*height);

//Now to Ascees it you can do this
array[0][0]=12;

//Now to delete it
for(i=0;i<width;i++)
free(array);
free(array);
array=NULL;
Quote:Original post by BornToCode
do this man:
**code removed**


No. There's no reason whatsoever to use malloc().
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Oluseyi's #2 is the best method in terms of speed and ease of creation/deletion. The other 2 methods fragment the array into different parts of memory. #2 keeps it all contiguous, thus very cache friendly and fast.
No offense Frunny but what is wrong with malloc.
I'm sure he meant in C++ there's no reason to use malloc, you should always use new/delete. malloc/free don't call constructors/destructors so while they work fine on basic types like ints, as soon as you start trying to use them for classes you will run into problems, so might as well just use new/delete always.
Quote:Original post by BornToCode
No offense Frunny but what is wrong with malloc.


1) The need to cast the result to int**.
2) The need to keep track of the type's size.
3) The fact that once you start using malloc() in a C++ program, you have pointers that need to be released with delete and others by free() and you have no way of knowing which is which.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
For more explanation of the reasons, here's a decent article

"To new is C++; To malloc is C; To mix them is sin"
http://www.codeproject.com/tips/newandmalloc.asp
Dr Evil get your fact straigh about you saying when you start using structures that free is not good. You really have no idea what the heck you are talking about and before you make such stupid comment get it right. You people are so stuck up to C++ and crap that you think that everything in C is bull. So i will stick to my malloc because i found it easier to used than the new operator especially when i have to delete things. i do not have to worry about putting a freaking [] in front of my object name when i used an array. So that is my two cent on this topic.

This topic is closed to new replies.

Advertisement