Home > Back-end >  Can't fix this error when running my code
Can't fix this error when running my code

Time:12-25

I get this error when I'm trying to run my code:

"error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive] 9 | int *a = GC::allocate(sizeof(int));"

I am trying to make a function that acts like a garbage collector and right now am first trying to make the dynamical allocation for pointers (since the pointers are going to point to their respective chunk/block of memory, allocated from the GC class).

main.cpp:

int *a = GC::allocate(sizeof(int));
*a = 5;
cout << a << " " << *a << endl;

GC.cc:

void* GC::allocate(size_t size)
{
    Chunk();
    bool flag = 1;
    size_t i, j, l;
    for(i = 0; i < SHEET_SIZE; i  )
    {
        if(sheets2[i] == 0)
        {
            for(j = i; j < i size; j  )
            {
                if(sheets2[j] != 0)
                {
                    flag = 0;
                    break;
                }
            }
            if(flag)
            {
                for(l = i;l < j;l  )
                {
                    sheets2[l] = 1;
                }
                chunk = &sheet[i];
                chunks.push_back(Chunk(chunk, size, 1));
                chunks_count  ;
                return chunk;
            }
        }
    }
    return (char*)"ERROR";
}

CodePudding user response:

You could explicitly cast the return value:

int *a = (int*) GC::allocate(sizeof(int));
  • Related