0

I am trying to simulate the null pointer undefined behaviour. What changes should be made in the below code to introduce null pointer undefined behaviour.

void foo( int * d )
{
    printf("hello\n");
}

int main(void)
{
    int a = 7 ;
    int *b = malloc(sizeof(int)) ;
    foo(b) ;
}
Madara
  • 163
  • 2
  • 11
  • 1
    possible duplicate of [Understanding NULL pointer in C](http://stackoverflow.com/questions/29379663/understanding-null-pointer-in-c) – kittykittybangbang Jul 15 '15 at 04:39
  • it explained the null pointer exception, but i wanted to know how to introduce the null pointer exception here. can you please remove the duplicate flag – Madara Jul 15 '15 at 05:10
  • 1
    There are no exceptions in C, so there cannot be any `NULL` pointer exception in C. – Basile Starynkevitch Jul 15 '15 at 05:45
  • Actually the question is wrong and meaningless, because C don't have exceptions. You need to understand what **undefined behavior** means. – Basile Starynkevitch Jul 15 '15 at 06:15
  • HI Basile, thanks for pointing out that its undefined behavior and not exceptions. I was requesting for assistance in simulating the undefined behavior in C for null pointer dereferencing. The answer mr.Eric Tsui gave was what i was requesting. – Madara Jul 15 '15 at 18:48

3 Answers3

3

Dereferencing a NULL pointer (or some address outside your current address space, often in virtual memory) in C is not an exception, but some undefined behavior (often a segmentation fault). You really should avoid UB.

By definition of undefined behavior, we cannot explain it without going down into very specific implementation details (compiler, runtime, optimization, ASLR, machine code, phase of the moon, ...).

The malloc library function can (and does) fail. You should always test it, at least as:

 int *b = malloc(sizeof(int));
 if (!b) { perror("malloc of int"); exit(EXIT_FAILURE); }; 

To trigger failure of malloc (but very often the first few calls to malloc would still succeed) you might lower the available address space to your program. On Linux, use ulimit in the parent shell, or call setrlimit(2).

BTW, you could even link with your own malloc which is always failing:

  // a silly, standard conforming, malloc which always fail:
  void* malloc(size_t sz) {
    if (sz == 0) return NULL;
    errno = ENOMEM;
    return NULL;
  }

The C programming language does not have exceptions. C++ (and Ocaml, Java, ....) does (with catch & throw statements). Raising an exception is a non-local change of control flow. In standard C you might use longjmp for that purpose. In C++ dereferencing a nullptr is UB (and does not raise any null-pointer exception which does not exist in C++).

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

Based on your code, we can simulate Null Pointer UB like this,

#include<stdio.h>
void foo( int * d )
{
    printf("hello, it is %d\n", *d);//dereference d (produces "Segmentation fault" if d is NULL)
}

int main(void)
{
    int a  = 7 ;
    int *b = NULL; // simulate failed to malloc(sizeof(int))

    foo(&a); // output is "hello, it is 7"
    foo(b); // will trigger something like "Segmentation fault"
}

As pointed by @Basile Starynkevitch, there are no exceptions in C, so here it would be more accurate to say "NULL pointer UB(Undefined Behaviour)" compared to "NULL pointer exception".

Eric Tsui
  • 1,924
  • 12
  • 21
1

Here's how to dereference a null pointer:

 int *b = 0;
 *b = 3;
Buddy
  • 10,874
  • 5
  • 41
  • 58