4 Replies - 330 Views - Last Post: 31 December 2012 - 06:59 PM Rate Topic: -----

#1 monkey_05_06  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 26-August 06

void* and temporaries

Posted 29 December 2012 - 07:41 PM

I was reading online and someone suggested doing something along the following lines (this is a simplified example of the mechanic, not a real-world example):

#include <iostream>

int main()
{
  int a = 5;
  int b = 10;
  void *p = reinterpret_cast<void*>(a * B)>;
  int result = *reinterpret_cast<int*>(&p);
  std::cout << std::endl << result << std::endl;
  return 0;
}


I want to know whether this is considered a safe operation, and why it works.

What is the pointer p pointing at, and why do I have to take the address of the pointer before I can dereference it?

As a follow-up I'm interested by the implications of a similar method in terms of function pointers, but first I need to understand what's happening here, whether it's safe, portable, etc.

Edit: I'm trying to get rid of a superfluous "/>", but it doesn't seem to be working... ://>/>

This post has been edited by GunnerInc: 31 December 2012 - 07:25 PM
Reason for edit:: Fixed superfluous "/>"


Is This A Good Question/Topic? 0
  • +

Replies To: void* and temporaries

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: void* and temporaries

Posted 29 December 2012 - 08:41 PM

the standard guarantees that any integral with a size less than or equal to the size of a pointer may be converted to a pointer and then back safely. so yes the above is safe.

A probable reason for this guarantee and the reason it works is becuase of how pointers are implemented in assembly on virtually(if not all) architectures. Pointers are effectively just integers that give addresses to places in memory.

I believe
*reinterpret_cast<int*>(&p); could be re-written as reinterpret_cast<int>(p); but I'm not positive. yup, I tested it

This post has been edited by ishkabible: 29 December 2012 - 08:46 PM

Was This Post Helpful? 1
  • +
  • -

#3 monkey_05_06  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 26-August 06

Re: void* and temporaries

Posted 30 December 2012 - 02:46 PM

Okay, yeah. I think that makes more sense now. :) Regarding function pointers, is there any kind of similar guarantee? I read that the standard doesn't strictly guarantee that void* is large enough to store any function pointer, but that any function pointer type is large enough to hold any function pointer (even of unrelated types).

I'm interested in doing something such as the following:

typedef void* (*pfun)(...);


With a reinterpret_cast this should then be sufficient to serve as a generic function pointer, yes? In my personal tests it seems to me that this is a legal and functional way of making a generic function pointer, but I'm just seeking to clarify.

I do of course also understand that due to the restrictions on what can be passed through the ellipses that the generic function pointer would be restricted to POD-type parameters. I'm interested to see if I can devise something similar (possibly relying on some C++11 features) without that restriction, but this alone could prove quite useful for my needs.
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: void* and temporaries

Posted 30 December 2012 - 09:35 PM

in the cdecl calling convention I suppose that you could make that work for anything becuase anything larger than void* would be returned as a pointer anyhow. this is in no way guaranteed by the standard however. it's also horribly unsafe.

why are you trying to make a one size fits all function pointer? there are much better, far more generic, and type safe options out there. have you seen boost function or the C++11 std::function? any callable object can be assigned to it so it works like a generic function pointer but with type safety. there are many other options too.

if you really want something like this there is an oddly fit example I saw in another post that does what your are trying to achieve with your function pointer. it can be seen here in the OP. I engorge the use of std::function there as well.

This post has been edited by ishkabible: 30 December 2012 - 09:50 PM

Was This Post Helpful? 2
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: void* and temporaries

Posted 31 December 2012 - 06:59 PM

Quote

I read that the standard doesn't strictly guarantee that void* is large enough to store any function pointer,


All pointer types are a fixed size, usually 4 bytes on 32 bit OS. The problem would never be the size of the poitner, since the pointer is simply used to stored a memory address. I'm not a 100% sure on how most compilers implement variadic functions, but I am willing to be that the compilers are smart enough to allocate enough memory for each stack frame for a variadic function call.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1