Swap values causes Segmentation fault

When I try to swap values it causes a Segmentation fault.
I’ve commented the part with “HERE THE CRASH HAPPENS”, at the point were the Segmentation fault is triggered.
If I comment that part out everything works fine.

Here’s my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

global
void calc_and_swap(char key, char plaintext, int len_plaintext, int len_key, int *results) {
int index = threadIdx.x;
int stride = blockDim.x;

unsigned char ciphertext[200];

unsigned char S[256];

int j = 0;

for(int i = 0; i < 256; i++)
    S[i] = i;

for(int i = 0; i < 256; i++) {
    j = (j + S[i] + key[i % len_key]) % 256;
    int tmp = S[i];
    S[i] = S[j]; <--- HERE THE CRASH HAPPENS
    S[j] = tmp; <--- HERE THE CRASH HAPPENS
}

int main(void){
int N = 10000;
int results;
cudaMallocManaged(&results, N
sizeof(int));
for (int i = 0; i < N; i++) {
results[i] = 0;
}

calc_and_swap<<<1,1>>>(key, plain, len_plaintext, len_key, results);
cudaDeviceSynchronize();
printf("Done\n");
cudaFree(results);
return 0;

}

That code won’t compile. Device code can’t cause a seg fault.

Run your code with cuda-memcheck.