Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting Function Pointer to Integer in C++

Tags:

c++

I have an array of unsigned integers that need to store pointers to data and functions as well as some data. In the device I am working with, the sizeof pointer is the same as sizeof unsigned int. How can I cast pointer to function into unsigned int? I know that this makes the code not portable, but it is micro controller specific. I tried this:

stackPtr[4] = reinterpret_cast<unsigned int>(task_ptr);

but it give me an error "invalid type conversion"

Casting it to void pointer and then to int is messy.

stackPtr[4] = reinterpret_cast<unsigned int>(static_cast<void *> (task_ptr));

Is there a clean way of doing it?

Edit - task_ptr is function pointer void task_ptr(void)

Love Barmar's answer, takes my portability shortcoming away. Also array of void pointer actually makes more sense then Unsigned Ints. Thank you Barmar and isaach1000.

EDIT 2: Got it, my compiler is thinking large memory model so it is using 32 bit pointers not 16 bit that I was expecting (small micros with 17K total memory).

like image 973
user1135541 Avatar asked May 06 '13 15:05

user1135541


People also ask

Can I cast a pointer to an integer?

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

Can you cast a function pointer?

Yes, it can. This is purpose of casting function pointers, just like usual pointers. We can cast a function pointer to another function pointer type but cannot call a function using casted pointer if the function pointer is not compatible with the function to be called.

What is cast in pointer in C?

In the C language, casting is a construct to view a data object temporarily as another data type. When you cast pointers, especially for non-data object pointers, consider the following characteristics and constraints: You can cast a pointer to another pointer of the same IBM® i pointer type.


1 Answers

A C-style cast can fit an octogonal peg into a trapezoidal hole, so I would say that given your extremely specific target hardware and requirements, I would use that cast, possibly wrapped into a template for greater clarity.

Alternately, the double cast to void* and then int does have the advantage of making the code stand out like a sore thumb so your future maintainers know something's going on and can pay special attention.

EDIT for comment: It appears your compiler may have a bug. The following code compiles on g++ 4.5:

#include <iostream>

int f()
{
    return 0;
}

int main()
{
    int value = (int)&f;

    std::cout << value << std::endl;
}

EDIT2: You may also wish to consider using the intptr_t type instead of int. It's an integral type large enough to hold a pointer.

like image 168
Mark B Avatar answered Oct 31 '22 01:10

Mark B