Have a question about this project? The main point is: a pointer has a platform dependent size. It generally takes place when in an expression more than one data type is present. This explicit cast clearly tells the compiler "Shut up, I know that this code does not look correct, but I do know what I am doing". reinterpret_cast<void *>(42)). To subscribe to this RSS feed, copy and paste this URL into your RSS reader. Disconnect between goals and daily tasksIs it me, or the industry? Remembering to delete the pointer after use so that we don't leak. ../lib/odp-util.c:5658:9: note: expanded from macro 'SCAN_END_SINGLE' @Shahbaz you could but I will not suggest to a C newbie to use globals. And then assign it to the double variable. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide. This is what the second warning is telling you. I have a function with prototype void* myFcn(void* arg) which is used as the starting point for a pthread. Why do academics stay as adjuncts for years rather than move around? In this case, casting the pointer back to an integer is meaningless, because . So, when you cast a (void*) to (long), you are losing 32 bits of data in the conversion. 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" (. 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. You need to pass an actual pointer. So make sure you understand what you are doing! By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. 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. I am an entry level C programmer. Only the following conversions can be done with static_cast, except when such conversions would cast away constness or volatility. 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. /** Dynamically allocate a 2d (x*y) array of elements of size _size_ bytes. "-D_FILE_OFFSET_BITS=64" "-Wall" "-Winvalid-pch" "-g" "-Wthread-safety" "-Wno-microsoft-enum-forward-reference" "-Wno-unused-function" "-Wno-sometimes-unini On a 64-bit Windows computer, 'long' is a 32-bit type, and all pointers are 64-bit types. - the incident has nothing to do with me; can I use this this way? this way you won't get any warning. linux c-pthreads: problem with local variables. Thanks Jonathan, I was thinking about my answer in another thread: AraK is correct, passing integers a pointers are not necessarily interchangeable. : iPhone Retina 4-inch 64-bit) is selected. Star 675. Why is there a voltage on my HDMI and coaxial cables? Since gcc compiles that you are performing arithmetic between void* and an int (1024). But you seem to suggest by your answer that the user can pass 5 to pthread_create and then perform the above cast to get it back. The preprocesor absolutely will not perform arithmetic. } SCAN_END_SINGLE(ATTR) But gcc always give me a warning, that i cannot cast an int to a void*. /** Dynamically allocate a 2d (x*y) array of elements of size _size_ bytes. (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. To perform a cast, specify the type that you are casting to in parentheses in front of the value or variable to be converted. If the original pointer value represents an address of a byte in memory that does not satisfy the alignment requirement of the target type, then the resulting pointer value is unspecified. How to use Slater Type Orbitals as a basis functions in matrix method correctly? However even though their types are different, the address is going to be the same. 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. ncdu: What's going on with this second size column? Put your define inside a bracket: #define M_TABLE_SIZE (64*1024) Now, you can do: static const void* M_OFFSET = (void*) M_TABLE_SIZE; without a problem. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. 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;. 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)). 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. From what I read about casting in the C11 standard, my feeling is, that it is arguable to emit a warning on an explicit conversion. You just need to suppress the warning, and this will do it: This may offend your sensibilities, but it's very short and has no race conditions (as you'd have if you used &i). Asking for help, clarification, or responding to other answers. If this is the data to a thread procedure, then you quite commonly want to pass by value. Not the answer you're looking for? 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. Thanks for contributing an answer to Stack Overflow! By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. 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. What is the correct method to cast an int to a void*? This is known as implicit type casting or type promotion, compiler automatically converts smaller data type to larger data type. To be honest, I think, clang is too restrictive here. This answer is incorrect for the reasons that have already been mentioned in the comment of, Error "Cast from pointer to smaller type 'int' loses information" in EAGLView.mm when update Xcode to 5.1 (5B130a), http://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models, http://stackoverflow.com/questions/18913906/xcode-5-and-ios-7-architecture-and-valid-architectures, How Intuit democratizes AI development across teams through reusability. @Artelius: Which, presumably, is exactly what Joshua did: A C++ reinterpret cast will not solve the problem. That could create all kinds of trouble. This allows you to reinterpret the void * as an int. 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. cast operators [edit] When an expression is used in the context where a value of a different type is expected, conversionmay occur: intn =1L;// expression 1L has type long, int is expectedn =2.1;// expression 2.1 has type double, int is expectedchar*p =malloc(10);// expression malloc(10) has type void*, char* is expected What can a lawyer do if the client wants him to be acquitted of everything despite serious evidence? 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. Why is there a voltage on my HDMI and coaxial cables? Pd = Pa; Similarly, Pc may be type cast to type int and assigned the value Pa. 1. Asking for help, clarification, or responding to other answers. You are getting warnings due to casting a void* to a type of a different size. Replacing broken pins/legs on a DIP IC package, How to handle a hobby that makes income in US. This is not even remotely "the correct answer". Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. . Casting int to void* loses precision, and what is the solution in required cases, c++: cast from "const variable*" to "uint32" loses precision, Recovering from a blunder I made while emailing a professor, Relation between transaction data and transaction id. How to correctly cast a pointer to int in a 64-bit application? for (i=0, j=0; j Donny Deutsch Daughter Wedding, Craigslist Perry, Ga Homes For Rent, Abc 10 News Anchors San Diego, The Ordinary Alpha Arbutin Turned Brown, Usatf Outdoor Championships 2022 Qualifying Standards, Articles C