Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:Cppcheck:可能的空点解引用_C_Null_Cppcheck - Fatal编程技术网

C:Cppcheck:可能的空点解引用

C:Cppcheck:可能的空点解引用,c,null,cppcheck,C,Null,Cppcheck,因此,在main.c中,我得到了这部分代码,如果加密的不是空的,则打印它的内容。就这么简单 cpp错误为: [main.c:40]:(错误)可能的空指针解引用:加密- 否则,在第31行检查encrypted是否为null是多余的 守则: char* encrypted = bmp_encrypt(key, text); if(encrypted != NULL) //error points here (line 31) { printf("Encrypted:"

因此,在main.c中,我得到了这部分代码,如果加密的不是空的,则打印它的内容。就这么简单

cpp错误为:

[main.c:40]:(错误)可能的空指针解引用:加密- 否则,在第31行检查encrypted是否为null是多余的

守则:

char* encrypted = bmp_encrypt(key, text);
    if(encrypted != NULL) //error points here (line 31)
    {
        printf("Encrypted:");
        for(int i=0; i<strlen(text);i++)
        {
            printf("%x ", (unsigned char) encrypted[i]);
        }
        printf("\n");
    }
    else{printf("Encrypted:%s\n", encrypted);} //this is line 40
char*encrypted=bmp\u加密(密钥,文本);
if(encrypted!=NULL)//此处有错误点(第31行)
{
printf(“加密:”);

对于(int i=0;i,只有在
encrypted
为空时,才会输入代码的
else
块。因此,您将传递一个空指针,指向可以调用的
printf

由于您知道指针在该点为空,只需显式打印它为空:

else{printf("Encrypted: (null)\n");} 

只有当
encrypted
为空时,才会输入代码的
else
块。因此,您正在向可以调用的
printf
传递一个空指针

由于您知道指针在该点为空,只需显式打印它为空:

else{printf("Encrypted: (null)\n");} 

如果到达第40行,
encrypted
肯定为空,
printf(“加密的:%s\n”,encrypted)
是未定义的行为。如果到达第40行,
encrypted
肯定为空,
printf(“加密的:%s\n”,encrypted)
是未定义的行为。@Mathue24:请注意,glibc让您打印
(null)只是出于礼貌
如果你传递一个空字符串-这不是标准要求的,gcc本身通常会重写
printf
调用
put
调用,如果传递空字符串,它会直接崩溃。如果你需要添加调试打印以查看字符串是否为空,请按照上面所示显式执行。@Mathue24:注意,这只是一种礼貌glibc让您在传递空字符串时打印
(null)
——这不是标准要求的,gcc本身通常会重写
printf
调用到
put
调用,如果传递空字符串,它会直接崩溃。如果您需要添加调试打印以查看字符串是否为null,请按上面所示显式执行。