I wanted to make a program to help me find the locations of some double valuse in current process memory.

I found some code snippets to study, but Im failing at the first hurdle.

First let me be clear that I am not the author of the below code, but I am free to use it, as per the authors consent.

My goal was to see how it works, the problem is, my compiler (VS2010) tells me this.

Void* __cdecl malloc(size_t _Size)
Error: a value of type "void*" cannot be used to initialize an entity of of type "MEMBLOCK*"

But when I look at the reference to malloc, I see it defined as "void * malloc ( size_t size );", thats void right? malloc reference

I'd sure appreciate it if someone has the time, knowhow, and inclination to point out my misassumptions.

#include <windows.h>
#include <stdio.h>
#include "StdAfx.h"
#define IS_IN_SEARCH(mb,offset) (mb->searchmask[(offset)/8] & (1<<((offset)%8)))
#define REMOVE_FROM_SEARCH(mb,offset) mb->searchmask[(offset)/8] &= ~(1<<((offset)%8));

typedef struct _MEMBLOCK
{
    HANDLE hProc;
    unsigned char *addr;
    int size;
    unsigned char *buffer;

    unsigned char *searchmask;
    int matches;
    int data_size;

    struct _MEMBLOCK *next;
} MEMBLOCK;

typedef enum 
{
    COND_UNCONDITIONAL,
    COND_EQUALS,

    COND_INCREASED,
    COND_DECREASED,
} SEARCH_CONDITION;


MEMBLOCK* create_memblock (HANDLE hProc, MEMORY_BASIC_INFORMATION *meminfo, int data_size)
{
    MEMBLOCK *mb = malloc (sizeof(MEMBLOCK)); // error here with malloc

    if (mb)
    {
        mb->hProc = hProc;
        mb->addr = meminfo->BaseAddress;
        mb->size = meminfo->RegionSize;
        mb->buffer = malloc (meminfo->RegionSize);
        mb->searchmask = malloc (meminfo->RegionSize/8);
        memset (mb->searchmask, 0xff, meminfo->RegionSize/8);
        mb->matches = meminfo->RegionSize;
        mb->data_size = data_size;
        mb->next = NULL;
    }

    return mb;
}

When using malloc(), you need to cast the return to the appropriate type of pointer.

From your linked reference:

buffer = (char*) malloc (i+1);

Notice the "(char*)"? That's a type cast. You need to add them to this code. Whoever the original author of this was must have been relying on a compiler-specific "feature" that doesn't require them and wrote sloppy code.

Thanks for reply

I tried fixing that, then found tons more code with errors.

Turns out, the code is C rather than C++

I never thought there was much difference, or much difference between compilers.

My new question regarding the same subject is where would I start porting C code to C++ to work in Visual Studio?

  1. Why bother porting it? Go into the Properties and change it so it compiles using C.
  2. No clue whether the windows.h and stdafx.h can be ported to C, but the rest of the code can be ported to C++ easily. I see a problem with the struct syntax, but nothing else stands out.
  3. Anyway, pick C or C++ and go with it. Depending on the rest of your needs, it may be easier to create an EMPTY project (gets rid of the stdafx.h and windows.h). Not sure what exactly your needs are.

To get from C to C++, you may need to change the typedef and struct and enum syntax a bit, but not that much. But I would probably just keep it as is if possible, get rid of windows.h and stdafx.h and use the C compiler.

commented: great advice. +1

That sounds like great advice, I never knew I could do such a thing (compile as c)

I'll be looking into that now, thanks for the tip.

EDIT

Really great advice, I appreciate it, it works now and compiles, so I can begin to follow the tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.