You seem to get this on 64 bit compilers that use 64 bit pointers. Converting a 32 bit integer to a 64 bit pointer is questionable and non-portable.
Correct this by using the portable integer type meant to be used for this very purpose:
uintptr_t dst_addr = 0x00FFABCD;
Now it will compile cleanly on all mainstream 64 bit compilers. Tried on gcc, clang icc with -std=c11 -Wextra -Wall -pedantic-errors, no problems.
In addition, when accessing an absolute address, you will almost certainly need to volatile qualify the pointer.
You seem to get this on 64 bit compilers that use 64 bit pointers. Converting a 32 bit integer to a 64 bit pointer is questionable and non-portable.
Correct this by using the portable integer type meant to be used for this very purpose:
uintptr_t dst_addr = 0x00FFABCD;
Now it will compile cleanly on all mainstream 64 bit compilers. Tried on gcc, clang icc with -std=c11 -Wextra -Wall -pedantic-errors, no problems.
In addition, when accessing an absolute address, you will almost certainly need to volatile qualify the pointer.
It's objecting to the conversion of the integer dst_addr to a pointer, not the assignment of the uint8_t
$ gcc -c -Wall -W type.c
type.c: In function 'function':
type.c:11:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
*((uint8_t *) (dst_addr+i)) = *(x+i); // <-- This line shows the warning
^
and clang offers similar:
$ clang -c type.c -W -Wall
type.c:11:12: warning: cast to 'uint8_t *' (aka 'unsigned char *') from smaller integer type 'unsigned int'
[-Wint-to-pointer-cast]
*((uint8_t *) (dst_addr+i)) = *(x+i); // <-- This line shows the warning
^
(clang 3.4.2; gcc 4.8.5)
Converting from an int to a pointer is always suspect, and I'm not sure there's a way to tell the compiler that you really meant it, at least if you turn on all the warnings (which is a good practice).
CCS :: View topic - Invalid type conversion?
misra - Invalid type conversion while using ANSI c - Stack Overflow
"invalid type" error
c++ - Invalid type conversion using static_cast, what proper casting should I use? - Stack Overflow
Why do I get no warnings/errors with:
/* test.c */
#include <stdio.h>
void calculate(int a){
a = a + 1;
printf("a = %i\n",a);
};
int main(void)
{
float c = 7.0;
calculate(c);
return 0;
};Output:
a = 8
I am passing a float to a function that expects int. It seems the compiler is doing some type of behind the scenes type conversion.
I am compiling using gcc 8.3.1 using the command
gcc test.c -Wextra -Wall
Should I get a "invalid type" error or am I going slightly mad?
Static cast will also fail if the compiler doesn't know (or is pretending not to know) about the relationship between the types. If your inheritance isn't declared public between the two, the compiler will consider them as unrelated types and give you the same cryptic warning.
This just bit me, so thought I'd share.
Your reasoning has a very usual flaw; I think we all have made the same mistake sometime. You are thinking of std::vector<> as just an output container because this is how you want to use it now, but it is not.
Just imagine that the following code would compile:
vector<BigObject*>* bigVector = box->ClipObjectInRect(); // OK
ObjList* objVector = static_cast<ObjList*>(bigVector); // Not OK; we'll now see why
objVector->push_back(new SmallObject()); // OUCH
As you can see, allowing that cast would allow you to try to put a SmallObject* in what can only contain BigObject*. This would surely result in a runtime error.
By the way: you can actually cast between arrays of related types. This is a behaviour inherited from C. And it results in runtime errors :)
I think, if you want the size of that structure, you might want to use sizeof(id), which returns a value of type size_t.
What you're attempting to do at the moment is to cast that structure to a size_t type, something that won't really work that well, if at all :-)
In other words:
printf("%zu\n", sizeof(id)); // Here is the solution.
In addition (as you'll notice), the standard-blessed way of printing size_t values is with the z modifier:
z: Specifies that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier applies to a pointer to a signed integer type corresponding to size_t argument.
The screenshot struct xhash code you posted is C++ code, not C code.
However, while it is generally possible to make it work for class types in C++, I still very much doubt that this C++ code was even supposed to be used with class types. I would guess that the posted C++ struct xhash implementation is intended to be used as "fallback" implementation for scalar types only, while class types are required to provide their own implementations for hashing functor (by using template specialization mechanism, for example, or by defining completely different hashing functors for themselves).
Meanwhile, in C none of this makes sense.