I still need to be checking for NULL columns. I need to set columns with nothing in any of the rows to point to null... or my code won't work. So yes it is giving me a problem.. I will post the rest of the code...
Originally Posted by
anduril462
Okay, so I wrote a wrapper around your rotate function to make sure it works. Compile and run the following for me and make sure this doesn't seg fault on your system. Admittedly, the rotation is a little strange, but no seg fault.
The only thing I noticed in there that was weird is your call to free(arena). That's the only other thing in there that seems suspicious. How is arena declared? If it's a regular 2-d array, then you can't free it. If you do it with a sequence of mallocs, similar to how you allocate newarena, then you aren't freeing all the memory. You have to free each row with free(arena[i]), then call free(arena). Has anything else in your code changed? How do you know it seg faults when you call rotate? Can I get a full listing to see if the problem actually lies elsewhere?
Here is the main prog:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "life.h"
#include "load.h"
#include "rotate.h"
#include "evolve.h"
#define MAX_CYCLES 100
#define ROTATE 20
/* Display the arena in a vt100 (xterm) terminal with the given width and height.
*/
static void
show(char **arena, int width, int height)
{
int x, y;
printf("\033[2J");
for (x = 0; x < width; x++)
if (arena[x] != NULL)
for (y = 0; y < height; y++) {
if (arena[x][y] & 1)
printf("\033[%d;%dH*", y, x);
}
printf("\033[H");
fflush(stdout);
}
int main(int argc, char **argv)
{
char **arena;
int width = WIDTH, height = HEIGHT;
int cycle;
if (argc != 2 && argc != 4) {
fprintf(stderr,"usage is %s filename [ width height ]\n",argv[0]);
exit(1);
}
if (argc == 4) {
width = atoi(argv[2]);
height = atoi(argv[3]);
if (width < MINSIZE || width > MAXSIZE || height < MINSIZE || height > MAXSIZE) {
fprintf(stderr, "%s: width and height must be in the range %d to %d\n",
argv[0], MINSIZE, MAXSIZE);
exit(1);
}
}
if ((arena = load(argv[1], width, height)) == NULL) {
fprintf(stderr,"%s: Cannot open %s\n",argv[0],argv[1]);
exit(1);
}
for (cycle = 1; cycle <= MAX_CYCLES; cycle++) {
show(arena, width, height);
evolve(arena, width, height);
if (cycle % ROTATE == 0)
arena = rotate(arena, &width, &height);
/* Add a 1/10 sec delay so that we can see what is happening.
*/
usleep(100000);
}
int x;
for (x = 0; x < width; x++)
if (arena[x] != NULL)
free(arena[x]);
free(arena);
return 0;
}
and there is nothing implemented in evolve yet so...