简体   繁体   English

Ubuntu Linux中的阵列复制实际上不起作用

[英]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. 我正在做的是,对于每一帧,将“当前”数组的内容复制到“上一个”数组,然后使用glfw将按钮状态转换为“当前”数组。
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. 现在,在Windows下一切正常,但在Linux上则不能。 It results in the prev and curr arrays to be the same. 结果是prev和curr数组相同。
Can someone explain me why it occurs? 有人可以解释我为什么会发生吗?
If you need this information I am using g++ under Linux and MinGW under Windows. 如果您需要此信息,我在Linux下使用g ++,在Windows下使用MinGW。

From what I see glfwGetJoystickButtons returns a pointer to some internal array. 从我看到的glfwGetJoystickButtons返回一个指向一些内部数组的指针。 You are assigning it to Input::joyCurrButtons[i] and in next iteration you assume it will have a previous values. 您将其分配给Input::joyCurrButtons[i]并在下一次迭代中假定它具有先前的值。 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. 这样,您的第一个循环将复制到joyPrevButtons新值,而不是以前的值。

glfwGetJoystickButtons returns a const pointer to make sure API user will not try to modify its values. glfwGetJoystickButtons返回一个const指针,以确保API用户不会尝试修改其值。 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. 我的建议是不要将glfwGetJoystickButtons指针存储在Input::joyCurrButtons[i] ,而是将值从glfwGetJoystickButtons复制到Input::joyCurrButtons[i]数组中。

Considering the code snippet you provided, there is no reason why the array copy does not perform the same way in Linux as in Windows. 考虑到您提供的代码片段,没有理由为什么数组副本在Linux中的执行方式与Windows中不同。 Your problem is likely elsewhere. 您的问题可能在其他地方。

I suggest that you check the following points: 我建议您检查以下几点:

  • is Input::joyButtonsCount[i] properly defined on both platform ? 在两个平台上都正确定义了Input::joyButtonsCount[i]吗?
  • double check on both platforms that Input::joyCurrButtons[i] properly changes when you press button #i. 仔细检查两个平台上按下按钮#i时Input :: joyCurrButtons [i]是否正确更改。
  • check on both platform if the copy happens at time you expect; 在两个平台上检查复制是否在您期望的时间进行; it's likely that, on Linux, the copy happens before you expect, so that when you check values, they are the same (because already copied earlier); 在Linux上,复制很可能比您预期的要早,因此当您检查值时,它们是相同的(因为先前已经复制过); then you will have to understand why copy happens before expected time. 那么您将必须了解为什么复制会在预期时间之前发生。

For everyone else in my situation (such as noobs like me :P) I'll post the problem. 对于其他情况下的其他人(例如像我这样的菜鸟:P),我将发布问题。
It seems that I had to copy the pointer to an array and not get the pointer and use that as my input data. 看来我必须将指针复制到数组,而不要获取指针并将其用作输入数据。
Here's how this code is now: 现在的代码如下:

Input::joyPrevButtons[i] = (char*)memcpy(Input::joyPrevButtons[i], Input::joyCurrButtons[i], Input::joyButtonsCount[i]);
Input::joyCurrButtons[i] = (char*)memcpy(Input::joyCurrButtons[i], glfwGetJoystickButtons(i, &Input::joyButtonsCount[i]), Input::joyButtonsCount[i]);

Obviously I had to include the string header 显然我必须包含字符串标题

#include <string.h>

But I still don't know why it doesn't work on Linux as it works on Windows. 但是我仍然不知道为什么它不能在Linux上运行,而在Windows上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM