将结果存储在变量中会产生奇怪的结果
Storing result in variable gives strange results
我有一段代码有时会产生正确的结果。唯一的区别是我将结果存储在中间变量中。
整体函数签名为:
FS_FATEntry getFATEntryForCluster(FS_Cluster cluster, FS_Instance * fsi)
这个有效:
switch (fsi->type) {
case FS_FAT12:
if (cluster % 2)
return ((*((uint16_t *)&FATSector[entOffset])) >> 4);
else
return ((*((uint16_t *)&FATSector[entOffset])) & 0x0FFF);
case FS_FAT16:
return (*((uint16_t *)&FATSector[entOffset]));
case FS_FAT32:
return ((*((uint32_t *)&FATSector[entOffset])) & 0x0FFFFFFF);
}
这不是:(请注意 FS_FATEntry 被类型定义为 uint32_t
)
FS_FATEntry entry = 0xFFFFFFFF;
switch (fsi->type) {
case FS_FAT12:
if (cluster % 2)
entry = ((*((uint16_t *)&FATSector[entOffset])) >> 4);
else
entry = ((*((uint16_t *)&FATSector[entOffset])) & 0x0FFF);
case FS_FAT16:
entry = (*((uint16_t *)&FATSector[entOffset]));
case FS_FAT32:
entry = ((*((uint32_t *)&FATSector[entOffset])) & 0x0FFFFFFF);
}
free(FATSector);
printf("Cluster %04X : %04X Entry\n", cluster, entry);
return entry;
当 运行 第二段代码时,这是我得到的输出片段:
Cluster 00F9 : FB0FA0 Entry
Cluster 00FA : D0FC0FB Entry
Cluster 00FB : FD0FC0 Entry
Cluster 00FC : F0FE0FD Entry
Cluster 00FD : FF0FE0 Entry
Cluster 00FE : 11000FF Entry
Cluster 00FF : 1011000 Entry
Cluster 0100 : 3102101 Entry
Cluster 0101 : 1031021 Entry
Cluster 0102 : 5104103 Entry
Cluster 0103 : 1051041 Entry
Cluster 0104 : 7FFF105 Entry
Cluster 0105 : 107FFF1 Entry
Cluster 0106 : FFFF107 Entry
Cluster 0107 : FFFFFF1 Entry
Cluster 0108 : B10AFFF Entry
Cluster 0109 : 10B10AF Entry
Cluster 010A : D10C10B Entry
Cluster 010B : 10D10C1 Entry
Cluster 010C : F10E10D Entry
Cluster 010D : FFF10E1 Entry
Cluster 010E : 1110FFF Entry
Cluster 010F : 111110F Entry
每个案例的原始代码 returns,新代码失效...您是否打算在开关上失效,即不使用 break?
我有一段代码有时会产生正确的结果。唯一的区别是我将结果存储在中间变量中。
整体函数签名为:
FS_FATEntry getFATEntryForCluster(FS_Cluster cluster, FS_Instance * fsi)
这个有效:
switch (fsi->type) {
case FS_FAT12:
if (cluster % 2)
return ((*((uint16_t *)&FATSector[entOffset])) >> 4);
else
return ((*((uint16_t *)&FATSector[entOffset])) & 0x0FFF);
case FS_FAT16:
return (*((uint16_t *)&FATSector[entOffset]));
case FS_FAT32:
return ((*((uint32_t *)&FATSector[entOffset])) & 0x0FFFFFFF);
}
这不是:(请注意 FS_FATEntry 被类型定义为 uint32_t
)
FS_FATEntry entry = 0xFFFFFFFF;
switch (fsi->type) {
case FS_FAT12:
if (cluster % 2)
entry = ((*((uint16_t *)&FATSector[entOffset])) >> 4);
else
entry = ((*((uint16_t *)&FATSector[entOffset])) & 0x0FFF);
case FS_FAT16:
entry = (*((uint16_t *)&FATSector[entOffset]));
case FS_FAT32:
entry = ((*((uint32_t *)&FATSector[entOffset])) & 0x0FFFFFFF);
}
free(FATSector);
printf("Cluster %04X : %04X Entry\n", cluster, entry);
return entry;
当 运行 第二段代码时,这是我得到的输出片段:
Cluster 00F9 : FB0FA0 Entry
Cluster 00FA : D0FC0FB Entry
Cluster 00FB : FD0FC0 Entry
Cluster 00FC : F0FE0FD Entry
Cluster 00FD : FF0FE0 Entry
Cluster 00FE : 11000FF Entry
Cluster 00FF : 1011000 Entry
Cluster 0100 : 3102101 Entry
Cluster 0101 : 1031021 Entry
Cluster 0102 : 5104103 Entry
Cluster 0103 : 1051041 Entry
Cluster 0104 : 7FFF105 Entry
Cluster 0105 : 107FFF1 Entry
Cluster 0106 : FFFF107 Entry
Cluster 0107 : FFFFFF1 Entry
Cluster 0108 : B10AFFF Entry
Cluster 0109 : 10B10AF Entry
Cluster 010A : D10C10B Entry
Cluster 010B : 10D10C1 Entry
Cluster 010C : F10E10D Entry
Cluster 010D : FFF10E1 Entry
Cluster 010E : 1110FFF Entry
Cluster 010F : 111110F Entry
每个案例的原始代码 returns,新代码失效...您是否打算在开关上失效,即不使用 break?