Reputation: 574
I'm writing a program and have gotten a memory location that I have stored as a unsigned int and the length of the mapping as an unsigned int and I want to unmap this.
My following approach generates the warnings:
warning: passing argument 1 of ‘munmap’ makes pointer from integer without a cast [enabled by default]
/usr/include/i386-linux-gnu/sys/mman.h:77:12: note: expected ‘void *’ but argument is of type ‘unsigned int’
and here is causing me the warning:
//startAddr and addrRange are stored as an unsigned int,
void unmap(mapping_t *maps, const int *curSize){
int i = 0;
for (; i < *curSize; i++){
munmap(maps[i].startAddr, maps[i].addrRange);
}
}
My program also crashes when I hit the munmap, but I am assuming that has to deal with the warning in some way
definition of struct mapping_t as requested:
typedef struct mapping{
unsigned int startAddr;
unsigned int endAddr;
unsigned int addrRange;
} mapping_t;
Upvotes: 0
Views: 430
Reputation: 1
I'm writing a program and have gotten a memory location that I have stored as a unsigned int
Do not do that. Use void *
, char *
, or even [u]intptr_t
. Do not stuff a pointer into an unsigned int
. That is wrong. Pointers are not int
values and may not be properly represented by an int
, which is why you get a warning. Pointers are allowed to be converted to an int
per the C standard - which is why you get a warning instead of an actual error - but there's no guarantee that the conversion back to a pointer value results in the same address.
and the length of the mapping as an unsigned int and I want to unmap this.
Do not do this either. Use size_t
:
typedef struct mapping{
void *startAddr;
size_t addrRange;
} mapping_t;
You don't need endAddr
as you have the start address and the size. If you need the end address, you need to convert startAddr
to a char *
to compute the end address.
Upvotes: 3
Reputation: 50110
you cannot use unsigned int for pointers. Use void *.
typedef struct mapping{
void * startAddr;
void * endAddr;
unsigned int addrRange;
}
mapping_t;
Upvotes: 2