0

Im not really sure that use of ptr&&(*ptr)
At first, I was not sure about is this for checking NULL ptr or existence of assigned value.
But now after running the code below I found that in both way it just doesnt work: runtime error occurred!

Can somebody explain what nasty thing inside is happening?
GCC 5.3.0

#include <stdio.h>
int main(void){
    int* ptr=NULL;
//    ptr=NULL;
    int res = (ptr && *ptr);

//assigning NULL to *ptr or ptr address results in runtime error. What's happening?
//compiled with gcc.exe (GCC) 5.3.0 on windows

    printf("address: %d \n val: %d \n",ptr,*ptr);
    printf("isNullptr? ptr&& *ptr %d",res);
    return;
}
Jens
  • 69,818
  • 15
  • 125
  • 179
S Simon
  • 3
  • 1
  • You are trying to get value by null pointer which is incorrect. It's like saying: give me i don't know what... – nopasara Apr 01 '17 at 10:05
  • Possible duplicate of [Is it safe to check if a pointer is null, then dereference it in the same if statement?](http://stackoverflow.com/questions/13782629/is-it-safe-to-check-if-a-pointer-is-null-then-dereference-it-in-the-same-if-sta) – msc Apr 01 '17 at 10:37

5 Answers5

3

Following line is completely ok, since short-circuiting prevents ptr from being dereferenced if it's equal to NULL.

int res = (ptr && *ptr);

But following code unconditionally dereferences it, leading to a crash.

int* ptr=NULL;
// ...
printf("address: %p \n val: %d \n",(void *)ptr,*ptr);
//                                             ^~~~

Also, please note that a proper way to print a pointer is by using %p instead of %d, and the pointer has to be casted to void *. I've changed your printf() accordingly.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
3
printf("address: %d \n val: %d \n",ptr,*ptr);

You have trying to de-referencing a NULL pointer. It's undefined behaviour.

From n1256 - §6.5.3.3 :

Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime.

msc
  • 33,420
  • 29
  • 119
  • 214
1

Doing

int * ptr = NULL

...

  printf("address: %d \n val: %d \n",ptr,*ptr);

results in:

  1. First ptr is initialised using the null-pointer constant. For this the C standard guarantees that ptr is not pointing to any valid object. (*1)

    C11 Standard (draft) 6.3.2.3/3:

    If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

  2. Then in the second statement with doing *ptr gets dereferenced, that is it is tried to access what it's pointing to, which is nothing valid (see 1. above), resulting in Undefined Behaviour, from then on anything can happen, including a crash of the program.

    C11 Standard (draft) 6.5.3.2/4:

    If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alk
  • 69,737
  • 10
  • 105
  • 255
0

What do you think *ptr (in the printf) does? It dereferences a pointer that has been assigned with NULL in the initialization. If you pass a NULL to function expecting non-NULL, your program invokes undefined behavior. The proper way to print addresses is using a %p specifier along with a ptr-to-void:

 if (ptr != NULL)
     printf ("Pointer ptr points to address %p\n", (void *)ptr);
 else
     printf ("ptr is NULL\n");
Jens
  • 69,818
  • 15
  • 125
  • 179
0

Technically, you have undefined behaviour. It is a very bad thing.

(remember that a programming language is a specification written in some report - not a software. For C11, read n1570)

In practice, you get a segmentation fault. You are dereferencing -in your first call to printf- a (null) pointer which is always outside of your virtual address space.

You should compile with all warnings & debug info e.g. with gcc -Wall -g if using GCC. Then use a debugger (gdb) and a memory leak detector like valgrind...

You need to read a lot more. Read carefully several books on programming (perhaps start with SICP), and several books on C programming. You might also want to read about operating systems.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547