关于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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM