Error with 'cuda-memcheck' in cuda 8.0
<p>It is strange that when I do not add <code>cuda-memcheck</code> before <code>./main</code>, the program runs without any warning or error message, however, when I add it, it will have error message like following.</p>

<pre><code>========= Invalid __global__ write of size 8
=========     at 0x00000120 in initCurand(curandStateXORWOW*, unsigned long)
=========     by thread (9,0,0) in block (3,0,0)
=========     Address 0x5005413b0 is out of bounds
=========     Saved host backtrace up to driver entry point at kernel launch time
=========     Host Frame:/usr/lib/x86_64-linux-gnu/libcuda.so.1 (cuLaunchKernel + 0x2c5) [0x204115]
=========     Host Frame:./main [0x18e11]
=========     Host Frame:./main [0x369b3]
=========     Host Frame:./main [0x3403]
=========     Host Frame:./main [0x308c]
=========     Host Frame:./main [0x30b7]
=========     Host Frame:./main [0x2ebb]
=========     Host Frame:/lib/x86_64-linux-gnu/libc.so.6 (__libc_start_main + 0xf0) [0x20830]
</code></pre>

<p>Here is my functions, a brief introduction on the code, I try to generate a random numbers and save them to a device variable <code>weights</code>, then use this vector to sample from discrete numbers.</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;curand.h&gt;
#include&lt;curand_kernel.h&gt;
#include&lt;time.h&gt;

using namespace std;

#define num 100


__device__ float weights[num];

// function to define seed
__global__ void initCurand(curandState *state, unsigned long seed){
    int idx = threadIdx.x + blockIdx.x * blockDim.x;
    curand_init(seed, idx, 0, &amp;state[idx]);
}


__device__ void sampling(float *weight, float max_weight, int *index, curandState *state){
    int j;
    float u;    
    do{
        j = (int)(curand_uniform(state) * (num + 0.999999)); 
        u = curand_uniform(state); //sample from uniform distribution;
    }while( u &gt; weight[j]/max_weight);
    *index  = j;
}

__global__ void test(int *dev_sample, curandState *state){
    int idx     = threadIdx.x + blockIdx.x * blockDim.x;\
    // generate random numbers from uniform distribution and save them to weights
    weights[idx]    = curand_uniform(&amp;state[idx]);
    // run sampling function, in which, weights is an input for the function on each thread
    sampling(weights, 1, dev_sample+idx, &amp;state[idx]);
}


int main(){ 
    // define the seed of random generator
    curandState *devState;  
    cudaMalloc((void**)&amp;devState, num*sizeof(curandState));

    int *h_sample;
    h_sample    = (int*) malloc(num*sizeof(int));

    int *d_sample;
    cudaMalloc((void**)&amp;d_sample, num*sizeof(float));

    initCurand&lt;&lt;&lt;(int)num/32 + 1, 32&gt;&gt;&gt;(devState, 1);
    test&lt;&lt;&lt;(int)num/32 + 1, 32&gt;&gt;&gt;(d_sample, devState);

    cudaMemcpy(h_sample, d_sample, num*sizeof(float), cudaMemcpyDeviceToHost);

    for (int i = 0; i &lt; num; ++i)
    {
        cout &lt;&lt; *(h_sample + i) &lt;&lt; endl;
    }

    //free memory
    cudaFree(devState);
    free(h_sample);
    cudaFree(d_sample);
    return 0;
}
</code></pre>

<p>Just start to learn cuda, if the methods to access the global memory is incorrect, please help me with that. Thanks</p>