sizeof(an_array) only returns the number of bytes in the array for static arrays. if you try this with a dynamically allocated array, you will return sizeof(pointer)
if you were to use memset, you would need to:
memset(ptr,0,sizeof(type)*length)
if you wanted to initialize the memory all at once, you would have to do the indexing yourself.
Code:
int* 2darray = new int[height*width];
memset(2darray,0,sizeof(int)*height*width);
you lose the [][] notation this way, so you'd do better to use a wrapper class e.g:
Code:
class 2darray
{
private:
int* buf;
public:
2darray(unsigned int _width,unsigned int _height):
height(_height),
width(_width),
buf(new int[_height*_width])
{
memset(buf,0,sizeof(int)*height*width);
}
const unsigned int height;
const unsigned int width;
int* operator[](unsigned int y)
{
return buf[width*y];
}
~2darray()
{
delete[] buf;
}
};
//uncompiled, untested - simply for demonstration purposes.
using the STL is probably the better choice, however.