为什么Visual Studio向我警告“取消引用NULL指针"?当我想检查结构分配时? [英] Why Visual Studio is warning me of "Derefencing NULL pointer" when I want to check a struct allocation?

查看:131
本文介绍了为什么Visual Studio向我警告“取消引用NULL指针"?当我想检查结构分配时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个随机结构,例如国际象棋的位置.

Say I have a random struct, for example, a chess position.

typedef char chessPos[2];

并且我已经链接了国际象棋位置列表.

and I have linked list of chess positions.

typedef struct _chessPosCell
{
    chessPos position;
    struct _chessPosCell* next;
} chessPosCell;

现在,我想创建一个指向列表的新指针.所以我使用以下内容:

Now, I want to create a new pointer to a list. so I use the following:

chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell));

现在,我想检查内存是否已正确分配.因此,我想创建一个特定的功能来检查代码中的每个分配.

Now, I want to check if the memory has been allocated correctly. So I want to create a specific function to check EVERY allocation in my code.

void checkAllocation(void* ptr)

    {
        if (ptr == NULL)
        {
            printf("Memory allocation failure. \n");
            exit(1);
        }
    }

我的问题是,如何发送新分配的内存?

My question is, how do I send my new allocated memory?

1.

checkAllocation(&answer);

checkAllocation(answer);

(区别仅在于'&')

(The difference is just the '&')

我问这个是因为我一直在和一个朋友讨论.我使用的是选项1,因为选项2给了我Visual Studio警告:取消引用NULL指针"答案".朋友说我需要使用选项2,因为我想检查答案"分配,而不是地址分配.但是,选项2给了我上面提到的警告!

I'm asking this because I've been discussing with a friend. I'm using option 1, because option 2 gives me Visual Studio warning of "Derefencing NULL pointer "answer". And the friend says I need to use option 2, because I want to check "answer" allocation, and not it's address allocation. However, option 2 gives me the warning mentioned above!

所以我有点困惑.有人可以向我解释这部分吗?预先感谢!

So I've been a bit confused. Can anyone please explain this part to me? Thanks in advance!

推荐答案

使用 checkAllocation(answer); .

定义 chessPosCell * answer =(chessPosCell *)malloc(sizeof(chessPosCell)); 调用 malloc 来保留一些内存. malloc 返回该内存的地址(或空指针).该定义将 answer 的值初始化为具有该地址.

The definition chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell)); calls malloc to reserve some memory. malloc returns the address of that memory (or a null pointer). The definition initializes the value of answer to have that address.

checkAllocation(& answer); 会将名为 answer 的对象的地址传递给 checkAllocation . chessAllocation(answer)将名为 answer 的对象的值传递给 checkAllocation .这是 answer 的值,它是 malloc 返回的地址,也是您要检查的内容.

checkAllocation(&answer); would pass the address of the object named answer to checkAllocation. chessAllocation(answer) passes the value of the object named answer to checkAllocation. It is the value of answer that is the address return by malloc, and that is what you want to check.

顺便说一句,最好将定义写成 chessPosCell * answer = malloc(sizeof * answer)); :

Incidentally, the definition is better written as chessPosCell *answer = malloc(sizeof *answer));:

  • 通过使用 sizeof * answer 而不是 sizeof(chessPosCell),您可以得到所指向类型的大小.即使以后在代码编辑中更改了 answer 的类型,这也将保持正确.
  • C会自动将 void * 转换为其他指向对象的指针类型,因此无需强制转换为(chessPosCell *).有时使用强制转换可以抑制有关错误的警告消息,因为编译器认为强制转换是故意的,它们表明作者知道自己在做什么.
  • By using sizeof *answer instead of sizeof(chessPosCell), you get the size of the type being pointed to. This will remain correct even if the type of answer is later changed in code edits.
  • C automatically converts from void * to other pointer-to-object types, so the cast to (chessPosCell*) is unnecessary. And sometimes using a cast can suppress a warning message about a bug, because compilers assume that casts are intentional, that they indicate the author knows what they are doing.

这篇关于为什么Visual Studio向我警告“取消引用NULL指针"?当我想检查结构分配时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆