Thread: segment fault?

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    segment fault?

    I dont' know what this is... it gives me a run-time error (the program just craps)... I debugged it and the debugger said it was an "access violation (segment fault)" here:
    Code:
    	for(v=0;v<vertical;v++)	//loop through rows
    	{
    		if(debug)
       			vcount++;
    		for(h=0;h<horizontal;h++) 	//loop through cols
    		{
    			if(debug)
    				hcount++;
    			tank[v][h]=emptychar;	//fill with empty space
    		}
    	}
    and that is part of this class:
    Code:
    class lifeClass
    {
    	public:
    		lifeClass(unsigned int,unsigned int,unsigned int);
      			//constructor (tank length,tank width,organism#)
    		void step();	//step through program
    		bool debg();	//returns bool debug
    		bool nextstep();	//returns true for another generation
    		~lifeClass();	//destructor
    	private:
      		void randxy(unsigned int&,unsigned int&);	//randomizes X and Y
      		void drawtemptank();	//debug - Draws temporary tank
    		void drawtank();	//draws tank to screen
    		void copytank();	//copies next tank to current tank
    		char live(unsigned int,unsigned int);	//living characters
    		char die(unsigned int,unsigned int);	//dying characters
    		unsigned int countNeighbors(unsigned int,unsigned int);	//counts neighbors
    		
    		bool debug;	//debugging statements - see lifeClass
    		
    		bool cont;	//continue to next generation?
    		char tank[][];	//living space
    		char emptychar,fillchar,killchar,bornchar;	//characters used
     		unsigned int vertical,horizontal;	//length and width of living space
    		unsigned int clickcount;	//counts clicks (steps) taken by the program
    };
    I would appreciate some help with what this is, and if there's a better way (which I'm sure there is) to write a 2D (array of arrays) array, please post...

    secondly, you define a new char array like this
    Code:
    char *array=new char[int];
    but how/can you do that for a 2D array? and how do you delete a 2D array?

    sorry for all the questions, but, well, I have a lot of questions
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    but how/can you do that for a 2D array? and how do you delete a 2D array?
    I think you just have to allocate it one element at a time or one dimension at a time (?)

    Code:
      for(int x=0;x<ROWS;x++)
         for(int y=0;y<COLS;y++)
               array[x][y]=new Type;
    But I could be incorrect...

    EDIT:

    This seems to work:

    Code:
    int *array[10];
      for(int x=0;x<10;x++)
               array[x]=new int[10];
    //or
    int *array[10][10]
         for(int x=0;x<10;x++)
            for(int y=0;y<10;y++)
                   array[x][y]=new int[10];
    Last edited by JaWiB; 09-17-2003 at 08:49 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    JaWiB's example is essentially a 3D array (though in essence, correct). To do this, I recommend using stl::vector, or a matrix class of some sort (or other vector class). Anyways, if this is the way you want to do it, here is a small example:
    Code:
    int** array2d = new int*[10];
    for(int i = 0; i < 10; ++i)
      array2d[i] = new int[10];
    
    ... etc ...
    
    for(int j = 0; j < 10; ++j)
      delete array2d[j];
    delete array2d;
    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    If you allocate something with new[], then you should frre it with delete [] operator. If you free the memory with just delete, it will probably turn wrong, for example memory will be really unallocated, but the destructor will be called only for first element in your array. Here we have integers, so it doesn't matter, but it is still wrong to use delete instead of delete[], because it's unpredictable and compiler dependent.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    cool thanks for all the help... I would do vectors, but I don't know how to use them/exactly what they are... =/ I still need to look into that some time...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  3. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM
  4. Segment fault on double whammy char pointer
    By Kleid-0 in forum C Programming
    Replies: 18
    Last Post: 12-20-2004, 05:51 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM