Segmentation Fault while adding two arrays

Hi, everyone!
I recently started learning CUDA C++. Here’s, my code

#include <iostream>
#include <math.h>

// kernel
// device code: runs on GPU
__global__
void add (int n, float *x, float *y) {
	// if we use this; it will do computation once per thread
	// for (int i = 0; i < n; i++)
	//	y[i] = x[i] + y[i];

	// Index of current thread within its block
	// int index = threadIdx.x;
	// number of threads in the block
	// int stride = blockDim.x;
	// for (int i = index; i < n; i += stride)
	//	y[i] = x[i] + y[i];

	// grid-stride loop
	// gridDim.x: Contains number of blocks in the grid
	// stride = total number of threads in the grid
	int index = blockIdx.x * blockDim.x + threadIdx.x;
	int stride = blockDim.x * gridDim.x;
	for (int i = index; i < n; i += stride)
		y[i] = x[i] + y[i];
}

int main(void) {
	int N = 1<<20;
	float *x, *y;

	// allocate unified memory
	cudaMallocManaged(&x, N*sizeof(float));
	cudaMallocManaged(&y, N*sizeof(float));

	// initialize x and y arrays on the host
	for (int i = 0; i < N; i++) {
		x[i] = 1.0f;
		y[i] = 2.0f;
	}

	// run kernel on 1M elements on the GPU
	// 2nd param: number of thread in thread block
	// add<<<1, 256>>>(N, x, y);

	int blockSize = 256;
	int numBlocks = (N + blockSize - 1) / blockSize;
	add<<<numBlocks, blockSize>>>(N, x, y);

	// wait for GPU to finish before accessing on host
	cudaDeviceSynchronize();

	// check for errors
	float maxError = 0.0f;
	for (int i = 0; i < N; i++)
		maxError = fmax(maxError, fabs(y[i]-3.0f));
	std::cout << "Max error: " << maxError << std::endl;

	// free memory
	cudaFree(x);
	cudaFree(y);

	return 0;
}

I am currently getting the following error:

Segmentation fault (core dumped)

I am not sure what’s happening here. What’s going on? How can I resolve the error?

Reference: An Even Easier Introduction to CUDA | NVIDIA Developer Blog

Thank you (: