时假定有符号数从不溢出所以这是这个海湾合作委员会警告说,困扰我:简化乘法
warning: assuming signed overflow does not occur when simplifying multiplication
它指向看起来像这样的代码:
/* Move the memory block of entries after the removed one - if any. */
if (database->entries + database->entries_size - 1 != database_entry) {
memmove(
database_entry,
database_entry + 1,
sizeof(spm_database_entry_t)
* (
(database->entries + database->entries_size)
- database_entry - 1
)
);
}
正如你可以轻易猜出它移除元素后移动部分容器的内存,以允许其进一步重新分配(缩小)。
database_entry
是spm_database_entry_t*
类型的指针指向被删除的元素database->entries
是指向的spm_database_entry_t
database->entries_size
阵列之前表示数字database->entries
元件一个size_t
除去
如何摆脱警告?我可以防止乘法简化,或者有更好的方法来计算需要移动多少内存?
编辑
你肯定database_entry < database->entries + database->entries_size
?
正面。
什么是您使用的编译器标志?
-Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes
-Wmissing-prototypes -Wdeclaration-after-statement -Wwrite-strings
-Winit-self -Wcast-align -Wstrict-aliasing=2 -Wformat=2
-Wmissing-declarations -Wmissing-include-dirs -Wno-unused-parameter
-Wuninitialized -Wold-style-definition -Wno-missing-braces
-Wno-missing-field-initializers -Wswitch-default -Wswitch-enum
-Wbad-function-cast -Wstrict-overflow=5 -Winline -Wundef -Wnested-externs
-Wunreachable-code -Wfloat-equal -Wredundant-decls
-pedantic -ansi
-fno-omit-frame-pointer -ffloat-store -fno-common -fstrict-aliasing
EDIT2
乘法之前投放到unsigned int
似乎做的伎俩,但铸造size_t
没有。我不明白这一点 - 标准说size_t
始终是无...
EDIT3
如果上下文可以是任何用途:https://github.com/msiedlarek/libspm/blob/master/libspm/database.c#L116
edit4
解决方案基于steveha的回答:
/* Calculate how meny entries need moving after the removal. */
size_t entries_to_move = (
(database->entries + database->entries_size)
- database_entry - 1
);
/* Move the memory block of entries after the removed one - if any. */
memmove(
database_entry,
database_entry + 1,
sizeof(spm_database_entry_t) * entries_to_move
);
你确定'database-> entries + database-> entries_size - 1> database_entry'吗? – kennytm
@KennyTM正面。 –