I guess the other important fact is that the cast operator has higher precedence that the multiplication operator. The 32 remaining bits stored inside int are insufficient to reconstruct a pointer to the thread function. The only exception is exotic systems with the SILP64 data model, where the size of int is also 64 bits. Issues. So,solution #3 works just fine. reinterpret_cast is a type of casting operator used in C++. If you write ((char*)ptr + 1), it will advance the pointer 1 byte, because a "char" is 1 byte. By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. How do I work around the GCC "error: cast from SourceLocation* to int loses precision" error when compiling cmockery.c? 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: I am using size_t here, because it is always having the same size as a pointer, no matter the platform. Is it plausible for constructed languages to be used to affect thought and control or mold people towards desired outcomes? Thanks for contributing an answer to Stack Overflow! 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. 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; 10) A prvalue of type pointer to void (possibly cv-qualified) can be converted to pointer to any object type. I am compiling this program in linux gcc compiler.. RNINGS" "-D_CRT_SECURE_NO_DEPRECATE" -MD -MQ lib/libopenvswitch.a.p/odp-util.c.obj -MF "lib\libopenvswitch.a.p\odp-util.c.obj.d" -o lib/libopenvswitch.a.p/od In both cases, converting the pointer to an integer type that's too small to represent all pointer values is a bug. this way you won't get any warning. Keep in mind that thrArg should exist till the myFcn() uses it. returnPtr = (void **) malloc (x * sizeof(void *)); ptr = (void *) malloc (x * y * size); [that could be a TODO - not to delay solving the ICE]. even though the compiler doesn't know you only ever pass myFcn to pthread_create in conjunction with an integer. The value of float variable is= 37.75. However the actual code in question contains an explicit c-style cast to int. Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2. Yeah, the tutorial is doing it wrong. AC Op-amp integrator with DC Gain Control in LTspice. I'm unfamiliar with XCode, but the solution should be something like follows: Most of the "solutions" above can lose part of the pointer address when casting to a smaller type. Update: Today, i download the latest version of cocos2d-x (cocos2d-x 2.2.3). Implementing From will result in the Into implementation but not vice-versa. The correct answer is, if one does not mind losing data precision. ../lib/odp-util.c:5489:33: note: expanded from macro 'SCAN_PUT_ATTR' pthread create managing values generated in function (in c), Establishing TCP socket connection between PHP client and C server on Ubuntu. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. In 64-bit programs, the size of the pointer is 64 bits, and cannot be put into the int type, which remains 32-bit in almost all systems. My code is all over the place now but I appreciate you all helping me on my journey to mastery! We have to pass address of the variable to the function because in function definition argument is pointer variable. static_cast on the other hand should deny your stripping away the const qualifier. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. The cast back to int * is not, though. return ((void **) returnPtr);} /* Matrix() */. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. There's no proper way to cast this to int in general case. 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). Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Alternatively, if you choose to castthe ptr variableto (size_t) instead, then you don't need to worry about the pointer typeanymore. Since the 'size' argument for your function is the number of bytes each element in the array needs, I think casting the pointerto (char*) is appropriate. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. Thank you all for your feedback. Thanks for pointing that out. Whats the grammar of "For those whose stories they are"? Please help me compile Chez Scheme. In the first example, the variable c1 of the char type is converted to a temporary variable of the int type, because the second operand in the division operation, the constant 2, is of the higher type int. I get the error: "cast to smaller integer type 'int' from 'string' (aka 'char *')" referencing line of code: while (isalpha(residents[i].name) == 0), How Intuit democratizes AI development across teams through reusability. This is not a conversion at all. Safe downcast may be done with dynamic_cast. Pd = Pa; Similarly, Pc may be type cast to type int and assigned the value Pa. 1. You can convert the values from one type to another explicitly using the cast operator as follows (type_name) expression Converting a void* to an int is non-portable way that may work or may not! 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. A long long would not work for 32bit systems and a long would not work for 64 bit Windows (while 64bit Unix and Unix-like systems like OS X use the LP64 data model, in which a long is 64bit, 64bit Windows uses the LLP64 data model, in which a long has a size of 32bit (http://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models)). for (i=0, j=0; jinteger casts is implementation defined, but the intent was that if the pointer type is large enough and isn't forcefully aligned (, But that's different.