An unset C pointer is not null -


i messing around c pointers. when compile , run following code.

example 1:

#include <stdio.h>  int main() {     int k;     int *ptr;     k = 555;     if (ptr == null) {         printf("ptr null\n");     } else {         printf("ptr not null\n");         printf("ptr value %d\n", *ptr);     }     printf("ptr address %p\n", ptr); } 

i output:

ptr not null ptr value 1 ptr address 0x7fff801ace30 

if don't assign value k:

example 2:

#include <stdio.h>  int main() {     int k;     int *ptr;     if (ptr == null) {         printf("ptr null\n");     } else {         printf("ptr not null\n");         printf("ptr value %d\n", *ptr);     }     printf("ptr address %p\n", ptr); } 

then output expect:

ptr null ptr address (nil) 

similarly if define variables outside function:

example 3:

#include <stdio.h>  int k; int *ptr;  int main() {     k = 555;     if (ptr == null) {         printf("ptr null\n");     } else {         printf("ptr not null\n");         printf("ptr value %d\n", *ptr);     }     printf("ptr address %p\n", ptr); } 

output:

ptr null ptr address (nil) 

in first example, ptr has address , value, expected behaviour? if then:

  • why ptr have address , value?
  • where these come from, sets them?
  • how correctly define null pointers in local scope , keep them null until i'm ready use?

i compiling 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) 

edit

i have numbered examples above clarity.

based on dietrich's answer did bit of searching , found question: why global variables initialized '0', not local variables?.

local variables in c not automatically initialized, global variables automatically initialized.

int x; // 0 default static int y; // 0 default  void function() {     static int z; // 0 default     int w; // not initialized!  anything! } 

the value of uninitialized variable unspecified. in practice, can mean different things.

  • it may zero, if compiler or runtime zeroes memory before used.

  • it may filled sentinel 0xdeadbeef or 0xeeeeeeee.

  • it may contain garbage whatever last in particular memory location. (this approach common.)

you using gcc, uses third approach, seeing garbage last function use memory location. can run program valgrind (highly recommended), , spit out error messages using uninitialized memory, although not guaranteed catch errors.

the correct way things

one option explicitly initialize local variables if going use them.

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

i prefer leave variables uninitialized if value not used, since compiler , valgrind can give me diagnostic messages letting me know understanding of code incorrect, if turns out uninitialized memory used.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -