Sputanofono Sputanofono - 1 year ago 58
Linux Question

Array copy in ubuntu Linux doesn't actually work

I mean, this thing makes me feel like a naive programmer :')

I'll explain it.

For my buttons controller input I use two arrays, one for the current button pressed and one for the last button pressed. The thing I'm doing is, for every frame, copying the content of the "current" array to the "previous" array and then use glfw to get the buttons state into the "current" array.

Here's the code i'm talking about:

for(int j=0; j<Input::joyButtonsCount[i]; j++)
Input::joyPrevButtons[i][j] = Input::joyCurrButtons[i][j];
Input::joyCurrButtons[i] = (char*)glfwGetJoystickButtons(i, &Input::joyButtonsCount[i]);

Now, under Windows all works correctly, but in Linux it doesn't.
It results in the prev and curr arrays to be the same.

Can someone explain me why it occurs?

If you need this information I am using g++ under Linux and MinGW under Windows.

Answer Source

From what I see glfwGetJoystickButtons returns a pointer to some internal array. You are assigning it to Input::joyCurrButtons[i] and in next iteration you assume it will have a previous values. This might not be true (maybe its implementation defined), as system might have updated this array internally with new values. This way your first loop will copy to joyPrevButtons new values and not the previous ones.

glfwGetJoystickButtons returns a const pointer to make sure API user will not try to modify its values. So you should not cast it to non-const. Also its often a sign that you should not keep this pointer, but use it only to copy values instead.

My suggestion for you is not to store in pointer from glfwGetJoystickButtons in Input::joyCurrButtons[i], but instead copy values from glfwGetJoystickButtons to Input::joyCurrButtons[i] array.