0

I do some malloc and afterwards I want to check wheather the result from malloc is in my RAM like this:

char (*message)[];
message = malloc(data_index);
if(message != NULL && message >= 0x20000000 && message < 0x20030000){
  //do something
}

It works, but I get a warning: comparrison between pointer and integer. I would like th get rid of this warning, but how? The warning disappears, when I cast the integer to char, but this is a false solution, obviously.

if(message >= (char)0x20000000) {

I also tried to cast it to a double pointer, which I think is the right type, but the warning is still there.

if(message >= (char**)0x20000000) {

How do I get a woking if statement without a warning?

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
Robin
  • 11
  • 6
  • 2
    If you're on a system with virtual memory (basically every PC-like system the last couple of decades) the addresses you receive by `malloc` will not reflect actual physical locations of memory. – Some programmer dude Oct 21 '19 at 08:52
  • Related: https://stackoverflow.com/q/24212792/1025391 – moooeeeep Oct 21 '19 at 08:54
  • 3
    Furthermore, declaring `message` as a pointer to an array of unknown size doesn't make much sense in itself, and seldom makes sense generally (and does it even build?). And for your warning, a pointer to an array is not the same type as a pointer to a pointer. – Some programmer dude Oct 21 '19 at 08:54
  • 5
    What are you _actually_ trying to achieve? This looks like an [XY Problem](http://xyproblem.info/) – Jabberwocky Oct 21 '19 at 09:05
  • 3
    The memory address certainly can't be held in a `char` type and might not fit `int` type either. – Weather Vane Oct 21 '19 at 09:12
  • 1
    _"I do some malloc and afterwards I want to check wheather the result from malloc is in my RAM"_: the return value of `malloc` is in your RAM, that's for sure and that's valid for any implementation. – Jabberwocky Oct 21 '19 at 09:18
  • Duplicate: [“Pointer from integer/integer from pointer without a cast” issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues) – Lundin Oct 21 '19 at 11:36
  • Possible duplicate of ["Pointer from integer/integer from pointer without a cast" issues](https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues) – Toby Speight Oct 21 '19 at 17:40

1 Answers1

1

You probably want something like this:

char *message;
message = malloc(data_index);
if(message != NULL && message >= (char*)0x20000000 && message < (char*)0x20030000){
  // will do something
  // if message contains an address between 0x20000000 and 0x20030000
}

Be aware that this makes only sense on specific hardware (you didn't mention your target system).

On a modern desktop system (Mac/Windows/Linux) this makes no sense as all memory addresses are virtual.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115