Char 指针与 NULL 的比较

发布于 2024-10-17 19:32:08 字数 786 浏览 2 评论 0原文

我在 SUSE Linux 上运行我的代码。我有一个指针,我在函数中将其设置为= NULL。但是当我尝试在 while 循环中将同一指针与 NULL 进行比较时,问题就出现了。这导致程序崩溃。我在下面的示例代码中重现了我的问题。有人可以告诉我这里发生了什么事吗?

我的代码如下:

#include <stdio.h>

int func(char *,int);

int main()
{
    char buffer[20];
    int i =20;

    int temp = func(buffer,i);

    if ( (temp == 0) && (buffer != NULL) )
    {
        printf("inside loop \n");
    }
}

int func(char *ad,int a)
{
    ad = NULL;
    printf("Integer is %d \n", a);
    return(0);
}

问题是比较 buffer != NULL 失败,并且控制进入循环内部,这在理想情况下不应该发生。我已经解决了这个问题,方法是:

ad[0] = NULL 并将比较更改为buffer[0] != NULL

由于 NULL 仅在指针上下文中使用,因此这是错误的代码。我可以在解决方法中使用 '\0' 而不是 NULL,并且不用编写“糟糕的代码”,但我真的想知道这里发生了什么。有人可以澄清一下吗?

非常感谢, 阿迪亚

I am running my code on SUSE Linux. I have a pointer which I make = NULL in a function. But the problem arises when I try to compare the same pointer with NULL in a while loop. This is leading to the program crashing. I have reproduced my problem in the sample code below. Can someone please tell me what is happening here?

My code is as below:

#include <stdio.h>

int func(char *,int);

int main()
{
    char buffer[20];
    int i =20;

    int temp = func(buffer,i);

    if ( (temp == 0) && (buffer != NULL) )
    {
        printf("inside loop \n");
    }
}

int func(char *ad,int a)
{
    ad = NULL;
    printf("Integer is %d \n", a);
    return(0);
}

The problem is that the comparison, buffer != NULL is failing and the control goes inside the loop, which should not be happening ideally. I have resolved this, by doing this:

ad[0] = NULL and the comparison changed to buffer[0] != NULL.

Since NULL is used only in a pointer context, this is bad code. I could use '\0's instead of the NULL in my workaround and get away with not writing 'bad code', but I really want to know what is happening here. Can someone please clarify?

Thanks a ton,
Aditya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中性美 2024-10-24 19:32:08

buffer 不是指针,它不能为 NULL。

您的func缓冲区的复制地址设置为NULL。这根本不影响缓冲区。

编辑:扩展解释

char buffer[20];

这在堆栈上的某个位置保留了 20 个字节。它们没有以任何方式初始化。由于您有 20 个字节的内存,显然这些字节必须驻留在某个地址。

int temp = func(buffer,i);

这将获取缓冲区中 20 个字节的地址,并将其传递给 func。

int func( char *ad,int a)
{

ad = NULL;

这里,在堆栈上的新位置处,有一个新的指针变量,该变量仅在 func 执行时存在。这个指针变量位于一个地址,并且指向一个地址。您更改此指针,这会影响它指向的位置。但它不会以任何方式更改您的原始缓冲区,因为 ad 只是堆栈上的一个临时变量,其中包含 buffer 变量中字节的地址(直到您设置这个临时变量为NULL)。

buffer isn't a pointer, it cannot be NULL.

Your func sets the copied address of buffer to NULL. That does not affect buffer at all.

EDIT: Extended explanation

char buffer[20];

This reserves 20 bytes somewhere on stack. They're not initialized in any way. As you have 20 bytes of memory, obviously these bytes must reside at some address.

int temp = func(buffer,i);

This takes the address of your 20 bytes in buffer, and passes it to func.

int func( char *ad,int a)
{

ad = NULL;

Here you have, at a new position on stack, a new pointer variable which will exist only while func is executing. This pointer variable is at an address, and points to an address. You change this pointer, which affects where it points. But it will not change your original buffer in any way, since ad is only a temporary variable on stack that contained the address of the bytes in your buffer variable (until you set this temporary variable to NULL).

万水千山粽是情ミ 2024-10-24 19:32:08

两个问题:

  1. func 中,您尝试修改 ad 的值,而不是修改 ad 的值> 指向。这是行不通的,因为参数值的更改不会反映在调用者中。您需要将该代码更改为

    <前><代码>
    int func(char **ad, int a)
    {
    *广告=空;
    printf("整数为 %d\n", a);
    返回0;
    }

    不幸的是,这不适用于其余代码,因为...

  2. buffer 被声明为数组对象,而不是指针,并且不能修改数组对象; IOW,buffer 无法分配。更不用说&buffer的类型是char (*)[20],而不是char **

这是您的源代码的修改版本,它将“起作用”,尽管我不确定您想要完成什么:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

int func(char **ad, int a)
{
  *ad = NULL;                   // WOOPA! WOOPA! MEMORY LEAK!!! MEMORY LEAK!!!
  printf("Integer is %d\n", a);
  return 0;
}

int main(void)
{
  char *buffer = malloc(sizeof *buffer * SIZE);
  int i = 20;
  int temp = func(&buffer, i);
  if (temp == 0 && buffer != NULL)
    printf("Inside loop\n");
  return 0;
}

Two problems:

  1. In func, you're attempting to modify the value of ad, not what ad points to. This won't work, as changes to the parameter value are not reflected in the caller. You would need to change that code to

    
    int func(char **ad, int a)
    {
      *ad = NULL;
      printf("Integer is %d\n", a);
      return 0;
    }
    
    

    Unfortunately, this won't work with the rest of the code, since...

  2. buffer is declared as an array object, not a pointer, and you cannot modify an array object; IOW, buffer cannot be assigned to. Not to mention that the type of &buffer is char (*)[20], not char **.

Here's a modified version of your source that will "work", although I'm not sure what you're trying to accomplish:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

int func(char **ad, int a)
{
  *ad = NULL;                   // WOOPA! WOOPA! MEMORY LEAK!!! MEMORY LEAK!!!
  printf("Integer is %d\n", a);
  return 0;
}

int main(void)
{
  char *buffer = malloc(sizeof *buffer * SIZE);
  int i = 20;
  int temp = func(&buffer, i);
  if (temp == 0 && buffer != NULL)
    printf("Inside loop\n");
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文