Thread: Access Violation: Segmentation Fault help

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    11

    Access Violation: Segmentation Fault help

    Whenever I try running this program, it brings up an error message "Access Violation: Segmentation Fault". However, I cannot find the problem. Here is my code:
    Code:
    #include <stdio.h>
    
    main()
    {
          char screen[80][24];
          /* Technical integers go here: (Integers that the user doesn't see.) */
          int initializer1, initializer2, frameCounter, displayScreenX, displayScreenY, individualEnemy, enemiesLeft, defendersPlaced;
          /* Integers that the user sees go here: */
          int selectCourse, life, money, level;
          bool retry;
          struct courses
          {
                 int whereToMoveNextX[840], whereToMoveNextY[840];
                 char courseLayout[80][24];
          } course1; /* More courses here. */
          struct enemies
          {
                 int enemyLife, enemyCoordinatesX, enemyCoordinatesY, spacesMoved;
                 char enemyType;
          } enemyData[100000];
          struct defenders
          {
                 int defenderCoordinatesX, defenderCoordinatesY, defenderTargetSpacesX[840], defenderTargetSpacesY[840];
                 char defenderType;
          } defenderData[1680]; /* 1680 is the game screen dimensions. */
          
          
          /* Course1 layout here: */
          for (initializer1=0; initializer1<24; initializer1++)
          {
              for (initializer2=0; initializer2<80; initializer2++)
              {
                  course1.courseLayout[initializer2][initializer1]=176; /* This sets course1's background. */
              }
              for (initializer2=70; initializer2<80; initializer2++)
              {
                  course1.courseLayout[initializer2][initializer1]=' '; /* This sets a margin at the edge of the screen. */
              }
          }
          /* This sets the track of course1: */
          for (initializer1=0; initializer1<24; initializer1++)
          {
              course1.courseLayout[1][initializer1]=' ';
          }
          
          
          /* Set more course layouts here. */
          
          for (initializer1=0; initializer1<100000; initializer1++)
          {
              enemyData[initializer1].enemyLife=0;
              enemyData[initializer1].spacesMoved=0;
              enemyData[initializer1].enemyType=' ';
          } /* This sets all of the enemies' data to 0. */
    Sorry that it's so long. Any help would be appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You should really use memset() for all of those initializations, it's much faster and easier to read.

    No implicit main! > SourceForge.net: Implicit main - cpwiki

    As for the problem, you're wasting so much stack memory:
    - 1920 bytes for char screen
    - 3600 bytes for struct courses
    - 1,800,000 bytes for struct enemies (17 bytes for SINGLE struct, padded to 18, times 100k)
    - 2,839,200 bytes for struct defenders (1689 bytes for SINGLE struct, padded to 1690, times 1680)

    Equals a total of 4,644,720 bytes, which could be much more than the computer allows your program for stack size. Learn dynamic memory allocation and be more efficient with your memory (something tells me you don't need all that data in the first place).

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Quote Originally Posted by memcpy View Post
    You should really use memset() for all of those initializations, it's much faster and easier to read.

    No implicit main! > SourceForge.net: Implicit main - cpwiki

    As for the problem, you're wasting so much stack memory:
    - 1920 bytes for char screen
    - 3600 bytes for struct courses
    - 1,800,000 bytes for struct enemies (17 bytes for SINGLE struct, padded to 18, times 100k)
    - 2,839,200 bytes for struct defenders (1689 bytes for SINGLE struct, padded to 1690, times 1680)

    Equals a total of 4,644,720 bytes, which could be much more than the computer allows your program for stack size. Learn dynamic memory allocation and be more efficient with your memory (something tells me you don't need all that data in the first place).
    Thanks for the reply. How do you use memset()?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    memset(3): fill memory with constant byte - Linux man page

    Did you try typing "man memset" into your terminal, or try using google? Both your terminal and google are very good resources when trying to find what a single function does.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I find the search terms "c++ memset" to work well for most if not all standard C functions.
    Just replace "memset" with the function name.

    Note: "man memset" may work better for non-windows users.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-01-2011, 11:26 PM
  2. Access Violation(Segmentation Fault) error
    By nirvana619 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 08:43 AM
  3. An Access Violation Segmentation Fault. Need Help ASAP
    By darknessfalls in forum C Programming
    Replies: 2
    Last Post: 08-22-2010, 05:56 AM
  4. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM