Q:

How to initialize an object at pointer location with cudaMallocManaged? (C++)

Shown bellow is a simplified version of the code that I'm working with, when I don't use cudaMallocManaged and just initialize everything using the new keyword, everything runs just fine. I have narrowed down the problem to the way the elements in the array are initialized in the for loop, but I cannot figure out what is going wrong here. When printing comparing the pointers everything seems to be the way it should yet it produces a segmentation fault.
#include <iostream>

class B
{
    public:
        __device__ __host__ virtual void test() = 0;
};

class A: public B
{
    public:
        __device__ __host__ A(int x) {number = x;};
        __device__ __host__ void test() {printf("test called!\n");}

        int number;
};

int main(int argc, char const *argv[])
{
    // Size of array.
    static const int count = 2;

    // Create array of pointers to A objects in memmory.
    B** list; // = new B*[count];
    cudaMallocManaged(&list, count*sizeof(B*));

    // Create objects for in array.
    for (int i = 0; i < count; i++)
    {
        A* tempPointer;
        cudaMallocManaged(&tempPointer, sizeof(A));
        *tempPointer = A(500);
        list[i] = tempPointer;
    }

    // Gives a segmentation fault.
    for (int i = 0; i < count; i++)
        list[i]->test();

    // Free memmory.
    for (int i = 0; i < count; i++)
        cudaFree(list[count]);
    cudaFree(list);
}

Using this for loop instead will result in working code, but I really need to use cudaMallocManaged so this is not an option:
for (int i = 0; i < count; i++)
{
    A* tempPointer = new A(500);
    list[i] = tempPointer;
}

A:

The problem here is that the way to initialize an object of a class containing virtual methods, and therefore a virtual function pointer table:
class B
{
    public:
        __device__ __host__ virtual void test() = 0;
};

class A: public B
{
    public:
        __device__ __host__ A(int x) {number = x;};
        __device__ __host__ void test() {printf("test called!\n");}

        int number;
};

is not via object-copy:
   *tempPointer = A(500);

That method will not initialize the virtual function pointer table in the object.
instead, for this particular case, my recommendation would be to use placement new:
$ cat t1674.cu
#include <iostream>
#include <stdio.h>

class B
{
    public:
        __device__ __host__ virtual void test() = 0;
};

class A: public B
{
    public:
        __device__ __host__ A(int x) {number = x;};
        __device__ __host__ void test() {printf("test called!\n");}

        int number;
};

int main(int argc, char const *argv[])
{
    // Size of array.
    static const int count = 2;

    // Create array of pointers to A objects in memmory.
    B** list; // = new B*[count];
    cudaMallocManaged(&list, count*sizeof(B*));

    // Create objects for in array.
    for (int i = 0; i < count; i++)
    {
        A* tempPointer;
        cudaMallocManaged(&tempPointer, sizeof(A));
//        *tempPointer = A(500);
        list[i] = new(tempPointer) A(500);
    }

    // Gives a segmentation fault.
    for (int i = 0; i < count; i++)
        list[i]->test();

    // Free memmory.
    for (int i = 0; i < count; i++)
        cudaFree(list[count]);
    cudaFree(list);
}
$ nvcc -o t1674 t1674.cu
$ cuda-memcheck ./t1674
========= CUDA-MEMCHECK
test called!
test called!
========= ERROR SUMMARY: 0 errors
$

Note in the above I have also fixed another error in the code, specifically that you are attepting to free the pointer list[0] more than once, that obviously cannot be correct.  I have changed it to list[count] which I assume was your intent.
Having said all that, I suspect you may run into a problem with this approach, shortly.  CUDA has a limitation around objects with virtual function pointer tables.  In particular, the object must be created in the domain that is going to be used.  If you intend to use it on the host only, initialize the object on the host.  If you intend to use it on the device only, initialize the object on the device.  Objects (with virtual function pointer tables) initialized in one domain cannot be safely used in the other.

