0

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;
Tyler Kelly
  • 564
  • 5
  • 23

2 Answers2

3

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.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • if I change to type `void*`, how would I go about reading an `unsigned int` into my `startAddr`? I altered how I was reading in my value like so: `sscanf(line, "%x-%x %s", (unsigned int*)maps[*curSize].startAddr, ...)` but it is causing my program to crash now on the my attempt – Tyler Kelly Jun 05 '17 at 17:43
  • "Reading an `unsigned int` into my `startAddr` (with `scanf`)" would _also_ be a design error, on several levels. Most importantly, the address of a mapping in process A is the address returned by `mmap` to process A, *not* whatever process B thinks it is. Second, if your code is performance-critical enough to bother with shared memory then it's also worth using a binary serialization for your IPC messages. And finally, [never use `scanf` for anything](https://stackoverflow.com/questions/15664664/scanf-regex-c/15664816#15664816). – zwol Jun 05 '17 at 17:53
2

you cannot use unsigned int for pointers. Use void *.

    typedef struct mapping{
      void * startAddr;
      void * endAddr;
      unsigned int addrRange;
    }   
mapping_t;
pm100
  • 48,078
  • 23
  • 82
  • 145