当编译代码从结构访问常量以在@for $i from 1 through 10 {
.fade-up-#{$i * 100}-enter-active,
.fade-up-#{$i * 100}-leave-active {
transition: all #{$i * 100}ms ease;
}
.fade-direction-#{$i * 100}-enter,
.fade-direction-#{$i * 100}-leave-to {
opacity: 0;
transform: translateY(#{$i * 100}px);
}
}
中使用时,在使用desktopCapturer.getSources()
时收到-Wsign-conversion警告:
malloc
带有警告的C代码是这样:
gcc -Wconversion sample.c
如果我不使用结构,而是直接提供值,则不会使用sample.c:12:45: warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
unsigned char *numbers = malloc(s.value * 100 * sizeof(unsigned char));
1 warning generated.
发出警告:
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
const int value;
} MyStruct;
int main(int argc, char *argv[])
{
MyStruct s = {5000};
unsigned char *numbers = malloc(s.value * 100 * sizeof(unsigned char));
printf("Address of numbers: %p\n", &numbers);
}
使用gcc -Wconversion sample.c
标志时,将#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned char *numbers = malloc(500 * 100 * sizeof(unsigned char));
printf("Address of numbers: %p\n", &numbers);
}
与结构值一起使用的正确方法是什么?
答案 0 :(得分:3)
您可以将结构声明更改为具有const unsigned int
,或仅将unsigned int
转换为s.value
到malloc()
函数中,因为它需要无符号参数(即使是size_t
准确。
void *malloc(size_t size);
答案 1 :(得分:2)
在此结构
typedef struct
{
const int value;
} MyStruct;
成员value
的声明中的所有单词似乎都是错误的
由于将“ value
”用作对malloc
的调用的乘数,这将暗示它不是通用的“ value
”,而是数组的大小,因此也许最好是unsigned
-甚至是size_t
;并且其名称类似于size
,length
之类。
警告是因为,如果该值确实是一个有符号的-1
,则将其乘以100
会得到-100
,然后乘以sizeof
的值类型size_t
,它将转换为size_t
,结果分配的字节数为(SIZE_MAX - 100) * 1
,这几乎不是您想要的。
最好是明确的,并使用传达含义的声明:
typedef struct
{
size_t size;
} MyStruct;
答案 2 :(得分:0)
代码中的一个问题是s.value * 100
可能溢出INT_MAX
,从而导致不确定的行为。
gcc会不断地准确调整哪些代码会触发-Wsign-conversion
,而不会触发s.value * 100UL
,因为它有很多误报,因此使用不同的版本可能会得到不同的结果。
要解决此问题,您可以更改为sizeof(unsigned char)
(并抛弃比冗余更糟的is very large, you could consider doing a check that the value is not too big (say, under
)。但是在进行乘法运算之前,请考虑一下如果s.value import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ConcreteNotification extends NotificationBase {
@Autowired
public ConcreteNotification(NotificationEnvironment notificationEnvironment) {
super(notificationEnvironment);
}
}
SIZE_MAX / 200`!会发生什么情况。