cast to void *' from smaller integer type 'int

The difference between the phonemes /p/ and /b/ in Japanese, Styling contours by colour and by line thickness in QGIS, AC Op-amp integrator with DC Gain Control in LTspice, Identify those arcade games from a 1983 Brazilian music video. Making statements based on opinion; back them up with references or personal experience. ../lib/odp-util.c:5834:5: error: cast to smaller integer type 'unsigned long' from 'void *' [-Werror,-Wvoid-pointer-to-int-cast] Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, @Dinesh: could you please 1) show us those. You are right! @Artelius: Which, presumably, is exactly what Joshua did: A C++ reinterpret cast will not solve the problem. In such condition type conversion (type promotion) takes place to avoid loss of data. Using indicator constraint with two variables. You are just making it bigger by tricking the compiler and the ABI. There's probably little you can do except look or hope for a fixed 2.x version or upgrade to 3.x (I would assume it's 64-bit safe but this is just a guess, do research this issue before you upgrade). The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie We've added a "Necessary cookies only" option to the cookie consent popup. long guarantees a pointer size on Linux on any machine. @BlueMoon Thanks a lot! Infact I know several systems where that does not hold. If this is the data to a thread procedure, then you quite commonly want to pass by value. Get the address of a callback function to call dynamically in C++, error: call of overloaded function ambiguous, error: cast from to unsigned int loses precision [-fpermissive]. Do roots of these polynomials approach the negative of the Euler-Mascheroni constant? The point is (probably) that the value passed to the thread is an integer value, not really a 'void *'. Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, "what happen when typcating normal variable to void* or any pointer variable?" Use #include to define it. Here is some piece of code where that error occur: /cocos2d-x-2.2.2/cocos2dx/platform/ios/EAGLView.mm:408:18: Cast from pointer to smaller type 'int' loses information. to the original pointer: The following type designates an unsigned integer type with the What is the point of Thrower's Bandolier? Use returnPtr[j] = (void *) ((long)ptr + i); ). SCAN_PUT(ATTR, NULL); How to use Slater Type Orbitals as a basis functions in matrix method correctly? Thanks Jonathan, I was thinking about my answer in another thread: AraK is correct, passing integers a pointers are not necessarily interchangeable. what happens when we typecast normal variable to void* or any pointer variable? I have the following function and when I compile it under VS.NET 2005, the following compile warnings show up: Warning1warning C4311: 'type cast' : pointer truncation from 'void *' to 'long'c:\temp\testone\lib\utils.c56Warning2warning C4312: 'type cast' : conversion from 'unsigned long' to 'void *' of greater sizec:\temp\testone\lib\utils.c56, Code Snippet In simple example code like this it's very easy to convince yourself that there's no problem, but in more elaborate real-world scenarios it's very easy to make this mistake inadvertently. vegan) just to try it, does this inconvenience the caterers and staff? rev2023.3.3.43278. Pull requests. Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2, Xcode 5 and iOS 7: Architecture and Valid architectures, xcode build error: Semantic issue cast from pointer to smaller type 'int' loses information, Issues in handling touches on subviews(uiviews) in UIScrollView, Architecture linking error after Xcode 5.1 upgrade, iOS 7.1 gives error after updating to Xcode 5.1, Linker error in Xcode 5.1 with cocos2d-x 3 beta 2. Just re-enforcing the old behavior of Xcode 5.0 and earlier versions, that already cut away parts of the address by casting it to int, won't introduce any new bugs and avoids the need to learn and understand lots of implementation-internal cocos code. For the remaining integral types, the result is the value of the enum if it can be represented by the target type and unspecified otherwise. Why are Suriname, Belize, and Guinea-Bissau classified as "Small Island Developing States"? On many systems, an 8-bit unsigned int can be stored at any address while an unsigned 32-bit int must be aligned on an address that is a multiple of 4. "After the incident", I started to be more careful not to trip over things. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. From the question I presume the OP does. Only the following conversions can be done with static_cast, except when such conversions would cast away constness or volatility. Can I tell police to wait and call a lawyer when served with a search warrant? ^~~~~~~~~~~~~~~~~~~~~ this question. To cast such pointers to 32-bit types and vice versa special functions are used: void * Handle64ToHandle ( const void * POINTER_64 h ) void * POINTER_64 HandleToHandle64 ( const void *h ) long HandleToLong ( const void *h ) unsigned long HandleToUlong ( const void *h ) The OP wanted to convert a pointer value to a int value, instead, most the answers, one way or the other, tried to wrongly convert the content of arg points to to a int value. Why is there a voltage on my HDMI and coaxial cables? AC Op-amp integrator with DC Gain Control in LTspice. Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2. Thanks for contributing an answer to Stack Overflow! Based on the design of this function, which modification is right. */void **MatrixIB (unsigned long x, unsigned long y, int size){ void *ptr; void **returnPtr; register unsigned long i, j; But gcc always give me a warning, that i cannot cast an int to a void*. "because the type "int" supports only -32768 ~ 32768" This is not true for any modern desktop or mobile OS or any OS that is targeted by cocos2d-x. ^~~~~~~~~~~~~~~~~~~~~ Actions. When is casting void pointer needed in C? You are correct, but cocos actually only uses that address as a unique id. and how to print the thread id of 2d array argument? The dynamic_cast<>operator provides a way to check the actual type of a pointer to a polymorphic class. Even if you are compiling your program for a 32-bit computer, you should fix your code to remove these warnings, to ensure your code is easily portable to 64-bit. tialized" "-Wno-format" "-Wno-incompatible-pointer-types" "-Werror" "-dM" "-U_MSC_VER" "-D_TIMESPEC_DEFINED" "-D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_SECURE_NO_WA Such a downcast makes no runtime checks to ensure that the object's runtime type is actually D, and may only be used safely if this precondition is guaranteed by other means, such as when implementing static polymorphism. What I am saying is that it would be safer to use new(5) rather than 5 and deal with it appropriately at the other end. Making statements based on opinion; back them up with references or personal experience. What happens if you typecast as unsigned first? So the compiler is very picky here and the correct solution to make the code compile again and still let it show the exact same behavior like in Xcode 5.0 is to first cast to an integer type with a size that matches the one of a pointer and to then do a second cast to the int that we actually want: ids [i] = (int) (size_t)touch; The difference between a and &a is the type. ../lib/odp-util.c:5603:13: note: expanded from macro 'SCAN_PUT' To learn more, see our tips on writing great answers. Otherwise, if the original pointer value points to an object a, and there is an object b of the . Asking for help, clarification, or responding to other answers. . This is why you got Error 1 C2036, from your post. Fork 63. I personally upvoted this answer because by it's first line of text it helped me to understand the reason of this strange error message and what am I, poor idiot, doing :D. Not valid on Windows 64 - long is still 32-bit but pointers are 64-bit. And when assigning to a void pointer, all type information is lost. As for the stack, I've written a few libraries using pthreds, thus I don't agree that what you describe "is quite often the case". This returns the first 32 bits of the pointer which may be the top or the bottom depending on big versus little endian, as comment #2 said. You can convert the values from one type to another explicitly using the cast operator as follows (type_name) expression He should pass the address of the integer, the thread should get that address, Note: This is only appropriate is you cast the. I don't see how anything bad can happen . 7.1 New Cast Operations The C++ standard defines new cast operations that provide finer control than previous cast operations. The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie We've added a "Necessary cookies only" option to the cookie consent popup. Don't do that. To learn more, see our tips on writing great answers. The problem was there before, you just are being notified of it. @kshegunov said in Number Type Cast: const void * x; int y = int (x); compiles just fine. ERROR: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int', error: cast to 'string' (aka 'char *') from smaller integer type 'int' [-Werror,-Wint-to-pointer-cast], error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' C, warning: initialization of 'unsigned char' from 'uint8_t *' {aka 'unsigned char *'} makes integer from pointer without a cast [-Wint-conversion], Minimising the environmental effects of my dyson brain. To access the object value, use the * dereference operator: int * p; assert (p == null ); p = new int (5); assert (p != null ); assert (*p == 5); (*p)++; assert (*p == 6); If a pointer contains a null value, it is not pointing to a valid object. On all of them int is 32bit, not 16bit. There are ways to prevent this: pass a dynamic allocated argument if your not the passing thread is not static or if your argument is a local variable, otherwise there is no issue. casting from int to void* and back to int. And then assign it to the double variable. A nit: in your version, the cast to void * is unnecessary. Note that it's not guaranteed that casting an, Generally this kind of type casting does not lead to any concern as long as the addresses are encoded using the same length as the "variable type" (. To learn more, see our tips on writing great answers. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. To perform a cast, specify the type that you are casting to in parentheses in front of the value or variable to be converted. Thanks for pointing that out. Please note that the error I am receiving is "cast to smaller integer type 'int' from 'string' (aka 'char *')" referencing line of code: while (isalpha (residents [i].name) == 0). Can Martian regolith be easily melted with microwaves? (Also, check out how it prints "5" twice), passing a unique pointer to each thread wont race, and you can get/save any kind of information in the th struct. p-util.c.obj "-c" ../lib/odp-util.c It solved my problem too. No sense in writing a few dozen lines of extra code just to get a bunch of numbered threads. Mutually exclusive execution using std::atomic? Passing arguments to pthread_create - invalid conversion from void(*)() to void(*)(void*). Asking for help, clarification, or responding to other answers. Did any DOS compatibility layers exist for any UNIX-like systems before DOS started to become outmoded? Why do small African island nations perform better than African continental nations, considering democracy and human development? On a 64-bit Windows computer, 'long' is a 32-bit type, and all pointers are 64-bit types. then converted back to pointer to void, and the result will compare The problem is not with casting, but with the target type loosing half of the pointer. Is it suspicious or odd to stand by the gate of a GA airport watching the planes? The value of float variable is= 37.75. Recovering from a blunder I made while emailing a professor. C99 standard library provides intptr_t and uintptr_t typedefs, which are supposed to be used whenever the need to perform such a cast comes about. Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide. The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie We've added a "Necessary cookies only" option to the cookie consent popup. you can pass the int value as void pointer like (void *)&n where n is integer, and in the function accept void pointer as parameter like void foo (void *n); and finally inside the function convert void pointer to int like, int num = * (int *)n;. Therefore it is perfectly valid for the compiler to throw an error for a line like. I'm using cocos2d-x-2.2.2. You can also convert values from one type to another explicitly using the cast operator (see Chapter 5 ): ( type_name) expression In the following example, the cast operator causes the division of one integer variable by another to be performed as a floating-point operation: int sum = 22, count = 5; double mean = (double)sum / count; Note the difference between the type casting of a variable and type casting of a pointer. This example is noncompliant on an implementation where pointers are 64 bits and unsigned integers are 32 bits because the result of converting the 64-bit ptr cannot be represented in the 32-bit integer type. And in the function you get the value of the pointer by using the dereference operator *: Please read why glib provide macros for this kind of conversions, there's no need to repeat the text here.