Pointer arithmetic for mmap

I recently tried to adapt the mmap code and came across the following error. (Actually, I doubt their code a bit, because it looks like they are doing something unnecessary - an attempt to use MAP_FIXED with their own page-aligned memory. The manpage suggests calling mmap with NULL, since the addr argument should do this on Linux. ) Therefore, I think that at least I will test the mmap call with a NULL address. However, I do not quite understand that the gcc error pounced on me when I changed it. Their code works fine as long as I get the lvalue as the left assignment operand .

Essentially they did the following:

uint8_t * ptr = (uint8_t *)mem; if ((uint32_t)ptr % PAGE_SIZE) { ptr += PAGE_SIZE - ((uint32_t)ptr % PAGE_SIZE); } 

Where mem is the void * in some malloc'd memory.

I am trying more or less the same with typecasts:

  if ((uint32_t)mem % PAGE_SIZE) { (uint8_t *)mem += PAGE_SIZE - ((uint32_t)mem % PAGE_SIZE); /* ERROR */ } 

So, I thought I was smart and delete the variable that I don't need. Can someone enlighten me about why my actuation does not work? Greetings.

+6
source share
1 answer

The error you are making:

 (uint8_t *)mem += /* Anything. */ 

You simply cannot assign the result of the cast. The result of the cast does not match the original expression.

Think how weird it is:

 (int) some_char_variable = 9999; 

I had a problem for the same reason .

Use temp and write the result, OR, as R .. says in his comment:

 mem = (void *) ( ( (uint8_t *) mem) + SOME_EXPRESSION ); 
+5
source

Source: https://habr.com/ru/post/919972/


All Articles