Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid conversion from uint8_t* to uint32_t - when migrating from 32 to 64bit architecture?

I had a little function which converted virtual memory address to physical on a 32-bit architecture:

uint32_t VIRTBASE;

uint32_t getPhysForVirt(void* virt) {
  uint32_t offset = (uint8_t*)virt - VIRTBASE;
  return PHYSBASE + offset;
}

It compiled and worked without any single issue in last 10+ years.

I've changed the compiler to build the repo for newer architectures (now with 64bit support for the first time).

Compilation fails stating

invalid conversion from ‘uint8_t*’ {aka ‘unsigned char*’} to ‘uint32_t’ {aka ‘unsigned int’} [-fpermissive]

Now, I understand the message, but I'm not sure about the necessary steps to make this compiling without errors.

I'm only sure in that I don't want to enable -fpermissive.

like image 414
Daniel Avatar asked Jan 24 '26 09:01

Daniel


1 Answers

You use the wrong types. C language has special types for casting pointers to integral values.

uintptr_t VIRTBASE;

uintptr_t getPhysForVirt(const void * restrict virt) {
  ptrdiff_t offset = (uintptr_t)virt - VIRTBASE;
  return PHYSBASE + offset;
}

If the rest of code is written the way as this function was - you have plenty work for Christmas.

like image 79
0___________ Avatar answered Jan 26 '26 23:01

0___________



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!