gpt4 book ai didi

c - 遇到奇怪的情况-变量值

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:58 24 4
gpt4 key购买 nike

我在一些设备上编程,我遇到了相当奇怪的情况。同一个变量——第一次有正确的值,但是代码中不同位置的 SAME 变量,具有不同的值。是什么原因造成的?我很确定我没有修改两者之间的变量,我也很确定我没有不小心使用一些指针修改了变量。

这可能是什么原因造成的?我真的糊涂了?例如,我使用了整个可用堆栈是否与此有关一些功能的空间 - 因为这个编译器自动0-ifies 我的变量(或类似的东西)?我在单个函数 f 中有一些长代码。

这是我的代码中 pointsEntered 变量用法的完整详细信息(如何使用)。

  /* Let the user input points */

s32 pointsEntered = 0;
int pointsCounter = 0;
while(1)
{

if(pointsCounter == 3)
return; // User entered wrong points 3 times, exit function

bool retStatus = false;
retStatus = inputPoints(&pointsEntered);

if(false == retStatus) // If user didn't enter amount, exit this function
return;

if(pointsEntered>atoi(userAmount)){
PromptBox(false, 0, "Points should not be more\n than current points");
pointsCounter++;
continue;
}
break;

}

// PROBLEM: pointsEntered - is OK here but as it will be shown below, it gets modified down the way
// even though I don't change it directly

char intTest1[50];
sprintf(intTest1, "1pentered %d", pointsEntered); // Here the value is OK! It shows value that I entered, e.g., 220
PromptBox(false, 0, intTest1);



/* Let the user enter 4 digit pin code */
u8 pinEntered[5] = {0};
bool retStatus1 = false;
retStatus1 = inputPin(pinEntered);
pinEntered[5]='\0';
if(false == retStatus1) // If user didn't enter amount, exit this function
return;

char intTest2[50];
sprintf(intTest2, "2pentered %d", pointsEntered); // Here pointsEnetered is OK
PromptBox(false, 0, intTest2);


/* Compute hash of pin code*/
s32 siRet1 = 0;
u8 pinVerify[25]={0};
u8 hashResult[16] = {0};
memcpy(pinVerify,pinEntered,4);
memcpy(&pinVerify[4],"XXXX",21);
siRet1 = sdkMD5(hashResult,pinVerify,25);

char intTest3[50];
sprintf(intTest3, "3pentered %d", pointsEntered); // Here pointsEntered has changed!!!
PromptBox(false, 0, intTest3);

/* convert string hash code to byte array */
unsigned char val[16] = {0};
unsigned char * pos = pinHash;
size_t count = 0;
// WARNING: no sanitization or error-checking whatsoever
for(count = 0; count < sizeof(val)/sizeof(val[0]); count++)
{
sscanf(pos, "%2hhx", &val[count]);
pos += 2 * sizeof(char);
}

char intTest4[50];
sprintf(intTest4, "4pentered %d", pointsEntered);
PromptBox(false, 0, intTest4);

/* Does the pin hash match ? */
if (siRet == SDK_OK && (!memcmp(hashResult,val,16)))
{
MsgBox("PIN OK","",0,SDK_KEY_MASK_ESC | SDK_KEY_MASK_ENTER);
}
else
{
MsgBox("PIN doesn't match-exiting","",0,SDK_KEY_MASK_ESC | SDK_KEY_MASK_ENTER);
return;

}

char intTest[50];
sprintf(intTest, "pentered %d", pointsEntered);
PromptBox(false, 0, intTest);

最佳答案

这两行可能会导致它(因为它是未定义的行为):

u8 pinEntered[5] = {0};
...
pinEntered[5]='\0';

在这里您声明了一个包含五个条目的数组,但随后您分配给了第六个 项目。这很可能会覆盖堆栈上的任何先前变量。

关于c - 遇到奇怪的情况-变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com