Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't fix this error when running my code

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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";
}

>Solution :

You could explicitly cast the return value:

int *a = (int*) GC::allocate(sizeof(int));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading