C++ 2D arrays

This topic is 4968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Thanks for the replies guys! I didn't have any idea that I couldn't use multiple
[] operators!

Share on other sites
Quote:
 Original post by CacksThanks 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.

Share on other sites
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;

Share on other sites
Quote:
 Original post by BornToCodedo this man:**code removed**

No. There's no reason whatsoever to use malloc().

Share on other sites
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.

Share on other sites
No offense Frunny but what is wrong with malloc.

Share on other sites
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.

Share on other sites
Quote:
 Original post by BornToCodeNo 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.

Share on other sites
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

Share on other sites
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.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 29
• 15
• 42
• Forum Statistics

• Total Topics
634843
• Total Posts
3019581
×