Segmentation fault.... again

It seems that the “evolve_species()” function is making the prog go into
segmentation fault. I can’t understand why though! that function is at the
moment standalone and has nothing to do with SDL. I’m rebuilding the program
piece by piece trying to figure out which part of it is causing the problem,
but I can’t make any sense on why this function acts on SDL like this.

please help, thanks.

Daniel

#include
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <fstream.h>

#include <SDL/SDL.h>

using namespace std;

class pixel_state {
public:
unsigned int R : 8, G : 8, B : 8;
double state_value;
pixel_state () {};
pixel_state operator + (pixel_state);
pixel_state operator += (pixel_state);
pixel_state operator = (pixel_state);
pixel_state operator / (double);
pixel_state operator * (double);
};

pixel_state pixel_state :: operator + (pixel_state param) {
pixel_state temp;
temp.state_value = state_value + param.state_value;
return (temp);
}

pixel_state pixel_state :: operator += (pixel_state param1) {
pixel_state temp1;
temp1.state_value = temp1.state_value + param1.state_value;
return (temp1);
}

pixel_state pixel_state :: operator = (pixel_state param4) {
pixel_state temp4;
temp4.state_value = param4.state_value;
return (temp4);
}

pixel_state pixel_state :: operator / (double param2) {
pixel_state temp2;
temp2.state_value = (double)floorf(((float)state_value / param2)+0.5);
return (temp2);
}

pixel_state pixel_state :: operator * (double param3) {
pixel_state temp3;
temp3.state_value = (double)floor((state_value * param3)+0.5);
return (temp3);
}

int random_seed () {

int rand_seed;
srand((unsigned int)time((time_t *)NULL));
rand_seed = rand();
return rand_seed;

}

int random_range(int lowest_number, int highest_number)
{
unsigned int w = rand();
srand(w);

if(lowest_number > highest_number){
    int q = highest_number;
    lowest_number = highest_number;
    highest_number = q;
}

int range = highest_number - lowest_number;
return lowest_number + (int)(range * ((double)rand()/(RAND_MAX)));

}

pixel_state** initialize_species (long k, int seed) {

srand((unsigned int)time((time_t *)NULL));

pixel_state **array;

array = new pixel_state* [640];
for (int i=0; i<640; i++) {array[i] = new pixel_state [480];}

ofstream evolution("check_color_initialization.txt");

for (int n=0; n<640; n++) {
    
      for (int l=0; l<480; l++) {
          
          srand((n+rand())*l+(n+l)); 
          
          int state = random_range(1,(k+1));
          
          srand(seed+state);
          array[n][l].R = (int)(((float)rand()/RAND_MAX)*255);
          array[n][l].G = (int)(((float)rand()/RAND_MAX)*255);
          array[n][l].B = (int)(((float)rand()/RAND_MAX)*255);
          
          array[n][l].state_value = state;

          evolution << state << "    " << array[n][l].R << " " << array[n]

[l].B << " " << array[n][l].G << “\n\n”;
}
}
evolution.close();
return array;
}

pixel_state** evolve_species (pixel_state **array, int L, int seed) {

pixel_state **array_to_evolve, counter;

array_to_evolve = new pixel_state* [640];
for (int i=0; i<160; i++) array_to_evolve[i] =new pixel_state [480];

/ofstream evolution(“check_color_evolution.txt”);/

for (int n=0; n<640; n++) {
for (int l=0; l<480; l++) {

    int k,s,p,q;
counter.state_value = 0;

for (k=-L;k<=L;k++) {

	for (s=-L;s<=L;s++) {


		if ((n+k)<0) p=(n+k+640);
		if ((n+k)>640) p=(n+k-640);
		if ((l+s)<0) q=(l+s+480);
		if ((l+s)>480) q=(l+s-480);
		if ((n+k)>0 && (n+k)<640) p=(n+k);
		if ((l+s)>0 && (l+s)<480) q=(l+s);  

		counter.state_value += array[p][q].state_value;
		}
	}

double z = ((counter.state_value+(array[n][l].state_value*3))/

(4LL+4*L+3));

array_to_evolve[n][l].state_value = (double)floorf((float)z+0.5);

/*evolution << counter.state_value << "  " << z << "  ";*/

srand(seed+(int)array_to_evolve[n][l].state_value);

array_to_evolve[n][l].R = (int)(((float)rand()/RAND_MAX)*255);
array_to_evolve[n][l].G = (int)(((float)rand()/RAND_MAX)*255);
array_to_evolve[n][l].B = (int)(((float)rand()/RAND_MAX)*255);

/*evolution << array_to_evolve[n][l].state_value << "    " << array[n]

[l].R << " " << array[n][l].B << " " << array[n][l].G << “\n\n”;*/

    }
}
     
/*evolution.close();*/

return array_to_evolve;

}

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
if ( SDL_LockSurface(screen) < 0 )
{
return;
}
}
}

void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
}

void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 )screen->pixels + yscreen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 )screen->pixels + yscreen->pitch/4 + x;
*bufp = color;
}
break;
}
}

void DrawScene(SDL_Surface *screen)
{
Slock(screen);

for(int x=0; x<640; x++)
{
for(int y=0; y<480; y++)
{

  int Red = (int)floorf(((float)rand()/RAND_MAX)*255);
  int Blue = (int)floorf(((float)rand()/RAND_MAX)*255);
  int Green = (int)floorf(((float)rand()/RAND_MAX)*255);
  
  DrawPixel(screen, x,y,(Uint8)Red,(Uint8)Green,(Uint8)Blue);
}

}

Sulock(screen);
SDL_Flip(screen);
}

int main(int argc, char *argv[]) {

pixel_state **evo_array, **evo_array_check;
int seed;

ofstream buffer ("Checks.txt");

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf(“Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}

seed = random_seed();

evo_array = initialize_species(100, seed);

evo_array = evolve_species(evo_array,2,seed);

int done=0;
while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
  if ( event.type == SDL_QUIT )  {  done = 1;  }

  if ( event.type == SDL_KEYDOWN )
  {
    if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
  }
}

DrawScene(screen);

}

buffer.close();

return 0;
}

Daniel wrote:

It seems that the “evolve_species()” function is making the prog go into
segmentation fault. I can’t understand why though! that function is at the
moment standalone and has nothing to do with SDL. I’m rebuilding the program
piece by piece trying to figure out which part of it is causing the problem,
but I can’t make any sense on why this function acts on SDL like this.

please help, thanks.

Daniel

I suggest you try the forums at GameDev.net. This really isn’t the
mailing list to get your code debugged for you, at least when the
problem dosn’t lie with using the SDL (even then I say it’s iffy).
They’re likely to also give you some pointers on how to debug your code
yourself, especially if you ask.

-Mike