如何检查指针是否指向NULL?

时间:2022-04-04 07:18:51

The title may be a little bit of a misnomer... just because I'm not sure if my char pointer is pointing to NULL, or if it's just pointing to a char array of size 0.

标题可能有点用词不当......只是因为我不确定我的char指针是否指向NULL,或者它只是指向大小为0的char数组。

So I have

所以我有

char* data = getenv("QUERY_STRING");

And I want to check if data is null (or is length < 1). I've tried:

我想检查数据是否为空(或长度<1)。我试过了:

if(strlen(data)<1) 

but I get an error:

但是我收到一个错误:

==24945== Invalid read of size 1
==24945==    at 0x8048BF9: main (in /cpp.cgi)
==24945==  Address 0x1 is not stack'd, malloc'd or (recently) free'd

I've also tried

我也试过了

if(data == NULL)

but with the same result.

但结果相同。

What's going on here? I've already tried cout with the data, and that works fine. I just can't seem to check if it's null or empty.

这里发生了什么?我已经尝试了cout数据,这很好。我似乎无法检查它是否为空或空。

I realize these are two different things (null and empty). I want to know which one data would be here, and how to check if it's null/empty.

我意识到这些是两个不同的东西(空和空)。我想知道这里有哪一个数据,以及如何检查它是否为空/空。

2 个解决方案

#1


21  

With getenv, you have to handle both cases! (Yay!) If the environment variable is not set, then the function returns NULL. If it is set, then you get a pointer to the value it's set to, which may be empty. So:

使用getenv,你必须处理这两种情况! (是的!)如果未设置环境变量,则该函数返回NULL。如果设置了,那么你得到一个指向它设置的值的指针,它可能是空的。所以:

const char* data = getenv("QUERY_STRING");
if (data != NULL && data[0] != '\0') {
    // Variable is set to value with length > 0
    // ...
}

Obviously, you need to check if it's NULL before attempting to determine its length or read any of the characters it points to -- this is why the two conditions in the above if are ordered the way they are.

显然,在尝试确定其长度或读取它指向的任何字符之前,您需要检查它是否为NULL - 这就是为什么上面的两个条件按照它们的方式排序的原因。

#2


4  

Generally you'd check with something like this. The first part checks the pointer for null, the second checks for an empty string by checking the first character for the null terminator at the end of every string.

一般来说,你会检查这样的事情。第一部分检查指针是否为null,第二部分通过检查每个字符串末尾的空终止符的第一个字符来检查空字符串。

if (data == NULL || data[0] == 0)

Your problem looks like some specific interaction between getenv and strlen that isn't standard.

您的问题看起来像getenv和strlen之间的一些特定的交互,这不是标准的。

#1


21  

With getenv, you have to handle both cases! (Yay!) If the environment variable is not set, then the function returns NULL. If it is set, then you get a pointer to the value it's set to, which may be empty. So:

使用getenv,你必须处理这两种情况! (是的!)如果未设置环境变量,则该函数返回NULL。如果设置了,那么你得到一个指向它设置的值的指针,它可能是空的。所以:

const char* data = getenv("QUERY_STRING");
if (data != NULL && data[0] != '\0') {
    // Variable is set to value with length > 0
    // ...
}

Obviously, you need to check if it's NULL before attempting to determine its length or read any of the characters it points to -- this is why the two conditions in the above if are ordered the way they are.

显然,在尝试确定其长度或读取它指向的任何字符之前,您需要检查它是否为NULL - 这就是为什么上面的两个条件按照它们的方式排序的原因。

#2


4  

Generally you'd check with something like this. The first part checks the pointer for null, the second checks for an empty string by checking the first character for the null terminator at the end of every string.

一般来说,你会检查这样的事情。第一部分检查指针是否为null,第二部分通过检查每个字符串末尾的空终止符的第一个字符来检查空字符串。

if (data == NULL || data[0] == 0)

Your problem looks like some specific interaction between getenv and strlen that isn't standard.

您的问题看起来像getenv和strlen之间的一些特定的交互,这不是标准的。