Hi all!
I'm having a trouble with the operator new[] used to allocate an array of long unsigned int.
I rely on the operator that all the array items are initialized to zero, as mentioned in
http://www.cplusplus.com/reference/std/new/operator%20new%5B%5D/ :
... it then automatically initializes or constructs each of the objects in the array (if needed). ... |
Somewhere in my program I use the array items as conditions of an
if clause. Nevertheless valgrind throws an error here saying "
Conditional jump or move depends on uninitialised value(s)".
Here's a simplified code of current, problematic state:
1 2 3
|
unsigned long int *plData = new unsigned long int[width*height];
...
if(plData[0]) ...
|
I found out how to simply solve the problem:
1 2 3 4
|
unsigned long int *plData = new unsigned long int[width*height];
memset((void*)plData, 0, width*height*sizeof(unsigned long int));
...
if(plData[0]) ...
|
Yes, the solution is simple, but my question is:
Isn't the memset() call redundant? I do think so since the data is declared to be initialized after the new[] call. Isn't the problem in valgrind? Or is it unsafe to rely on this functionality of the operator new[].
Now the program is compiled using g++, nevertheless it's also cross-compiled using arm-none-linux-gnueabi-g++ and possibly others, so I need to be sure it's not dependent on the compiler.
Thanks in advance!
yman