Array objects acting like Quantum particles (explained inside)

So I have these variables. They do certain functions (obviously).

Anyway it seems that if I try to write them to a file (to troubleshoot). They get completely broken.

I'll give you an example.

arraypointer = Pairme(current);
for (zz = 0; zz < 7; zz++)
{
ordered[zz] = arraypointer[zz];
}
myfile<<arraypointer[0]<<"-"<<
arraypointer[1]<<"-"<<
arraypointer[2]<<"-"<<
arraypointer[3]<<"-"<<
arraypointer[4]<<"-"<<
arraypointer[5]<<"-"<<
arraypointer[6]<<endl;

If I get rid of the myfile everything gets done properly. But if I do the myfile then the contents get filled with garbage. I don't understand why this happens. It happens to just about every array in my program. If I try to view it I break it. But if I leave it alone it works fine. THis is stupid why is it like this?
Post more code.
In particular, please post the contents of Pairme(), and the declarations of ordered and arraypointer. My guess is that arraypointer is pointing at unallocated memory.
This is pairme (yeah I use colorful variable names, i don't even notice it, first thing popped into my head)



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
int * Pairme(int cards[])
{
            int i = 0; bool trips = false; bool quads = false;
            int order[7] = {-1,-1,-1,-1,-1,-1,-1};
            int poopy; int poopy1; int poopy2; int poopy3;
            int subpoopy; int subpoopy1; int subpoopy2; int subpoopy3;
            int temp; int temp1; int temp2; int temp3; 
            int j;
            for (i = 0; i < 7; i++)
            {
                order[i] = cards[i];
            }
			int *returned;
			returned = order;
            for (i = 2; i < 15; i++)
            {

                for (j = 0; j < 6; j++)
                {
                    poopy = returned[j];
                    poopy1 = returned[j + 1];
                    subpoopy2 = 55;
                    subpoopy3 = 55;
                    temp = returned[j];
                    temp1 = returned[j + 1];
                    temp2 = 55;
                    temp3 = 55;
                    if (j < 5)
                    {
                        temp2 = returned[j + 2];
                        poopy2 = returned[j + 2];
                        subpoopy2 = poopy2 % 100;
                    }
                    if (j < 4)
                    {
                        temp3 = returned[j + 3];
                        poopy3 = returned[j + 3];
                        subpoopy3 = poopy3 % 100;
                    }

                    subpoopy = poopy % 100;

                    subpoopy1 = poopy1 % 100;
                    if (subpoopy == i)
                    {
                        if (subpoopy == subpoopy1)
                        {
                            if (subpoopy == subpoopy2)
                            {
                                if (subpoopy == subpoopy3)
                                {
									returned = remove(returned, j);
									returned = insert(returned, temp, 0);
									returned = remove(returned, j+1);
									returned = insert(returned, temp1, 1);
									returned = remove(returned, j+2);
									returned = insert(returned, temp2, 2);
									returned = remove(returned, j+3);
									returned = insert(returned, temp3, 3);
                                    quads = true;
                                    j++;
                                }
                                else
                                {
                                    if (quads == false)
                                    {
										returned = remove(returned, j);
										returned = insert(returned, temp, 0);
										returned = remove(returned, j+1);
										returned = insert(returned, temp1, 1);
										returned = remove(returned, j+2);
										returned = insert(returned, temp2, 2);
                                        trips = true;
                                    }
                                }
                                j++;
                            }
                            else
                            {
                                if (trips == false && quads == false)
                                {
									returned = remove(returned, j);
									returned = insert(returned, temp, 0);
									returned = remove(returned, j+1);
									returned = insert(returned, temp1, 1);

								}
                                if (trips == true && quads == false)
                                {
									returned = remove(returned, j);
									returned = insert(returned, temp, 3);
									returned = remove(returned, j+1);
									returned = insert(returned, temp1, 4);
                                }


                            }
                            j++;

                        }

                    }

                }
            }
            return returned;
}


1
2
int* arraypointer;
int ordered[7];

is how they are declared

So what happens is if I don't do "myfile" which is basically my way of troubleshooting it works perfectly fine I get the values I'm looking for. If I do however start peeking into what's happening during the computations. It starts giving me numbers like 6503030 which don't make any sense at all. It does so consistently. And the one reliable fix is to stop looking at it :(
Basically it does this

arraypointer = function(functionthatreturnsapointertoanarray);

Then what I tried to do is this

for (a = 0; a < 7; a++) {
array1[a] = arraypointer[a];
array2[a] = arraypointer[a];
}

myfile<< array1[0]<<"-"<<array1[1]<<endl;

THE WEIRDEST THING

Even if I try to reach into array1 AFTER THE FACT. It breaks Array2.

However if I never reach into anything. It doesn't break anything.

It's like the act of looking into the array BREAK ANOTHER ARRAY. It makes absolutely no sense. The only thing is they were assigned with values from the same pointer. But they are completely different variables.

This doesn't make any sense whatsoever.

Edit: It's important to note. Assigning Array1 DOES NOT break Array2.
It literally only breaks if I actually look at the contents of Array1. If I never look at the contents it's completely fine. It's as if I unset the values of both arrays by simply peeking at the values.
Last edited on
You are returning a pointer to a local variable. That variable gets deleted when the function returns so your return value is pointing at garbage
1
2
3
4
5
6
7
8
9
10
int * Pairme(int cards[])
{
	int i = 0; bool trips = false; bool quads = false;
	int order[7] = {-1,-1,-1,-1,-1,-1,-1};
	...
	int *returned;
	returned = order;
	...
	return returned;
}

Honestly Pairme() isn't the problem. It's this function. Pairme() works perfectly.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
int * Evaluate(int order[])
{
            int i = 0;
            int eval = 0;
			int evarr[6];
            int poopy; int poopy1; int poopy2; int poopy3; int poopy4;
            int subpoopy; int subpoopy1; int subpoopy2; int subpoopy3; int subpoopy4;
            poopy = order[0];
            poopy1 = order[1];
            poopy2 = order[2];        
			poopy3 = order[3];           
            poopy4 = order[4];         
			subpoopy = poopy % 100;
            subpoopy1 = poopy1 % 100;
			subpoopy2 = poopy2 % 100;
			subpoopy3 = poopy3 % 100;
			subpoopy4 = poopy4 % 100;

            if (subpoopy == subpoopy1)
            {
              if (subpoopy == subpoopy2)
              {
                if (subpoopy3 == subpoopy4)
                {
                   eval = 6; // house
                   evarr[0] = eval;
				   evarr[1] = subpoopy;
				   evarr[2] = subpoopy1;
				   evarr[3] = subpoopy2;
				   evarr[4] = subpoopy3;
				   evarr[5] = subpoopy4;
                   i = 5;
				   //fhousez++;
                }
                else
                {
				   if (subpoopy == subpoopy3)
                   {
                     eval = 7; //quads
                     evarr[0] = eval;
					 evarr[1] = subpoopy;
					 evarr[2] = subpoopy1;
					 evarr[3] = subpoopy2;
					 evarr[4] = subpoopy3;
					 evarr[5] = subpoopy4;
                     i = 5;
					 //quadz++;
                   }
                   else
                   {
                     eval = 3; // trips
                     evarr[0] = eval;
					 evarr[1] = subpoopy;
					 evarr[2] = subpoopy1;
					 evarr[3] = subpoopy2;
					 evarr[4] = subpoopy3;
					 evarr[5] = subpoopy4;
                     i = 5;
					 //tripz++;
                    }
                 }
              }
              else
              {
				if (subpoopy2 == subpoopy3) 
				{
					eval = 2; // two pair
					evarr[0] = eval;
					evarr[1] = subpoopy;
					evarr[2] = subpoopy1;
					evarr[3] = subpoopy2;
					evarr[4] = subpoopy3;
					evarr[5] = subpoopy4;
                    i = 5;
					//twopairz++;
				}
				else
				{
                    eval = 1; // pair
					evarr[0] = eval;
					evarr[1] = subpoopy;
					evarr[2] = subpoopy1;
					evarr[3] = subpoopy2;
					evarr[4] = subpoopy3;
					evarr[5] = subpoopy4;
                    i = 5;
					//pairz++;
				}
             }
            }
          else
          {
            eval = 0; // no pair
			evarr[0] = eval;
			evarr[1] = subpoopy;
			evarr[2] = subpoopy1;
			evarr[3] = subpoopy2;
			evarr[4] = subpoopy3;
			evarr[5] = subpoopy4;
			i = 5;
			//nopairz++;
          }
		 int *returned;
		 returned = evarr;
		 return returned;

}
Both functions rely on undefined behavior.
Pairme() works perfectly.

No. It works by accident as Helios and I stated. As for Evaluate, it has the same problem: returning a pointer to a local variable:
1
2
3
4
5
6
7
8
9
int * Evaluate(int order[])
{
	...
	int evarr[6];
	...
	int *returned;
	returned = evarr;
	return returned;
}
Ok then how do you return an array?

I was taught to do it this way. On this forum I believe.

Basically I was told you have to create a pointer to the array and use that because you can't return an array straight up.

Edit:

I was told you create a pointer

int *pointer;

then run the function on that pointer because the function returns a pointer

pointer = Pairme(whatever);

Then i can use pointer[0] pointer[1] to get stuff out of there as if it returned an array

for (i = 0; i<2; i++) {
blah[i] = pointer[i];
}

Then just use blah[i] as the array.

It "worked" I guess maybe not.........
Last edited on
I agree with: dhayden

You are returning a pointer to a local variable. That variable gets deleted when the function returns so your return value is pointing at garbage.


One possible solution is: To store the array as global variable and pass it to the parime function as a pointer reference variable and work with this pointer in pairme function.

http://ncomputers.org
Last edited on
I'd pass both the input and the output arrays as parameters as ncomputersorg suggested:

1
2
3
4
5
6
7
8
9
void pairme(int cards[], int order[]);
void Evaluate(int order[], int evarr[]);

...
int cards[7];
int order[7] = {-1, -1, -1, -1, -1, -1, -1};
int evarr[6];
pairme(cards, order);
Evaluate(order, evarr);
I agree with dhayden

I want to supplement his answer.

I compiled it with -O3 flag and obtained the results, that I want to show you :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

int * pairme(int order_as_global_or_inside_the_outside_scope[],int index){
	return &order_as_global_or_inside_the_outside_scope[index];
}

int * pairme_local(int index){
	int order_as_local[]={1,2,3,4,5};
	return &order_as_local[index];
}

int main(void){
	for(int index_to_see=0;index_to_see<5;index_to_see++){
	  int order_in_this_scope[]={-1,-2,-3,-4,-5};
	  int *pointer1=pairme(order_in_this_scope,index_to_see);
	  cout<<"pointer1 value: "<<*pointer1<<endl;
	  int *pointer2=pairme_local(index_to_see);
	  cout<<"pointer2 value: "<<*pointer2<<endl;
	}
	return 0;
}
;

It is also possible to pass a pointer to the matrix as reference variable.

1
2
3
4
5
6
7
8
9
int * other_way(int *const &pointer,const int index){
  return pointer+index;
}

int main(void){
 int array[]={1,2,3};//int array[] is almost the same as int * array
 int * pointer=other_way(array,2);
 return 0;
}


http://ncomputers.org
Last edited on
Thank you everybody who has been helping.

Let me see if I can make this work fo rme.
Topic archived. No new replies allowed.