0

I am not sure if that is feasible, but I need to cast a function pointer to long in order to map C level code back to java code.

dbush
  • 205,898
  • 23
  • 218
  • 273
Bionix1441
  • 2,135
  • 1
  • 30
  • 65
  • 1
    Could you post your code? – Bob Brinks Sep 01 '16 at 09:47
  • I have seen long to function pointer not the other way around ? – Bionix1441 Sep 01 '16 at 09:53
  • Undefined behavior: _A pointer is used to call a function whose type is not compatible with the referenced type (6.3.2.3)_ – David Ranieri Sep 01 '16 at 09:53
  • Do you refer to section 6.3 in C99 standard ? – Bionix1441 Sep 01 '16 at 09:54
  • For the code I cannot, but may be I will do a small example, I look for a way to pass function pointer to Java code with JNI – Bionix1441 Sep 01 '16 at 09:56
  • 3
    In the Annex: _J.2 Undefined behavior_ – David Ranieri Sep 01 '16 at 09:57
  • 1
    Doing some minimal testing, casting to `long` does not seem to change the pointer bitwise. So I'd say it is safe to pass back as a `jlong`. – Jorn Vernee Sep 01 '16 at 10:05
  • Please be more specific, cast in which language, and how exactly, show the code? – 2501 Sep 01 '16 at 10:11
  • 1
    Correct me if I'm wrong, but the idea here is that, since Java does not have pointers, you need to cast C pointers to `jlong` (which is just a `long`) to be able to pass it back to Java code. There you can store it, or pass it back to another native function, in which you would cast it back to the right function type to then call it. – Jorn Vernee Sep 01 '16 at 10:16
  • 2
    @AlterMann You aren't quoting the standard correctly. First of all, Annex J is merely an informative summary, it is not normative, so quoting something from there proves nothing. The actual normative text in 6.3.2.3/6 is this: "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." So it is not necessarily undefined behavior. – Lundin Sep 01 '16 at 10:58
  • @JornVernee Yes that is right – Bionix1441 Sep 01 '16 at 11:01
  • @Lundin Yep, converting function pointers to integers, and vice-versa, is implementation-defined. – 2501 Sep 01 '16 at 11:04

1 Answers1

2

A pointer's value is an integer, you just need to know what exactly you are doing, and allocate enough storage to store the pointer's value. (In a 32bit OS, a pointer occupies 32bit storage, in a 64bit OS, pointer occupies 64bit storage).

A C example:

#include <stdio.h>

void fun()
{
    printf("fun\n");
    return;
}

typedef void (*fun_type)();

int main()
{
    long long int a = (long long int)fun;

    fun_type func_point = (fun_type)a;

    func_point();
    return 0;
}
Koshinae
  • 2,240
  • 2
  • 30
  • 40
jacob
  • 671
  • 6
  • 15
  • Casting `long long int` to a pointer and vice versa will issue a warning on certain 32-bit compilers. The correct way is to cast them through [`intptr_t`](http://stackoverflow.com/a/6543455/3448419). – apangin Sep 10 '16 at 11:07