Author Topic: C::B .exe file crashes when I put a pointer in the source file  (Read 206 times)

Offline tomsonjack

  • Single posting newcomer
  • *
  • Posts: 2
C::B .exe file crashes when I put a pointer in the source file
« 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?
« Last Edit: November 19, 2017, 04:06:06 am by tomsonjack »

Online stahta01

  • Lives here!
  • ****
  • Posts: 5991
    • My Best Post
Re: C::B .exe file crashes when I put a pointer in the source file
« Reply #1 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

Please read the rules before you post again on this site
http://forums.codeblocks.org/index.php/topic,9996.0.html
« Last Edit: November 19, 2017, 06:13:08 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Testing, compiling CB Trunk against wxWidgets 3.0.2.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline sodev

  • Multiple posting newcomer
  • *
  • Posts: 110
Re: C::B .exe file crashes when I put a pointer in the source file
« Reply #2 on: November 19, 2017, 06:25:30 am »
Dereferencing an uninitialized pointer is undefined behavior, most probably you wanted to do
Code: [Select]
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.

Offline tomsonjack

  • Single posting newcomer
  • *
  • Posts: 2
Re: C::B .exe file crashes when I put a pointer in the source file
« Reply #3 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.