Thread: dynamic array of pointers, how to initialize quickly?

1. dynamic array of pointers, how to initialize quickly?

I still have trouble understanding how to handle initialization of an array when its very large and unmanageable, i am trying to test out memset function to see if that will do the trick.

I have an array of pointers for a 'multidimensional array' ..I need the something that will do this here..without a loop that takes so much time.

size of len1 will be around ~250,000
Code:
```

int **dp = new int*[2];
for(int i=0;i<2;++i)
dp[i] = new int[len1+1];

// Really need a faster solution for this!...250,000*2 loops is no good.
for(int i=0;i<2;++i)
for(int j=0; j<len1+1;++j)
dp[i][j] = 0;```
how can I use memset to place 0's in every spot of this kind of array?

I see this example somewhere..for normal array.
But, it gets complicated with pointer array of pointers...bleh
Code:
`memset (an_array, 0, sizeof(an_array));`
any ideas? should I be using memset?

Thanks a ton!

2. You should use memset on each of the two sub-arrays.

If you are not using a buggy compiler, you can avoid memset altogether:
Code:
```int **dp = new int*[2];
for(int i=0;i<2;++i)
dp[i] = new int[len1+1]();```

3. is it as quick?

4. Originally Posted by rodrigorules
is it as quick?
As what?

5. You should probably use a vector instead of pointers. Anyhow, memset is not possible here. memset works on contiguous arrays and obviously this isn't. It's more like a tree, so the only solution is a loop. Why do you need to 0 it in the first place, though?

6. as memset

sorry i lack the knowledge on how the exactly the program sets the data..might be exactly the same way

(i need zeros in it because I will sometimes accessing array without possibly assigning a value to it previously...i need it to be 0 by default)

edit: hmm the () notation at the end of array doesn't seem to work for me.

7. Originally Posted by rodrigorules
as memset
It should be no worse, but as I mentioned before, I have used a compiler that has a bug such that this value initialisation does not work on an array of objects of built-in types.

By the way, why is dp a pointer to a pointer? It should be an array of 2 pointers, or an array of 2 vectors.

8. why do you guys recommend vectors?

9. Originally Posted by rodrigorules
why do you guys recommend vectors?
How do I deal with memory leaks?
What's wrong with arrays?

10. Unless you are repeatedly initializing the array, the initialization time simply isn't going to matter. Use a loop.

11. 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.

12. Why not just doing one big malloc (which is a lot faster than calling new for every byte of your array) and calculate pointer offsets in a for loop? The fact that you are calling new for every item in your array is what cause it to be slow, unless I misunderstood your code or what you are trying to do.

13. he's only doing 2 allocations.

14. Why not malloc? Because this is C++, and malloc is bad in C++.
It is possible to do...
int (* p)[100] = new int[100][100];
...to get a pointer to a 2D array (which is contiguous). However, once again, you should be using vector if possible.

15. Oh ok nevermind I thought he was doing 2*250000 allocations.