未设定的C指针不为空 [英] An unset C pointer is not null

查看:143
本文介绍了未设定的C指针不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与C指针瞎搞。当我编译并运行下面的code。

I am messing around with C pointers. When I compile and run the following code.

实施例1:

#include <stdio.h>

int main()
{
    int k;
    int *ptr;
    k = 555;
    if (ptr == NULL) {
        printf("ptr is NULL\n");
    } else {
        printf("ptr is not NULL\n");
        printf("ptr value is %d\n", *ptr);
    }
    printf("ptr address is %p\n", ptr);
}

我得到的输出:

ptr is not NULL
ptr value is 1
ptr address is 0x7fff801ace30

如果我不分配一个值,K:

If I don't assign a value to k:

例2:

#include <stdio.h>

int main()
{
    int k;
    int *ptr;
    if (ptr == NULL) {
        printf("ptr is NULL\n");
    } else {
        printf("ptr is not NULL\n");
        printf("ptr value is %d\n", *ptr);
    }
    printf("ptr address is %p\n", ptr);
}

则输出如我所料:

Then the output is as I expect:

ptr is NULL
ptr address is (nil)

同样的,如果我定义的函数外部变量:

Similarly if I define the variables outside the function:

实施例3:

#include <stdio.h>

int k;
int *ptr;

int main()
{
    k = 555;
    if (ptr == NULL) {
        printf("ptr is NULL\n");
    } else {
        printf("ptr is not NULL\n");
        printf("ptr value is %d\n", *ptr);
    }
    printf("ptr address is %p\n", ptr);
}

输出:

ptr is NULL
ptr address is (nil)

在第一个例子,其中PTR具有地址和值,这是预期的行为?如果是这样的话:

In the first example, where ptr has an address and value, is this expected behaviour? If so then:


  • 为什么PTR有一个地址和价值?

  • 在哪里这些来自何方,什么套呢?

  • 如何正确地在局部范围内定义空指针,让他们空,直到我已经准备好使用?

我用gcc在Ubuntu 04年4月12日在x64编译:

I am compiling with gcc on Ubuntu 12.04.04 on x64:

root@dev:~# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

修改

我有编号我的例子上面清晰。

I have numbered my examples above for clarity.

根据迪特里希的答案,我做了一些搜索,发现这个问题:<一href=\"http://stackoverflow.com/questions/14049777/why-global-variables-are-always-initialized-to-0-but-not-local-variables\">why全局变量总是被初始化为'0',而不是局部变量?。

Based on Dietrich's answer I did a bit of searching and found this question: why global variables are always initialized to '0', but not local variables?.

推荐答案

用C的局部变量不会被自动初始化,只有全局变量自动初始化。

Local variables in C are not automatically initialized, only global variables are automatically initialized.

int x; // 0 by default
static int y; // 0 by default

void function()
{
    static int z; // 0 by default
    int w; // Not initialized!  Could be anything!
}

未初始化变量的值的未指定的。在实践中,这意味着不同的事情。

The value of an uninitialized variable is unspecified. In practice, this can mean different things.


  • 这可能是零,如果你的编译器或运行时零之前它的内存。

  • It may be zero, if your compiler or runtime zeroes memory before it is used.

这可能是充满定点像 0xdeadbeef 0xeeeeeeee

It may be filled with a sentinel like 0xdeadbeef or 0xeeeeeeee.

它可能包含任何上次在那个特定的内存位置的垃圾。 (此方法是最常见的。)

It may contain garbage from whatever was last in that particular memory location. (This approach is the most common.)

您正在使用GCC,它使用了第三种方法,让你从最后一个函数看到垃圾使用该内存位置。你可以用Valgrind的(强烈推荐)运行程序,它可能会吐出使用未初始化的内存错误信息,但不保证捕获所有的错误。

You are using GCC, which uses the third approach, so you are seeing garbage from the last function to use that memory location. You can run the program with Valgrind (highly recommended), and it will probably spit out error messages for using the uninitialized memory, although it is not guaranteed to catch all errors.

一个选项是显式初始化本地变量,如果你打算使用它们。

One option is to explicitly initialize local variables if you are going to use them.

void function()
{
    int *ptr = NULL;
    ...
}

我preFER离开变量初始化,如果不使用的值,因为编译器和Valgrind的可以给我诊断消息让我知道,我的code的理解是不正确,如果事实证明,未初始化的内存被使用。

I prefer to leave variables uninitialized if the value is not used, since the compiler and Valgrind can give me diagnostic messages letting me know that my understanding of the code is incorrect, if it turns out that the uninitialized memory is used.

这篇关于未设定的C指针不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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