0

In a code I didn't write and I have to use, using OpenCV (but it could be anything) I come across something like :

void foo (long addrImage){
[...]
   Mat& image  = *(Mat*)addrImage;
[...]
}

I want to do the reverse operation : create an adress based on a Mat&, but I'm not sure I understand completely the trick used.

What would the line be ?

Teleporting Goat
  • 417
  • 1
  • 6
  • 20
  • 3
    Trick? Any reason `Mat* p = &image` doesn't work? – DeiDei Oct 12 '16 at 09:08
  • 5
    long addrImage = (long)&image – UKMonkey Oct 12 '16 at 09:13
  • That's bad code anyways. Casting a `long` to a pointer is undefined behavior. – πάντα ῥεῖ Oct 12 '16 at 09:13
  • @πάνταῥεῖ I thought it was implementation-defined? – TartanLlama Oct 12 '16 at 09:14
  • @TartanLlama Hmmm, well implementation specific. Bad code though. – πάντα ῥεῖ Oct 12 '16 at 09:15
  • @πάνταῥεῖ Found the quote: [expr.reinterpret.cast]/6: A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined. – TartanLlama Oct 12 '16 at 09:17
  • If this is on some embedded platform with memory-mapped i/o, I don't think it's bad code. Otherwise, sure. – TartanLlama Oct 12 '16 at 09:19
  • @ πάντα-ῥεῖ Well it's JNI for Android (so it's actually a jlong) and I can't change the whole project. I didn't chose to store adresses as longs... – Teleporting Goat Oct 12 '16 at 09:19
  • @TeleportingGoat [UKMonkey gave you the correct answer](http://stackoverflow.com/questions/39994687/what-is-the-reverse-operation-of-mytype-var-mytype-addrvar-c#comment67267384_39994687) – πάντα ῥεῖ Oct 12 '16 at 09:22
  • For a little more reading on why long isn't really the type you want to use - http://stackoverflow.com/questions/153065/converting-a-pointer-into-an-integer – UKMonkey Oct 12 '16 at 09:24
  • Am I right to understand that `*(Mat*)addrImage` is a pointer to `addrImage` converted to a `Mat*` ? – Teleporting Goat Oct 12 '16 at 09:37
  • 1
    addrImage is `long` (number). It is then cast into `Mat*` pointer (which means the same number is used as address), and then it's dereferenced by `*` into the `Mat` instance itself. Of that one the reference `Mat &` is created in temporary local variable (for simple access of that `Mat` instance). @πάνταῥεῖ: Android + JNI libraries are full of this long<->ptr (and all other evil). I think the creators of JNI made it intentionally so, that everything good about C++ is lost on the interface level, to make it total hell to use (at least for me it's hell), so people will rather stick in pure Java. – Ped7g Oct 12 '16 at 10:06
  • @Ped7g I'm scared about running my phone and tablet now :P – πάντα ῥεῖ Oct 12 '16 at 10:07
  • @πάνταῥεῖ It's just phone, what can go wrong... I just wonder why people voluntarily want it to connect to a car, "smart" home, or anything else which can mechanically kill you in case something goes wrong. It's like they have absolutely no idea how it works and what's inside. And then there's the "IoT". My favourite horror movie. (sorry for chat in SO comments, ending it here) – Ped7g Oct 12 '16 at 10:20
  • @UKMonkey I'd accept your solution if it was an answer. It seems to work. @ Ped7g Thank you ! – Teleporting Goat Oct 12 '16 at 10:25

1 Answers1

2

long addrImage = (long)&image

UKMonkey
  • 6,941
  • 3
  • 21
  • 30