[GRASS-dev] Segmentation Fault in addition to r.walk code

Colin Nielsen colin.nielsen at gmail.com
Wed Jun 18 07:14:03 EDT 2008


My first post to this list, so go easy.

I've been playing with the r.walk code for my own purposes. I've been
trying to add functionality for a second output file. Basically I
copied everything done to the original output file, but with different
file names. The odd thing is, that I get a seg fault on
		segment_put(&out_seg2, &fbuff[i], row, i);   when above it worked
fine for the original &out_seg. The relevent bits of code are below
(though tell me if I should post the whole thing):

    void *dtm_cell, *cost_cell, *cum_cell, *dir_cell, *cell2 = NULL;
//cn: added dir_cell
    SEGMENT dtm_in_seg, cost_in_seg, out_seg, out_seg2; //cn: added
out_seg2 for move dir
    char *cum_cost_mapset, *move_dir_mapset; //cn: added move_dir_mapset
    char *dtm_in_file, *cost_in_file, *out_file, *dir_out_file; //cn:
added dir_out_file
    double *dtm_value, *cost_value, *value_start_pt, *dir_value; //cn:
and dir_value
    double cur_dir, old_cur_dir; //cn: added cur_dir and old_cur_dir
    int cost_fd, cum_fd, dtm_fd, dir_fd; //cn:added dir_fd
    int dtm_in_fd, cost_in_fd, out_fd, dir_out_fd; //cn: added dir_out_fd
    struct Option *opt9, *opt10, *opt11, *opt12, *opt13, *opt14,
*opt15; //cn: added *opt15

...
    /*CN: added the opt for outputing the raster of movement directions*/
    opt15 = G_define_option();
    opt15->key = "outdir";
    opt15->type = TYPE_STRING;
    opt15->required = YES;
    opt15->gisprompt = "new,cell,raster";
    opt15->description = _("Name of output raster map to contain
movement directions");

...
    strcpy(cum_cost_layer, opt1->answer);
    current_mapset = G_mapset();

    /*  Search for output layer (cum_cost_layer) in all mapsets */

    search_mapset = "";
    cum_cost_mapset = G_find_cell2(cum_cost_layer, search_mapset);

    /*CN: Copy the specified filename to a new file*/
    strcpy(move_dir_layer, opt15->answer);

    /*CN: Search for output layer (move_dir_layer) in mapsets*/
    search_mapset = "";
    move_dir_mapset = G_find_cell2(move_dir_layer, search_mapset);
...

    if (G_legal_filename(cum_cost_layer) < 0)
	G_fatal_error(_("<%s> is an illegal file name"), cum_cost_layer);
	
	/*CN: check if layer name is legal*/
	if (G_legal_filename(move_dir_layer) < 0)
	G_fatal_error(_("<%s> is an illegal file name"), move_dir_layer);
...

    out_fd = creat(out_file, 0600);
    segment_format(out_fd, nrows, ncols, srows, scols, sizeof(double));
    close(out_fd);

    /*cn: create a seg file for dir output*/
    dir_out_fd = creat(dir_out_file, 0600);
    segment_format(dir_out_fd, nrows, ncols, srows, scols, sizeof(double));
    close(dir_out_fd);
...

    out_fd = open(out_file, 2);
    segment_init(&out_seg, out_fd, segments_in_memory);

    /*cn: and the dir output file*/
    dir_out_fd = open(dir_out_file, 2);
    segment_init(&out_seg2, dir_out_fd, segments_in_memory);

...

    /* Initialize output map with NULL VALUES */

    /*   Initialize segmented output file  */

	G_message(_("Initializing output "));
    {
	double *fbuff;
	int i;

	fbuff = (double *)G_malloc((unsigned int)(ncols * sizeof(double)));

	if (fbuff == NULL)
	    G_fatal_error(_("Unable to allocate memory for segment fbuff == NULL"));

	G_set_d_null_value(fbuff, ncols);

	for (row = 0; row < nrows; row++) {
	     {
		G_percent(row, nrows, 2);
	    }
	    for (i = 0; i < ncols; i++) {
		segment_put(&out_seg, &fbuff[i], row, i);
	    }

	}
	segment_flush(&out_seg);
	
	    G_percent(row, nrows, 2);
	G_free(fbuff);
    }

    /*cn:init segmented dir output file */
    G_message(_("Initializing directional output "));
    {
	double *fbuff;
	G_message(_("*fbuff"));
	int i;

	fbuff = (double *)G_malloc((unsigned int)(ncols * sizeof(double)));
	G_message(_("fbuff"));

	if (fbuff == NULL)
	    G_fatal_error(_("Unable to allocate memory for segment fbuff == NULL"));

	G_set_d_null_value(fbuff, ncols);
	G_message(_("set_d_null"));

	for (row = 0; row < nrows; row++) {
	     {
		G_percent(row, nrows, 2);
	    }
	    for (i = 0; i < ncols; i++) {
	    	G_message(_("almost seg_put <%d>"), &fbuff[i]);
		segment_put(&out_seg2, &fbuff[i], row, i);
		G_message(_("done seg_put"));
	    }

	}
	G_message(_("almost seg_flush"));
	segment_flush(&out_seg2);
	G_message(_("done seg_flush"));
	
	    G_percent(row, nrows, 2);
	G_free(fbuff);
	G_message(_("G_free"));
    }



It compiles fine but when I run it, I get a segmentation fault at
segment_put(&out_seg2,....

Thanks, I hope someone can help.

-CN


More information about the grass-dev mailing list