general protection exception is written when execute my program why is it happen.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naobi
    New Member
    • May 2012
    • 1

    general protection exception is written when execute my program why is it happen.

    my code in c++ is
    Code:
    #include<iostream.h>
    main()
    {int a[10],x[5];
    a[-7]=15;
    x[5]=20;
    cout<<x[-5];
    }
    Last edited by Meetee; May 8 '12, 06:57 AM. Reason: code tags added
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    because -7 and -5 are outside the bounds of the array and result in undefined behaviour.

    Most likely when you access with these indices you hit a memory address that is not current valid for the program and hence the exception.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What do you want to happen when you use those negative indices?

      Comment

      Working...