After looking harder at the code, I figured out the answer to my question. I was mistaken in thinking the initial declaration of linked list meant all future types had to be the same
The code I am looking at is for linked list insertion. The linked list is used to store logical addresses. The linked list is defined as unsigned 32 bit, but the addresses being inserted are defined as void*.
A logical address is cast to paddr_t and is then inserted. paddr_t was used to be reverse compatible with 32bit or 64 bit systems, since the void* size changes.
When the paddr_t is passed to the insert function, will the insert function parameter be promoted to paddr_t? Will the linked list member be promoted as well once the insertion is complete?
Abbreviated example of the code
LinkedList <unsigned> foo;
unsigned bar = 1001;
void* Laddr = &bar;
foo.insert((paddr_t)Laddr);
CodePudding user response:
Assuming a common-sense definition of LinkedList
: You don't pass paddr_t
to the insert function. You pass unsigned
to the insert function. If you try to pass paddr_t
, the compiler will add an (unsigned)
cast for you, and may or may not emit a warning.
If paddr_t
and unsigned
are the same type, it's unlikely that you'll get a warning, and extremely likely that it will work fine.
If paddr_t
is bigger than unsigned
, and you have a paddr_t
that doesn't fit in an unsigned
, then the extra bits will be removed to make it fit in unsigned
, and that number will be stored in the list, and whatever code get the number back out of the list and converts it back to paddr_t
will not work properly because it gets the wrong number.