Help: adding two arrays (beginner)

Hello, I’m trying to write my first program in OpenC using C++ bindings, but I’m getting errors. Since it’s my first program and I don’t have much experience, I don’t know exactly if I’m doing things wrong or not. I’m getting "“Access violation reading location 0x00000000” on func().wait() call. Source code’s here:

#define __NO_STD_VECTOR

#define __NO_STD_STRING

#include <malloc.h>

#include <cl.hpp>


using namespace std;

using namespace cl;

const char *kernelSrc = “__kernel void”

              "vectorAdd(__global const int *a,"

              "__global const int *b,"

              "__global int *resultado)"                  


              "// Vector element index"

              "int nIndex = get_global_id(0);"

              "resultado[nIndex] = a[nIndex] + b[nIndex];"


int main(void)


const int size = 4;

// get platform

vector platformList;


cout << "Platform number is: " << platformList.size() << endl;

// show platform info

cl::string platformInfo;

platformList[0].getInfo(CL_PLATFORM_VERSION, &platformInfo);

cout << "Platform Info: " << platformInfo.c_str() << endl;

// get available devices

vector devices;

platformList[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);

cout << devices.size() << " device(s) found!" << endl;

// show device info

cl::string name;

devices[0].getInfo(CL_DEVICE_NAME, &name);

cout << "Device name: " << name.c_str() << endl;

// create OpenCL context

Context hContext(devices, 0, NULL, NULL, NULL);

// queue

CommandQueue queue(hContext, devices[0], 0, 0);

// Load source

cl::Program::Sources source(1, std::make_pair(kernelSrc,strlen(kernelSrc)));

// create and build program

Program program(hContext, source, NULL);, NULL, NULL, NULL);

// Get the kernel so we can use it

Kernel kernel(program, “vectorAdd”, NULL);

// arrays on host

int *a = new int;

int *b = new int;

int *c = new int;

// initialize

a[0]=1; a[1]=2; a[2]=20; a[3]=8;

b[0]=29; a[1]=28; a[2]=10; a[3]=22;

// result should be {30, 30, 30, 30}

// Allocate memory on device and copy the arrays there

Buffer bufferA(hContext, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * size, a, NULL);

Buffer bufferB(hContext, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * size, b, NULL);

// Create buffer to get output on kernel

Buffer bufferResultado(hContext, CL_MEM_WRITE_ONLY, sizeof(int)*size, NULL, NULL);

// set arguments

kernel.setArg(0, bufferA);

kernel.setArg(1, bufferB);

kernel.setArg(2, bufferResultado);

// execute kernel

NDRange global(size);

NDRange local(2);

KernelFunctor func = kernel.bind(queue, global, local);

func().wait(); // error here…

cout << “Done!” << endl;

// copy from device to host

queue.enqueueReadBuffer(bufferResultado, CL_TRUE, 0, sizeof(int)*size, c, NULL, NULL);

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

  cout << c[i] << endl;