Dereferenced pointer input to a function

I'm relatively new to C++ and I have a problem that I hope you can help me solve. I'm using MS visual studio running on Windows 10 home. I have a function(Coord_to_Index) that takes an int [4] input when called. The output is a pointer to an int variable. If I call the code with a derefenced pointer as input, the input is corrupted in the function. However,
if I just make a copy of the dereferenced pointer to an int [4] variable and then pass this variable it works fine. This is even more confusing to me given that the inverse function (Index_to_Coords) is treated in the same way but works fine. My hypothesis is that this is somehow related to the pointer being to 4 integers (as opposed to 1 as in the inverse function.)

I am really confused about why this substitution works whereas just using the dereferenced pointer does not. I'm sure I am overlooking something silly.

Thanks for your help!

The code looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main(int argc, char *argv[])
{
	long long int *IndexPtr;// index into a data structure
	int *CoordPtr[4]	// spacetime coordinates corresponding to this index
	int s[4];		// used in the second example of code, unused here
	int mu;

// Allocate memory for pointers

	IndexPtr = new long long int;
	*IndexPtr = {random integer from 0 to some max number};		// works fine

	for (mu = 0; mu < 4; mu++) 	
	{
		CoordPtr[mu] = new int;
	}

// map from an integer to 4D spacetime coordinates. input is *IndexPtr, output is CoordPtr
	Index_to_Coord(*indexPtr, CoordPtr); 	// works fine, *CoordPtr contains the 4 
                                                                        // expected values

	{do something, works fine}

// map from 4D spacetime coordinates to index
	Coord_to_Index(IndexPtr, *CoordPtr);	// IndexPtr is well defined here but only 
                                                                // *CoordPtr[1] is correct
						               // in function Coord_to_Index; the others 
                                                               //contain junk, so IndexPtr
						              // is meaningless but consistent

	{do something more, then delete pointers}

	return();
}	


	void Index_to_Coord(long long int Index2, int *Coord[4])		
	// returns spacetime Coordinates [t, x, y, z] given an index 
{
	*Coord[0...3] = {some integer function of Index2};
	return;
}

// functions
	void Coord_to_Index(long long int *Index1, int Coord2[4]) 
// returns index given 4D spacetime Coordinates

	{
// Coord2 corrupted in function
		*Index1 = {some long long integer function of Coord2}  
		return;
	}


However, if I make a copy of the dereferenced pointer *CoordPtr into a non-pointer variable,
the call to Coord_to_Index works:

1
2
3
4
5
6
	// map from 4D spacetime coordinates to index
	for (icount = 0; icount < 4; icount++)
	{			
		s[icount] = *CoordPtr[icount];
	}
	Coord_to_Index(IndexPtr, s);  // works just fine now 

Last edited on
> The code looks something like this:
Post something which actually looks like the problem.

Facsimiles of the problem rarely demonstrate the actual problem. If they did, you would already have the answer.

Also, please use the [code][/code] tags (the <> format button) when posting code.
1
2
3
4
5
6
7
8
9
// Allocate memory for pointers

IndexPtr = new long long int;
*IndexPtr = {random integer from 0 to some max number};	// works fine

for (mu = 0; mu < 4; mu++) 
{
   CoordPtr[mu] = new int;
}
yuck


1
2
3
4
5
6
void Index_to_Coord(long long int Index2, int *Coord[4])	
// returns spacetime Coordinates [t, x, y, z] given an index 
{
   *Coord[0...3] = {some integer function of Index2};
   return;
}
¿is that your prototype?
also, it would have been nice to know the *Coord[0...3] = {some integer function of Index2}; part.
(not really the conversion, assign 42 if you want, but how you are accessing the array)


void Coord_to_Index(long long int *Index1, int Coord2[4])
read the prototype, it ask for an array of integers ¿what are you trying to pass?
Last edited on
Thanks for the tip about code tags salem c. You and ne555 are right, I should have at least posted the two functions (the other {do something}'s are very long but behave properly in terms of generating function inputs. I can post them too if you like, but meanwhile here are the full functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
extern const int Link_Dimension[4];

void Coord_to_Index(long long int *Index1, int Coord[4])					// returns index given [t, x, y, z] Coordinates
{
	*Index1 = (long long int) (
		Coord[3] +
		Link_Dimension[3] * Coord[2] +
		Link_Dimension[2] * Link_Dimension[3] * Coord[1] +
		Link_Dimension[1] * Link_Dimension[2] * Link_Dimension[3] * Coord[0]);
	return;
}

void Index_to_Coord(long long int Index2, int *Coord[4])					// returns Coordinates [t, x, y, z] given an index 
{
	long long int rem{ 0 };

	*Coord[0] = (int) (Index2 / (long long int) (Link_Dimension[1] * Link_Dimension[2] * Link_Dimension[3]));
	rem = (Index2 % (long long int) (Link_Dimension[1] * Link_Dimension[2] * Link_Dimension[3]));
	*Coord[1] = (int)(rem / (long long int) (Link_Dimension[2] * Link_Dimension[3]));
	rem = rem % (long long int) (Link_Dimension[2] * Link_Dimension[3]);
	*Coord[2] = (int)(rem / (long long int) Link_Dimension[3]);
	*Coord[3] = (int)(rem % (long long int) Link_Dimension[3]);

	return;
}


Here Link_Dimension is a dreaded global variable for which I have not yet made a better solution.
Last edited on
> void Coord_to_Index(long long int *Index1, int Coord[4])
> void Index_to_Coord(long long int Index2, int *Coord[4])
You need some consistency here with your Coord array.

For starters, you can't call Coord_to_Index with the Coord array as defined in main. Sure, you can pass something syntactically valid, but it's meaningless.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

void Index_to_Coord(long long int Index2, int *Coord[4]) {
    for ( int i = 0 ; i < 4 ; i++ ) {
        *Coord[i] = i;
    }
}

void Coord_to_Index(long long int *Index1, int *Coord[4]) {
    *Index1 = 0;
    for ( int i = 0 ; i < 4 ; i++ ) {
        *Index1 += *Coord[i];
    }
}

int main()
{
    int *CoordPtr[4];
    for (int mu = 0; mu < 4; mu++)
    {
        CoordPtr[mu] = new int;
    }
    Index_to_Coord(0,CoordPtr);
    long long int foo;
    Coord_to_Index(&foo,CoordPtr);
    cout << "foo=" << foo << endl;
    for (int mu = 0; mu < 4; mu++)
    {
        cout << *CoordPtr[mu] << endl;
        delete CoordPtr[mu];
    }
    return 0;
}

It would be better to use int Coord[4] both times.
also, being c++ may use pass by reference

1
2
3
4
5
6
7
8
9
10
11
void Coord_to_Index(long long int &Index1, int Coord[4]);
void Index_to_Coord(long long int Index2, int Coord[4]);


int main(){
	long long int index;
	int coord[4];

	Coord_to_Index(index, coord);
	Index_to_Coord(index, coord);
}
Last edited on
Thank you both for your time and input. The solution proposed by ne555 worked!

I tried initializing my pointers like salem c suggested but I had the same issue. I probably didn't understand the solution correctly.

I'm left with a good solution but I still don't understand why my original approach wouldn't work once I properly initialized my pointers.

But thanks again for everything!
Topic archived. No new replies allowed.