Code::Blocks Forums

User forums => Help => Topic started by: Mishkafofer on May 05, 2018, 05:54:19 pm

Title: [SOLVED] No error when using pointer with no address
Post by: Mishkafofer 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
Title: Re: [HELP] No error when using pointer with no address
Post by: Mishkafofer 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
#include <stdio.h>

{
    int *p1 = NULL, *p2;
    printf("%p", p1); //points to NULL
    printf("%p", p2); //point nowhere, should crash but CB ignores it.
}
Title: Re: [HELP] No error when using pointer with no address
Post by: oBFusCATed 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.
Title: Re: [HELP] No error when using pointer with no address
Post by: sodev 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 ;)