Author Topic: [SOLVED] No error when using pointer with no address  (Read 272 times)

Offline Mishkafofer

  • Single posting newcomer
  • *
  • Posts: 4
[SOLVED] No error when using pointer with no address
« on: May 05, 2018, 05:54:19 pm »
Hi all,
I saw a code that according to lecturer in UNI should create an error. I attached a print screen from lecturer pptx where he posted an error message from Visual Studio. I was expecting to get similar error from CB but didn't. I not sure if it's CB issue or compiler. I use GCC.
The code is very simple. The IDE is expected to give error when trying to access pointer with garbage address.
Here is the code:
https://pastebin.com/yC5AcGiF
« Last Edit: May 05, 2018, 06:29:14 pm by Mishkafofer »

Offline Mishkafofer

  • Single posting newcomer
  • *
  • Posts: 4
Re: [HELP] No error when using pointer with no address
« Reply #1 on: May 05, 2018, 05:57:00 pm »
I am new to this forum, i am not sure if using paste bin is appropriate. Anyway i preferred paste bin due to code highlights.
Here is the code directly.
Code: [Select]
#include <stdio.h>

{
    int *p1 = NULL, *p2;
    printf("%p", p1); //points to NULL
    printf("%p", p2); //point nowhere, should crash but CB ignores it.
}

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11126
    • Travis build status
Re: [HELP] No error when using pointer with no address
« Reply #2 on: May 05, 2018, 06:05:29 pm »
If you're on linux or mac you can achieve the same error detection using valgrind or some of the sanitizers (address or memory).

And yes this is more a feature of the compiler/runtime than something codeblocks provides.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline sodev

  • Almost regular
  • **
  • Posts: 166
Re: [HELP] No error when using pointer with no address
« Reply #3 on: May 05, 2018, 06:29:21 pm »
This is a feature of the MSVC runtime that does some checks at runtime in debug mode and displays these dialogs, i doubt gcc offers something like that.

And your code example should not crash, you just output the value of that uninitialized pointer, this doesnt crash but just displays its random value. It would very likely crash if you would dereference that pointer, unless by chance the random value points to a valid address you have access to ;)