Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Pointers to Pointers in Java

Tags:

java

I am a Java noob. I have been able to grasp the concept of converting C/C++ pointers into Java references and this has gone fairly smoothly.

I hit a piece of code that has pointers to pointers (ie **ptr). I need to dereference the pointer and change the value of the pointer it is pointing to (ie *ptr = &newthing;)

This seems alot tougher in java. Does anyone have any ideas on how to solve this issue? Quick google search turned up nothing.

Here's a code example in C++. I want to get something similar working in java but the ptr_to_ptr variable is a problem:

struct _coord
{
   int x;
   int y;
   _coord * next_coordinate;
} coordinate_t;   

coordinate_t buffer[100];
coordinate_t * head;
coordinate_t ** ptr_to_ptr;

if (wednesday)
{
    ptr_to_ptr = &head;
}
else
{
    ptr_to_ptr = &head->next_coordinate;
}
*ptr_to_ptr = &buffer[3];   // <<<---- HOW DO YOU MAKE THIS WORK?
like image 295
Batdude Avatar asked Jun 20 '12 19:06

Batdude


People also ask

Can I use pointers in Java?

Java doesn't have pointers; Java has references.

What is the replacement for pointers in Java?

Java uses the (safer) idea of references instead of pointers.

Why pointer is not used in Java?

No pointer support make Java more secure because they point to memory location or used for memory management that loses the security as we use them directly.

What are the pointers used in C and Java?

Pointers are memory addresses and a pointer points to a memory address of a variable. In C/C++, a pointer can be incremented/decremented to point to a new address but in Java, arithmetic operations on references are not allowed.


3 Answers

A typical trick one could use to model pointers to pointers in Java is using arrays: a reference to the array itself serves as the "pointer", while the array element serves as the item pointed to. You can nest these as much as you'd like, but the code does not look nearly as nice as it does in C/C++.

Rather than porting C/C++ idioms into Java, you should build a mutable class that lets you change the item inside it. This class would serve as a "pointer" to give you the functionality that you are looking for, and it would also add readability to your Java code. Generics greatly simplify your task, requiring only one implementation that you could reuse.

like image 133
Sergey Kalinichenko Avatar answered Sep 27 '22 21:09

Sergey Kalinichenko


Well pointer to pointer is not directly mappable in Java. I assume you want to assign a new object to a pointer inside a function.

As far as I know you can only solve this by using a wrapper object that acts as a reference, so you can modify the real reference inside:

class ObjRef {

   public RealObj obj;

}

void modifyRef(ObjRef ref){

   ref.obj = new RealObj()

}

Hope this helps

like image 44
TeaOverflow Avatar answered Sep 27 '22 22:09

TeaOverflow


I'll answer my own question. I could not find any easy way to do a pointer to a pointer (reference to a reference) in java. So I refactored the code to use arrays of references, and array indices instead of pointers. I think this will work (it coded easily) but I have not tested it yet.

like image 25
Batdude Avatar answered Sep 27 '22 21:09

Batdude