cast to pointer from integer of different size

Programming, for all ages and all languages.
Post Reply
viruss33
Posts: 14
Joined: Sun Apr 23, 2017 4:28 am
Freenode IRC: Viruss

cast to pointer from integer of different size

Post by viruss33 »

Currently I'm working on physical memory manager and I have a code that assigns address to a pointer variable, and I got this warning which I don't know how to fix. I have a uint32_t* bitmap = (uint32_t*)address, where address is taken from grub structure and is uint64_t.
Similarly, I have a code
memory_region* memory_regions = (memory_region*) address; where address uint64_t.
User avatar
iansjack
Member
Member
Posts: 4604
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: cast to pointer from integer of different size

Post by iansjack »

Your code is 64-bit?
Octocontrabass
Member
Member
Posts: 5219
Joined: Mon Mar 25, 2013 7:01 pm

Re: cast to pointer from integer of different size

Post by Octocontrabass »

The warning means exactly what it says. The physical addresses in the Multiboot structures are 64 bits. You're writing 32-bit code, so your virtual addresses (pointers) have 32 bits.

If you haven't enabled paging, or you've set up paging for identity mapping only, you need to check to make sure the address is below 4GB before assigning it to a pointer.

If you have enabled paging and you're not using identity mapping, you can't use a physical address as a pointer. You need to assign a virtual address to that physical address in your page tables and assign the virtual address to a pointer. If the physical address is above 4GB, you must use PAE to access it.
viruss33
Posts: 14
Joined: Sun Apr 23, 2017 4:28 am
Freenode IRC: Viruss

Re: cast to pointer from integer of different size

Post by viruss33 »

Thanks, checking for addresses <4gb and casting address to uint32_t fixes the problem. Paging is not yet enabled, that's what im working now. I'm wondering how the compiler knows that the code I'm writing is 32-bit. Is it the target architecture that we pass to it?
User avatar
eekee
Member
Member
Posts: 827
Joined: Mon May 22, 2017 5:56 am
Freenode IRC: eekee
Location: Hyperspace
Contact:

Re: cast to pointer from integer of different size

Post by eekee »

viruss33 wrote:Thanks, checking for addresses <4gb and casting address to uint32_t fixes the problem. Paging is not yet enabled, that's what im working now. I'm wondering how the compiler knows that the code I'm writing is 32-bit. Is it the target architecture that we pass to it?
With gcc, the executable is different for each... what to call it? major architecture version, and for some other details about the target. For instance, the full name of gcc on my old Knoppix disk is "i686-linux-gnu-gcc-8". (It's the only gcc install I have handy.) There's a bunch of symlinks in /usr/bin:

gcc -> gcc-8
gcc-8 -> i686-linux-gnu-gcc-8
i586-linux-gnu-gcc -> gcc-8
i686-linux-gnu-gcc -> gcc-8
i686-linux-gnu-gcc-8
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
Post Reply