Question about arrays of objects


So I've made a struct called cube (working on building a functional rubrix cube). I've made a 3d array of type cube. I'm all good to this point. I've populated the array with colors ect. Ive wrote a function to orient the cube. Still good. The problem I have is when I try to start doing moves. I send the cube array to a function then I copy the cube array to a temp. That's when everything starts to fall apart. I'm wondering initial try to just copy the object over top of the existing object if I'm just copying a pointer to the temp array and once the temp array goes out of scope youd think that the data would just disappear but that doesn't seem to happen. It just doesn't seem like anything happens. When I display the cube array it just doesn't change. Even if I go as far as to extract out the specific variable values.
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
  struct Cube{
char color1,color2,color3,up,down,left,right,back,front;
int type;
};

void MoveRightBack(Cube cubeary){

Cube temp[3][3][3];
for (y=0 to 2) {// just example for speed
for (z=0 to 2) {

temp[2][y][z]=cubeary[2][y][z]; //copy
}
}

for(z=0 to 2;z++){
y=2
cubeary[2][y][2]=temp[2][2][z]; //move top right toback.   //right
y--

}// this doesn't seem to change anything 

//using the same for loop an doing...

cubeary[2][2][z].color1=temp[2][y][2].color1;

and so on doesn't seem to do anything either



}

int main{

Cube cubearray[3][3][3];
}


 


I've even put in a break point and closely looked to see if different values were being copied which the are. Just seems like after the fact they have no effect. Just mind boggling to me. I just really don't know what is happening. I'd post more of the code but its huge and like I said I I intentionally left errors in there just so I could get this out quicker. Any help would be great thanks.

Again this is not that actual code I just typed this out quick on my phone the for loops in my code are properly formatted
Last edited on
main does not call the function?
once the temp array goes out of scope youd think that the data would just disappear but that doesn't seem to happen.

Just because the array goes out of scope, doesn't mean the contents of that memory are going to be immediately overwritten. They'll hang around until something wants to use that memory for something else.

So, if you look at that memory right after the last thing it was being used for has gone out of scope, there's a good chance those values will still be in there. You can't rely on that, obviously.
@jonin in the actual .cpp the function is called correctly. As I said I just threw this together quickly. I'm at work and on a phone so I didn't want to reinvent the wheel. Everything else is working right so far and I'm about 700 lines in again.

@Mikey. So is this part of the issue that just copying one object over the other in this situation is just copying the pointer to the object in the temp array?

Wouldn't the copy part of the code copy the pointers to the elements and then just overwrite the pointers of the cube array with the temp array? That's why I'm confused. Or is the temp array storing pointers in pointers so that it would need dereference to actually copy the actual pointer? Should I just force the temp array to behave like this so that I know for sure what is happening.?

Ok I think I figured out what is happening. I copied the colors over but then when I go to orient the cube I'm not copying the colors over to the directions and then swapping directions.

I'm just copying the colors and the I'm going through and using if statements. Like so

1
2
3
4
5
6
7
8
9
10

If (cubeary[2][y][z].up != NULL){  //for rotate back
cubeary[2][x][y].back = cubeary[2][y][z].up;
     
cubeary[2][y][z].up=NULL;

}




But like I said this can be done all day but if the colors dont initially replace the colors in the directions then it's all for nothing.
Last edited on
If you still need help try posting the actual code that is bad.
I think you need to stop and add to your struct. I can't be sure but if you make an assignment operator so you can just swap 2 cube structs with std::swap it would take a lot of this pain away, right? Your operator would swap what needs to swap (eg, the colors) and leave alone what needs to be left alone (eg the position on the cube). The default assignment will copy everything, and some of it seems like it shouldn't (?) move for what you want it to do. But maybe I am not seeing your approach correctly.

Also, the big thing about pointers, if you change a pointer in a function, it must be by reference. You can change the data pointed to and that is by reference already as a side effect of being under a pointer.
let me show you..
void foo(int *ip)
{
ip = new int;
}
int *x = null;
foo(x);
cout << *x; //crash. x is still null and just to be sure it is messed up, it leaked memory too.

void foo(int *&ip)
{
ip = new int;
}
int *x = null;
foo(x);
cout << *x; //fine. x was passed by reference and the new was applied to it.

a pointer that was passed into a function that is on the left side of an assignment needs to be by reference if you want to keep the change. A pointer under a pointer that was passed in by reference follows the other rule of being under a pointer and being reference due to that. Please read this a few times to grasp it; its critical if you insist on using pointers to get this down. If data isnt changing when you think it should, its likely you are doing this kind of thing, where you changed it but its not by reference and the changes are lost (and for pointers, may also be leaked if a new was used, or damaged if a delete was used). For deletes, you can give the memory back to the os but you still point there, the pointer didn't change, so deletes done in a function stick when you return out even if not by reference. Pretty much just like delete in main and then trying to use it again -- except it is harder to spot when it happened.


Last edited on
The issue was initially I was just copying replacing an object over a different object in the array so the colors were being copied. Then when I wasn't getting the desired results I switched to copying the colors individually and then I failed to reassign them to a side. I thought that was the problem. The real issue was that I was giving the z or y a value set to zero or 2 inside of the for loop. I the example it was y I believe. Then I was incremeting that variable at the end of the loop but back at the top it was just being reassigned to 0 or 2.

If I'm sending an array to a function do I still need to have it be passed by reference? I realize I need to do that with individual variables but it seems like arrays behave differently. I.e. sending an array to a function is sending just the pointer to the array and it seems to make the conversion between dereference and reference on it's own. Sending a pointer to a function and modifying the underlying data is going to modify the underlying data at that pointer regardless if its sent by reference or not correct?

I dont see how I have much of a choice to use pointers with arrays bc I dont think c++ gives the option to actually copy the array into functions like I said it just passes the pointer and automatically
Reference or dereference based on what is happening.

But anyways the issue is solved.

The nature of a cube I cant just swap objects. Bc if the cube is turned by 1/4 no 2 pieces can just switch places and if I just move one piece to a different location without some kinda copy of the section that's being moved around inevitably pieces would be lost in the shuffle. Now if I was moving by a 1/2 turn it might be possible to swap places but 1/4 back the top pieces go to the back, back to down down to front etc.

I just reread what you write and yes the entire struct needs copied. Also it looks like you confirmed what I said about pointers not needing passed by reference. And I do understand what your saying about trying to make a new pointer and pass it back to the original variable. That's not what I'm trying to do.

It just seems weird to say it but an array of objects is just an array of pointers pointing at those objects. So the copy portion of the example just copies the pointers and then just swaps them to different places in the array. So there shouldn't be any reason why they wouldn't stay persistent. Especially since those pointers are created in the main(). But they seem to be behaving that way . The issue with this particular function seems to be solved I'm having issues elsewhere but its probably some kinda faulty logic elsewhere in the program. Once I run that down I can get back to creating the move functions for the rest of the cube. Hopefully that will be faster since I'll just be replicating the same methods I've already established.

Last edited on
If I'm sending an array to a function do I still need to have it be passed by reference?

no. arrays are passed as pointers under the hood, which as I said, makes the *data* under the pointer 'by reference', and for arrays, the data is all you care about.

Sending a pointer to a function and modifying the underlying data is going to modify the underlying data at that pointer regardless if its sent by reference or not correct?
Exactly.

just be careful with cube. It is made of arrays but is not itself a pointer and cube objects need to be passed by reference (but not arrays of them, the above idea handles it). So if you pass the whole cubearray, its an array, and its fine. If you pass just one or two parts of the cube eg foo (cube[0][1][2];) that needs to be by ref...!

Yes, how you swap is the key to the problem. Don't let me confuse you if you have a good idea in your head of what to do.

Last edited on
Topic archived. No new replies allowed.