Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: tomsonjack on November 19, 2017, 04:03:38 am

Title: C::B .exe file crashes when I put a pointer in the source file
Post by: tomsonjack on November 19, 2017, 04:03:38 am
Hi guys, when I put a pointer in my source code, the code can be compiled, and the .exe file also can be generated.
But the .exe can't be executed properly, and it just crushed on the Windows 7.

The followed is the software version.
C::B version:13.12
SDK version:1.19.0
wx version: 2.8.12
MinGW version: 4.7.1

The source code is:

#include <stdio.h>

void main()
{   int a, *p;
    a=2;
    *p=a;
    printf("a=%d",a);
}

Could you please help me with this issue?
Title: Re: C::B .exe file crashes when I put a pointer in the source file
Post by: stahta01 on November 19, 2017, 04:44:20 am
Find a site that teaches programming. Edit2: Your code is likely trying to write to address zero which is undefined.
Edit: Or a site that supports your compiler. They will likely want a build log http://forums.codeblocks.org/index.php/topic,9996.0.html (http://forums.codeblocks.org/index.php/topic,9996.0.html)

Please read the rules before you post again on this site
http://forums.codeblocks.org/index.php/topic,9996.0.html (http://forums.codeblocks.org/index.php/topic,9996.0.html)
Title: Re: C::B .exe file crashes when I put a pointer in the source file
Post by: sodev on November 19, 2017, 06:25:30 am
Dereferencing an uninitialized pointer is undefined behavior, most probably you wanted to do
Code
p = &a;
, but still there is no sense in this.

But indeed here is no compiler problem and for sure no CodeBlocks problem, only a user doesnt know c basics problem :). No need for a build log because this is syntactically correct and builds fine.
Title: Re: C::B .exe file crashes when I put a pointer in the source file
Post by: tomsonjack on November 21, 2017, 09:27:00 am
Hi guys, Thanks a lot.  An uninitialized pointer is root cause. This is a C basics knowledge, which I should konw.