char * var1 = "Hello";
__int32 * var2 = &(const_cast<__int32>(0x02i32));
char * var1 = "Hello";
__int32 * var2 = &(const_cast<__int32>(0x02i32));
Quote:
Original post by likeafox
Does anyone know how to get the address of an inline constant?
Quote:
Original post by Simian Man
I could be wrong, but isn't the point of something being *inline* so that the compiler sticks it directly into the code that uses it. So it does not even have an address at all. Why would you want to do this?
Quote:
Original post by likeafox
If I were to write something like this it would compile and work just fine:
pointer to a string literal
So why can't I do something like this?:
pointer to an integer constant
Quote:The first case is different from the second case. "Hello" is an array, which converts to a pointer automatically. It's as simple as that. BTW, some compilers don't generate an error due to const mismatch in the first case because of the all the code that would no longer compile.
Original post by likeafox
If I were to write something like this it would compile and work just fine:So why can't I do something like this?:char * var1 = "Hello";__int32 * var2 = &(const_cast<__int32>(0x02i32));
__int32 * var2 = &(0x02i32);generates this error in VS 2005: error C2101: '&' on constantQuote:Considering that it is not stored in memory, how could it have an address?
Original post by likeafox
Does anyone know how to get the address of an inline constant?
Quote:
Original post by LessBread
That would make the pointer point to the address 0x00000002. Accessing that address would probably throw an exception.
Quote:
Original post by likeafox
it's going to be on the stack somewhere inside the function code--
Quote:
In this case I'm calling CreateThread which takes a void pointer parameter, for which I want to pass in an int, but I'd look a lot tidier to me if I didn't have to break it into multiple lines to start declaring variables. But I guess I'll have to if there's no way of avoiding it.
int functionWhosInterfaceWeDontLike(int* out_parameter) { // Evil C code here}std::pair<int, int> prettyFunctionExploitingCppFeatures() { int out_buffer; int result = functionWhosInterfaceWeDontLike(&out_buffer); return std::make_pair(result, out_buffer);}// or, when the input is not supposed to be modified, and the indirection// exists for a sillier reason:int functionWhosInterfaceWeDontLike(int* in_handle) { // Evil C code here}int usingAConstReferenceLikeOneOughtToInCpp(const int& in) { int tmp(in); int result = functionWhosInterfaceWeDontLike(&tmp); assert(tmp == in); // if this trips, report a bug to the API vendor return result;}Quote:
Original post by Prgrmr@wrk Quote:
Original post by LessBread
That would make the pointer point to the address 0x00000002. Accessing that address would probably throw an exception.
You're absolutely right, but using the method you posted would also cause hard to debug errors if the class/function the value is passed to uses that value later on, after the original "__int32 var1" fell out of scope.
After looking closer at the CreateThread() the OP mentioned, what he should be doing is exactly what you said, except that he should just pass the address of var1 as the parameter, instead of copying that address to var2. I'm sure there's a good reason CreateThread was designed that way, but I have no idea what it is.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more