Archived

This topic is now archived and is closed to further replies.

Sneftel

horrible, sneaky ways around cast

Recommended Posts

suppose I have this function: void* intToVoidPtr(int i) { /* ... */ return /*...*/; } This function simply casts the given integer to the void pointer address of the same value. Now here''s the caveat: no explicit casts of any kind. You can do whatever else you want, as long as it adheres to the C++ standard and works on "most" architectures. Oh, and before anyone accuses me, this isn''t homework. I''m way beyond learning C++. I just thought of this during my EE discussion, and thought up a couple of solutions, and wanted to see if there are any others I''m not thinking of.
How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
That''s a nice fun low level way to get people who don''t know about the stack beating their head into the wall, but what''s the problem with explicit casting?

Share this post


Link to post
Share on other sites
quote:
Original post by Cherez
That''s a nice fun low level way to get people who don''t know about the stack beating their head into the wall, but what''s the problem with explicit casting?


Eh, nothing, really. Just a fun exercise that tests your knowledge of the language standard.

Share this post


Link to post
Share on other sites
Here''s mine, although I didn''t try it out, just flying by the seat of my pants:


  
void* intToVoidPtr(int i) {
union wrapper {
int _i;
void* _ptr;
};
wrapper wrap;
wrap._i = i;
return wrap._ptr;
}


Regards,
Jeff

Share this post


Link to post
Share on other sites
*claps*

Any others? I just thought one up, but don''t know enough about the underlying operations yet....


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by rypyr
Of course my solution will only work on platforms where sizeof(int) == sizeof(void*)...

True, but if we''re talking about a direct conversion, then this should be taken as a given. Besides, even if int and void* are of different sizes, your solution will work if the machine is little-endian....


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

void* intToVoidPtr(int i)
{
void *p;

memcpy(&p, &i, sizeof(void*));
return p;
}


Assuming a pointer-to-void is the same size as an int, but I get chastized for the pointer-to-int to pointer-to-void conversion, don''t I? How about...


void* intToVoidPtr(int i)
{
void *p;
stringstream ss(stringstream::in|stringstream::out);

ss << i;
ss >> p;

return p;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
but I get chastized for the pointer-to-int to pointer-to-void conversion, don''t I?

You shouldn''t be. C++ silently coerces any kind of pointer to a pointer-to-void, when necessary (but not the other way around, going from a void* to any other pointer type requires an explicit cast).

Share this post


Link to post
Share on other sites
Here''s an overly complicated variant.
(assuming it''s allowed to involve more than one function in the conversion)
#include <cstdarg>

void* anyToVoidPtr(int dummy, ...)
{
va_list args;
va_start(args, dummy);
void* p = va_arg(args, void*);
va_end(args);
return p;
}

void* intToVoidPtr(int i)
{
return anyToVoidPtr(0, i);
}

Share this post


Link to post
Share on other sites
Damn, AP...that memcpy solution I thought of on the way home from work...you beat me to it. I consider it a valid solution because it is only an implicit cast from an int* to a void*, not an explicit one.

Here's a fourth:


    
#pragma warning( disable : 4035 )

void* intToVoidPtr(int i) {
_asm {
mov eax,dword ptr [ebp+8]
}
}


I cheated...I disassembled this:

void* intToVoidPtr(int i) { return (void*)(i); }

Regards,
Jeff

Edit: I added the stupid pragma

[edited by - rypyr on May 2, 2003 7:53:57 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just came up with this one.


void* intToVoidPtr(int i)
{
void *p;
p = *(&p+sizeof(int));
return p;
}


That is, assuming that i is allocated just above p on a down-growing stack and the size of an int is less than or equal to the size of a pointer-to-void. For some reason I had to put in 3 as the sizeof an int to make it work (in FreeBSD 5-current running in VirtualPC 6.0). Tweak as needed?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:

For some reason I had to put in 3 as the sizeof an int to make it work (in FreeBSD 5-current running in VirtualPC 6.0). Tweak as needed?



Nevermind, I''m stupid. Too much SML makes your brain go soft on pointer arithmetic.

Share this post


Link to post
Share on other sites
placement new--is there anything it can''t do?

void* intToVoidPtr(int i)
{
return *new(&i) void*;
}


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Dactylos
Here''s an overly complicated variant.



Drat... I was hoping I was the only one to come up with that. Good job


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
placement new--is there anything it can''t do?

void* intToVoidPtr(int i)
{
return *new(&i) void*;
}


How appropriate. You fight like a cow.


This was another example I thought of on the way home, but I wasn''t so familiar with the syntax...nice one.

Share this post


Link to post
Share on other sites

struct blah {
void *p;
int a[1];
};

void *intToVoidPtr(int i)
{
struct blah s;

s.a[-1] = i;

return( s.p );
}

Yeah I know, struct padding might break it...

Share this post


Link to post
Share on other sites

  
#include <stdio.h>

void* intToVoidPtr(int i)
{
_asm{mov eax, i};
}

int main()
{
printf("pointer:%x\n",intToVoidPtr(130));
return 0;
}


Try this ;-)

____________________________________________________________
Try RealityRift at www.planetrift.com
Feel free to comment, object, laugh at or agree to this. I won''t engage in flaming because of what I have said.
I could be wrong or right but the ideas are mine.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
void* intToVoidPtr(int* i)
{
union U
{
int* _i;
void* _p;
};

U u;
u._i = i;

return u._p;
}


int main(int argc, char* argv[])
{
int i = 0;
int j = 0;

printf("&i=%i\n&j=%i\n", intToVoidPtr(&i), intToVoidPtr(&j));

return 0;
}

this will output:

&i=1245056
&j=1245052

these locations will ofcource change every time you start the program.

Share this post


Link to post
Share on other sites