关于C6011:取消对NULL指针的引用解决方案

在函数的址传递过程中,都应该习惯性考虑做空指针判断,否则很容易出现莫名奇妙的问题。这次就是因为忘了这茬导致半天找不到问题所在,做个文章警醒一下自己,也提醒大家注意这些小细节
如下图:
image
实际上是因为是因为忘了做空指针判断,加上之后问题解决:

#include <iostream>

using namespace std;

int* test(int count)
{
    int* p = (int*)malloc(sizeof(int) * count);
    if (!p)
    {
        cout << "p is null" << endl;
    }
    else
    {
        *(p + 0) = 5;
    }

    return p;
}

int main()
{
    int* p = test(3);
    *(p + 1) = 6;
    *(p + 2) = 7;

    for (int i = 0; i < 3; i++)
    {
        cout << *(p + i) << endl;
    }

    free(p);
    return 0;
}

参考:https://blog.csdn.net/qq_41649549/article/details/118927224

posted @   荒年、  阅读(1636)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示