Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ getting error C2440

Tags:

c++

c

visual-c++

Hello I'm using Visual Studio c++ 2010

I'm having a problem with this code ( it's taken from C language code ) :

MEMBLOCK* create_memblock (HANDLE hProc,  MEMORY_BASIC_INFORMATION *meminfo)
{

    MEMBLOCK *mb = malloc(sizeof(MEMBLOCK));

    if (mb)
    {
        mb->hProc = hProc;
        mb->addr = meminfo->BaseAddress;
        mb->size = meminfo->RegionSize;
        mb->buffer = malloc(meminfo->RegionSize);
        mb->next = NULL;

    }
    return mb;
}

I'm having these errors :

error C2440: 'initializing' : cannot convert from 'void *' to 'MEMBLOCK *'          
error C2440: '=' : cannot convert from 'PVOID' to 'unsigned char *'    
error C2440: '=' : cannot convert from 'void *' to 'unsigned char *'

I'm kinda newbie. Can you please provide a converted code for this that actually works with c++.

Thank you

like image 331
user3735032 Avatar asked Feb 12 '23 07:02

user3735032


1 Answers

Since you're programming in C++, you should not use the old C function malloc. Instead I would recommend that you use the C++ new construct:

MEMBLOCK *mb = new MEMBLOCK;
like image 167
Some programmer dude Avatar answered Feb 15 '23 10:02

Some programmer dude