local memory array initialization

I’ve searched through the forum and couldn’t find a post on this and thought I’d at least bring it up.

I was under the impression that I had an initialized array in local memory when I performed the following inside a kernel:

float arr[32] = {0.0f};

In retrospect, I was a bit too confident in believing the compiler would allocate it in global memory and initialize it to zero.

It turns out there is still some junk in that array when I actually compute some values.

I do see different behavior between:

float arr[32];
float arr[32] = {0.0f};


float arr[32];

   for (int i=0; i < 32; i++)

	   arr[i] = 0.0f;

It resulted in arrays with more to less, to no junk, respectively.

I found this when I was forced to initialize in a loop for a local shared memory implementation, but had it not been for that, I would have been operating with faulty code.

Has anyone else experienced this? Should I do something special to have it actually initialize the array to some value?

You missed a comma… Otherwise only the first entry will be zero…

This is the right way:

float arr[32] = {0.0f , };

Disclaimer: I am not a language expert. I might be wrong. But try it out.

Thanks for the recommendation but this had the same effect as setting the array to {0.0f}, definitely didn’t zero out the entire array still.