View Single Post
Old Jun 6th, 2006, 2:12 AM   #18
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
This is a horribly complicated subject for me to explain, but I'll try.

If I understand you correctly, you want to pass the array and modify it and not have to return it. In Java, you can do this the same way as C++.

The references/pointers you pass in Java/C++ pass by value. They are numbers.


Just for sake of an example:
char *temp = new char[x];
foo( temp );

void foo( char *ptr )
{
	ptr = new char[y];
}
`temp` still points at "new char[x]". Why?
I'll just show you a simplified example with sample addresses. It would probably help if you kept track on paper.
char *temp;
temp is created at location 0x00

temp = new char[x];
a new array is allocated starting at location 0x10
the value of the array's address (0x10) is stored in 0x00 (temp)

foo( temp );
`ptr` is created at location 0x04

ptr = new char[y];
a new array is allocated starting at location 0x20
the value of the array's address (0x20) is stored in 0x04 (ptr)


The chart would look like this:
location value
0x00 0x10
0x04 0x20
0x10 the value of temp[0]
0x20 the value of ptr[0]


Put another way, it is like saying this:
int temp = 1;
int ptr = temp;
ptr = 100;

Temp won't change, just ptr. char * is just an int that holds a memory address. (on 32-bit machines)
Harakim is offline   Reply With Quote