0

Hello,
I have a wiered error. My code works if my struct has less then six floats but not otherwise. If I have more then six floats it freezes the system. See code for better description:

/*
 *
 * SSI2.cpp
 *
 * Data Description:
 *
 *    The data consists of a series of runs, one run per file. A data
 *    run consists of a number of bands, where the minimum number of
 *    bands is 3 and the maximum envisioned is currently thirty but
 *    could grow. Currently only 3 bands are supported. Each band consists 
 *    of three traces.  Each trace consists of a circular scan of a measured 
 *    height for a given angular position.  Thus the three traces map alpha, beta, 
 *    gamma in conventional polar notation, the height varibale represents the difference
 *    of height of this point to radius (1000nm). Phi represents phi and theta represents 
 *	  theta.  The set of bands represents incremental
 *    scans where the middle band (if the number of bands is odd) or the
 *    middle 2 bands (if the number of bands is even) represent the
 *    equatorial scan(s) for the run.
 *
 *    The data are represented by rows of comma-delimited ASCII integers 
 *    which can be positive or negative.  Each row represents one angular
 *    position for all the bands.  The first row of the file is an ASCII
 *    comma-delimited description of the data.  Each descriptor is of the
 *    form 'Ya-b', where a is the trace identifier (0, 1 or 2), and b
 *    is the band identifier, starting with 0. Trace identifier is not used.
 *     
 */
 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define MAXSTRINGLENGTH 80
#define OK  0
#define ERR 1

#ifndef PI
#define PI 3.141592654
#endif

/*
 * The row oriented data, for each band
 */


/*
************************ !ERROR! *********************
IF I COMMENT ANY OF THE FLOATS BELOW OUT THE CODE WORKS
OTHERWISE IT FREEZES MY COMPUTER (Mac G3, OS 9.2, Codewarrior 7.0 Gold)
*/
struct banddatum {
                    //alpha
                    int    alpha_height;
                    float  alpha_theta;
                    float  alpha_phi;
                    //beta
                    int    beta_height;
                    float  beta_theta;
                    float  beta_phi;
                    //gamma
                    int    gamma_height;
                    float  gamma_theta;
                 	float  gamma_phi;
                 };
/*
 * The data associated with one "band"
 */
struct databand  {
                     int    bandid;                    
                     int    num_pts;                   
                     struct banddatum *bdata;          /* Pointer to array of band datums  */
                 };
/*
 * The data associated with a run
 */
struct datarun   {
                     FILE            *infile;
                     char             descriptor[MAXSTRINGLENGTH];
                     int              numfields;
                     int              numbands;
                     int              numrows;
                     int              equator_1;
                     int              equator_2;
                     struct databand **band;          /* Pointer to array of Band Data     */
                 };

char* strdup (char* string)
{
	int size = strlen (string);
    char * copy = (char*)calloc((size), sizeof(char));
  	memcpy (copy,string, size);
 	return copy;
}

int
GetNumFields(char line[], char delimiters[])
{
    char *fieldptr;
    char *mycopy;
    int   fieldcount = 0;

    if (strlen(line) == 0)
    {
        return 0;
    }
    mycopy = strdup(line);
    fieldptr = strtok(mycopy, delimiters);
    while (fieldptr != NULL)
    {
        fieldptr = strtok(NULL, delimiters);
        ++fieldcount;
    }
    free(mycopy);
    if (line[strlen(line) - 2] == ',')  /* some example files had traling commas*/
    {
        --fieldcount;
    }
    return fieldcount;
}

int
GetRunAttributes(char infilename[], struct datarun *run)
{
    char mystring[MAXSTRINGLENGTH];
    int  linecount = 0;

    run->infile = fopen(infilename,"r");
    if (run->infile == NULL)
    {
        fprintf(stderr,"Can't open data file named: %s\n",infilename);
        return ERR;
    }
    while(feof(run->infile) == OK)
    {
        fgets(mystring, MAXSTRINGLENGTH, run->infile);
        ++linecount;
    }
    run->numrows = linecount - 2;

    rewind(run->infile);
    fgets(run->descriptor, MAXSTRINGLENGTH, run->infile);
    run->numfields = GetNumFields(run->descriptor,"', ");
    run->numbands = run->numfields / 3;

    if ((run->numbands % 2) == 1)
    {
        run->equator_1 = run->equator_2 = (run->numbands / 2);
    }
    else
    {
        run->equator_1 = (run->numbands / 2) - 1;
        run->equator_2 = run->equator_1 + 1;
    }
    return OK;
}


int
SetupDynamicData(struct datarun *run)
{
  
    printf("In Setup Dynamic Data\n"); 

    /*
     * A run has an array of bands
     */
    run->band = (struct databand **)calloc(run->numbands, sizeof(struct databand *));
    for (int i = 0; i < run->numbands; ++i)
    {
        /*
         * Each band has an array of datum (data) - heights, and angles
         */
        run->band[i] = (struct databand *)calloc(1, sizeof(struct databand));
        run->band[i]->num_pts = run->numrows;
        run->band[i]->bdata   = (struct banddatum *)calloc(run->numrows, sizeof(struct banddatum));

    }

         
    printf("Allocated all dynamic data, and assigned computable values\n");
    getchar();

    return OK;
}

void
FreeDynamicData(struct datarun *run)
{
    for (int i = 0; i < run->numbands; ++i)
    {
        if (run->band[i] != NULL)
        {
            if (run->band[i]->bdata != NULL)
            {
                free(run->band[i]->bdata);
            }
            free(run->band[i]);
        }
    }
}

int
LoadHeightData(struct datarun *run)
{
    char delimiters[] = ", ";
    char mystring[MAXSTRINGLENGTH];
    char *fieldptr;

    float angle = 0.0;
    float angle_increment;

    angle_increment = 360.0 / run->numrows;

    for (int i=0; i < run->numrows; ++i)
    {
        fgets(mystring, sizeof(mystring), run->infile);
        fieldptr = strtok(mystring,delimiters);
        for (int j=0; j<run->numbands; ++j)
        {

             run->band[j]->bdata[i].alpha_height = atoi(fieldptr);
             fieldptr = strtok(NULL,delimiters);
             run->band[j]->bdata[i].beta_height = atoi(fieldptr);
             fieldptr = strtok(NULL,delimiters);
             run->band[j]->bdata[i].gamma_height = atoi(fieldptr);    
             fieldptr = strtok(NULL,delimiters);
  }
        angle += angle_increment;
    }
    return OK;
}

int 
main(int argc, char argv[])
{
    struct datarun run;
    char           infile_name[] = "Ilya-tipical.csv";

    if (GetRunAttributes(infile_name, &run) == ERR)
    {
        fprintf(stderr, "Couldn't set up Data Run...quitting\n");
        return 1;
    }
    if (SetupDynamicData(&run) == ERR)
    {
        fprintf(stderr, "Couldn't Allocate Dynamic Data...quitting\n");
        return 1;
    }
    if (LoadHeightData(&run) == ERR)
    {
        fprintf(stderr, "Couldn't Load Height Data....quitting\n");
        FreeDynamicData(&run);
        return 1;
    }
    /*
     * etc...
     */
    FreeDynamicData(&run);
    printf ("\nDone");
    return 0;
}

#undef MAXSTRINGLENGTH
#undef ERR
#undef OK

Ilya
P.S.: Please help

1
Contributor
1
Reply
2
Views
14 Years
Discussion Span
Last Post by Valmian
0

well I found my problem. My stupid mac (I am really a win and linux user) was not allowing my program to use enough memory (it didn't give more then 324KB and it needed more then a meg). There is no error in the actual code. :)
Ilya

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.