Which type to use for pointers?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
skeen
Member
Member
Posts: 59
Joined: Tue Sep 27, 2011 6:45 am
Location: Denmark

Which type to use for pointers?

Post by skeen »

My question is somewhat simple, should i use; 'uintptr_t', 'uint32_t', 'void*' or something else, for passing around pointers in my kernel? - and why?

It seems like people are using different schemes.
// Skeen
// Developing a yet unnamed microkernel in C++14.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Which type to use for pointers?

Post by dozniak »

Pointers are usually declared as pointers, e.g. char*, void*.

Pointer-type-compatible addresses can be represented via uintptr_t.

uint32_t is a non-portable way to represent values similar to uintptr_t, it will only work for 32 bits systems.
Learn to read.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Which type to use for pointers?

Post by bluemoon »

For address manipulation where a physical address field is needed, i use custom type phyaddr_t which is defined as uint32_t or uint64_t depends on the architecture.
In case a logical address is needed, i use void* (or better yet, typed pointer).

Note that uintptr_t may be 64-bit even on 32-bit target(VC++ do this).
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Which type to use for pointers?

Post by zhiayang »

It really depends.

uint8_t* will address memory in terms of bytes;

Code: Select all

uint8_t* a = <memory_address>
uint8_t byte = *(a + 1);
useful for byte-fields/structures in memory.

Similar thing for uint16_t/32_t.

Don't assume uint32_t for pointers; many make the jump to long-mode; If you don't you'll need to live with the limitations of a 32-bit address space. So if (when) you move, you'd have a hell-of-a-time changing all those pointer references.


The safest and laziest way would be to use uint64_t*. A more appropriate type would be uintptr_t; it's always guaranteed to be *at least* the size of a pointer, on any system.

void* might also work, but honestly what's wrong with uintptr_t? It was conceived for only one purpose. Use it.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Which type to use for pointers?

Post by gravaera »

Yo:

If it's a pointer, you should declare it as a pointer; if it's an integer, declare it as an integer.

--Peace out,
gravaera
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: Which type to use for pointers?

Post by OSwhatever »

I have two types:

vaddr_t for virtual addresses
paddr_t for physical addresses

I could have chosen char* for vaddr_t but char* is limited for strange pointer arithmetics, something that you do a lot in the memory management so vaddr_t is an uint32_t or uint64_t depending on system. When I don't need vaddr_t benefits, I use void*, char* or MyVerySpecialType_t*.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Which type to use for pointers?

Post by dozniak »

requimrar wrote:The safest and laziest way would be to use uint64_t*.
This is a pointer to 64 bit word, with corresponding arithmetics, perhaps not something you would want.
Learn to read.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: Which type to use for pointers?

Post by Jezze »

What a weird collection of answers. How you define a pointer depends on what it is pointing to. If it points to a char use *char, if it is an int use *int or if you dont know use *void.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Which type to use for pointers?

Post by Brendan »

Hi,
Jezze wrote:What a weird collection of answers. How you define a pointer depends on what it is pointing to. If it points to a char use *char, if it is an int use *int or if you dont know use *void.
Agreed.

Also; (assuming paging is involved) physical addresses are not pointers (they're unsigned integers) and you should use something like "uint64_t" for them (even on 32-bit kernels if PAE is involved).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Which type to use for pointers?

Post by Combuster »

Jezze wrote:if you dont know use *void.
If the type varies between executions, use void *. If you don't know, you're doing it wrong :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
arseniuss
Member
Member
Posts: 32
Joined: Tue Aug 10, 2010 12:13 am
Location: Latvia
Contact:

Re: Which type to use for pointers?

Post by arseniuss »

I use compiler to determinate pointer size.

Code: Select all

#if __SIZEOF_POINTER__ == 1
typedef u8_t addr_t;
# define p_max u8_max
# define __BITS__ 8
#elif __SIZEOF_POINTER__ == 2
typedef u16_t addr_t;
# define p_max u16_max
# define __BITS__ 16
#elif __SIZEOF_POINTER__ == 4
typedef u32_t addr_t;
# define p_max u32_max
# define __BITS__ 32
#elif __SIZEOF_POINTER__ == 8
typedef u64_t addr_t;
# define p_max u64_max
# define __BITS__ 64
#else
# error Unknown pointer size!
#endif
But where I don't manipulate with pointer address I use void *, int *, ... etc.
Hobby stuff (suckless libs, compilators, game engines, kernels): github. Work @ zabbix: arseniuss@zabbix
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: Which type to use for pointers?

Post by Griwes »

That's exactly what `uintptr_t` from <stdint.h> (or <cstdint>) is for.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Which type to use for pointers?

Post by Owen »

Also, __SIZEOF_POINTER__ checks while ignoring CHAR_BITS? :-)

It's as if you think architectures don't exist with __SIZEOF_POINTER__ == 1 and CHAR_BITS == 32!
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Which type to use for pointers?

Post by dozniak »

Owen wrote:It's as if you think architectures don't exist with __SIZEOF_POINTER__ == 1 and CHAR_BITS == 32!
But in that case "char" is good?
Learn to read.
Post Reply