简体   繁体   English

Char指针与NULL的比较

[英]Comparison of a Char Pointer to NULL

I am running my code on SUSE Linux. 我在SUSE Linux上运行我的代码。 I have a pointer which I make = NULL in a function. 我有一个指针,我在一个函数中使= NULL。 But the problem arises when I try to compare the same pointer with NULL in a while loop. 但是当我尝试在while循环中将同一指针与NULL进行比较时,问题就出现了。 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. 问题是比较, buffer != NULL失败,控制进入循环,理想情况下不应该发生。 I have resolved this, by doing this: 我通过这样做解决了这个问题:

ad[0] = NULL and the comparison changed to buffer[0] != NULL . ad[0] = NULL并且比较更改为buffer[0] != NULL

Since NULL is used only in a pointer context, this is bad code. 由于NULL仅在指针上下文中使用,因此这是错误的代码。 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. 我可以在我的解决方法中使用'\\ 0而不是NULL,并且不会写'坏代码',但我真的想知道这里发生了什么。 Can someone please clarify? 有人可以澄清一下吗?

Thanks a ton, Aditya 非常感谢,Aditya

buffer isn't a pointer, it cannot be NULL. 缓冲区不是指针,它不能为NULL。

Your func sets the copied address of buffer to NULL. 您的func复制的缓冲区地址设置为NULL。 That does not affect buffer at all. 这根本不会影响缓冲区。

EDIT: Extended explanation 编辑:扩展说明

char buffer[20];

This reserves 20 bytes somewhere on stack. 这在堆栈的某处保留了20个字节。 They're not initialized in any way. 他们没有以任何方式初始化。 As you have 20 bytes of memory, obviously these bytes must reside at some address. 由于你有20个字节的内存,显然这些字节必须驻留在某个地址。

int temp = func(buffer,i);

This takes the address of your 20 bytes in buffer, and passes it to func. 这将在缓冲区中获取20个字节的地址 ,并将其传递给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. 在堆栈的新位置,你有一个新的指针变量,它只在func执行时才存在。 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). 但它不会以任何方式更改原始缓冲区,因为ad只是堆栈上的临时变量,包含buffer变量中的字节地址(直到将此临时变量设置为NULL)。

Two problems: 两个问题:

  1. In func , you're attempting to modify the value of ad , not what ad points to. func ,您尝试修改ad的价值, 而不是 ad指向的价值。 This won't work, as changes to the parameter value are not reflected in the caller. 这不起作用,因为参数值的更改不会反映在调用者中。 You would need to change that code to 您需要将该代码更改为

    \nint func(char **ad, int a) { *ad = NULL; printf("Integer is %d\\n", a); return 0; }\nint 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; buffer被声明为数组对象,而不是指针,你不能修改数组对象; IOW, buffer cannot be assigned to. IOW,无法分配buffer Not to mention that the type of &buffer is char (*)[20] , not char ** . 更不用说&buffer的类型是char (*)[20] ,而不是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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM