Jump to content
  • Advertisement
Sign in to follow this  
Cacks

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 directly

Array2D<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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by BornToCode
do this man:
**code removed**


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

Share this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!