I have exactly the same problem that was asked 8 and 9 years ago (!) in error when copying dynamically allocated data in device to host? and CUDA - Copy device data to host?
There was mention this will be fixed soon. Is this still not done or am I doing something else wrong here? Running Nvidia Cuda 11.1
so my code is
#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#define N 400
__device__ int* d_array;
__global__ void allocDeviceMemory() {
d_array = new int[N];
for (int i = 0; i < N; i++) d_array[i] = 123;
}
int main() {
allocDeviceMemory <<<1, 1>>> ();
cudaDeviceSynchronize();
int* d_a = NULL;
cudaMemcpyFromSymbol((void**) &d_a, d_array, sizeof(d_a), 0, cudaMemcpyDeviceToHost);
printf("gpu adress: %p\n", d_a);
int* h_array = (int*) malloc(N * sizeof(int));
cudaError_t errr = cudaMemcpy(h_array, d_a, N * sizeof(int), cudaMemcpyDeviceToHost);
printf("h_array: %d, %d\n", h_array[0], errr);
printf("%s\n", cudaGetErrorString(errr)); //invalid argument
return errr;
}