943,605 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3900
  • C RSS
Oct 9th, 2003
0

struct error (!?)

Expand Post »
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:
  1.  
  2. /*
  3.  *
  4.  * SSI2.cpp
  5.  *
  6.  * Data Description:
  7.  *
  8.  * The data consists of a series of runs, one run per file. A data
  9.  * run consists of a number of bands, where the minimum number of
  10.  * bands is 3 and the maximum envisioned is currently thirty but
  11.  * could grow. Currently only 3 bands are supported. Each band consists
  12.  * of three traces. Each trace consists of a circular scan of a measured
  13.  * height for a given angular position. Thus the three traces map alpha, beta,
  14.  * gamma in conventional polar notation, the height varibale represents the difference
  15.  * of height of this point to radius (1000nm). Phi represents phi and theta represents
  16.  * theta. The set of bands represents incremental
  17.  * scans where the middle band (if the number of bands is odd) or the
  18.  * middle 2 bands (if the number of bands is even) represent the
  19.  * equatorial scan(s) for the run.
  20.  *
  21.  * The data are represented by rows of comma-delimited ASCII integers
  22.  * which can be positive or negative. Each row represents one angular
  23.  * position for all the bands. The first row of the file is an ASCII
  24.  * comma-delimited description of the data. Each descriptor is of the
  25.  * form 'Ya-b', where a is the trace identifier (0, 1 or 2), and b
  26.  * is the band identifier, starting with 0. Trace identifier is not used.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <math.h>
  33. #include <stdlib.h>
  34.  
  35. #define MAXSTRINGLENGTH 80
  36. #define OK 0
  37. #define ERR 1
  38.  
  39. #ifndef PI
  40. #define PI 3.141592654
  41. #endif
  42.  
  43. /*
  44.  * The row oriented data, for each band
  45.  */
  46.  
  47.  
  48. /*
  49. ************************ !ERROR! *********************
  50. IF I COMMENT ANY OF THE FLOATS BELOW OUT THE CODE WORKS
  51. OTHERWISE IT FREEZES MY COMPUTER (Mac G3, OS 9.2, Codewarrior 7.0 Gold)
  52. */
  53. struct banddatum {
  54. //alpha
  55. int alpha_height;
  56. float alpha_theta;
  57. float alpha_phi;
  58. //beta
  59. int beta_height;
  60. float beta_theta;
  61. float beta_phi;
  62. //gamma
  63. int gamma_height;
  64. float gamma_theta;
  65. float gamma_phi;
  66. };
  67. /*
  68.  * The data associated with one "band"
  69.  */
  70. struct databand {
  71. int bandid;
  72. int num_pts;
  73. struct banddatum *bdata; /* Pointer to array of band datums */
  74. };
  75. /*
  76.  * The data associated with a run
  77.  */
  78. struct datarun {
  79. FILE *infile;
  80. char descriptor[MAXSTRINGLENGTH];
  81. int numfields;
  82. int numbands;
  83. int numrows;
  84. int equator_1;
  85. int equator_2;
  86. struct databand **band; /* Pointer to array of Band Data */
  87. };
  88.  
  89. char* strdup (char* string)
  90. {
  91. int size = strlen (string);
  92. char * copy = (char*)calloc((size), sizeof(char));
  93. memcpy (copy,string, size);
  94. return copy;
  95. }
  96.  
  97. int
  98. GetNumFields(char line[], char delimiters[])
  99. {
  100. char *fieldptr;
  101. char *mycopy;
  102. int fieldcount = 0;
  103.  
  104. if (strlen(line) == 0)
  105. {
  106. return 0;
  107. }
  108. mycopy = strdup(line);
  109. fieldptr = strtok(mycopy, delimiters);
  110. while (fieldptr != NULL)
  111. {
  112. fieldptr = strtok(NULL, delimiters);
  113. ++fieldcount;
  114. }
  115. free(mycopy);
  116. if (line[strlen(line) - 2] == ',') /* some example files had traling commas*/
  117. {
  118. --fieldcount;
  119. }
  120. return fieldcount;
  121. }
  122.  
  123. int
  124. GetRunAttributes(char infilename[], struct datarun *run)
  125. {
  126. char mystring[MAXSTRINGLENGTH];
  127. int linecount = 0;
  128.  
  129. run->infile = fopen(infilename,"r");
  130. if (run->infile == NULL)
  131. {
  132. fprintf(stderr,"Can't open data file named: %s\n",infilename);
  133. return ERR;
  134. }
  135. while(feof(run->infile) == OK)
  136. {
  137. fgets(mystring, MAXSTRINGLENGTH, run->infile);
  138. ++linecount;
  139. }
  140. run->numrows = linecount - 2;
  141.  
  142. rewind(run->infile);
  143. fgets(run->descriptor, MAXSTRINGLENGTH, run->infile);
  144. run->numfields = GetNumFields(run->descriptor,"', ");
  145. run->numbands = run->numfields / 3;
  146.  
  147. if ((run->numbands % 2) == 1)
  148. {
  149. run->equator_1 = run->equator_2 = (run->numbands / 2);
  150. }
  151. else
  152. {
  153. run->equator_1 = (run->numbands / 2) - 1;
  154. run->equator_2 = run->equator_1 + 1;
  155. }
  156. return OK;
  157. }
  158.  
  159.  
  160. int
  161. SetupDynamicData(struct datarun *run)
  162. {
  163.  
  164. printf("In Setup Dynamic Data\n");
  165.  
  166. /*
  167.   * A run has an array of bands
  168.   */
  169. run->band = (struct databand **)calloc(run->numbands, sizeof(struct databand *));
  170. for (int i = 0; i < run->numbands; ++i)
  171. {
  172. /*
  173.   * Each band has an array of datum (data) - heights, and angles
  174.   */
  175. run->band[i] = (struct databand *)calloc(1, sizeof(struct databand));
  176. run->band[i]->num_pts = run->numrows;
  177. run->band[i]->bdata = (struct banddatum *)calloc(run->numrows, sizeof(struct banddatum));
  178.  
  179. }
  180.  
  181.  
  182. printf("Allocated all dynamic data, and assigned computable values\n");
  183. getchar();
  184.  
  185. return OK;
  186. }
  187.  
  188. void
  189. FreeDynamicData(struct datarun *run)
  190. {
  191. for (int i = 0; i < run->numbands; ++i)
  192. {
  193. if (run->band[i] != NULL)
  194. {
  195. if (run->band[i]->bdata != NULL)
  196. {
  197. free(run->band[i]->bdata);
  198. }
  199. free(run->band[i]);
  200. }
  201. }
  202. }
  203.  
  204. int
  205. LoadHeightData(struct datarun *run)
  206. {
  207. char delimiters[] = ", ";
  208. char mystring[MAXSTRINGLENGTH];
  209. char *fieldptr;
  210.  
  211. float angle = 0.0;
  212. float angle_increment;
  213.  
  214. angle_increment = 360.0 / run->numrows;
  215.  
  216. for (int i=0; i < run->numrows; ++i)
  217. {
  218. fgets(mystring, sizeof(mystring), run->infile);
  219. fieldptr = strtok(mystring,delimiters);
  220. for (int j=0; j<run->numbands; ++j)
  221. {
  222.  
  223. run->band[j]->bdata[i].alpha_height = atoi(fieldptr);
  224. fieldptr = strtok(NULL,delimiters);
  225. run->band[j]->bdata[i].beta_height = atoi(fieldptr);
  226. fieldptr = strtok(NULL,delimiters);
  227. run->band[j]->bdata[i].gamma_height = atoi(fieldptr);
  228. fieldptr = strtok(NULL,delimiters);
  229. }
  230. angle += angle_increment;
  231. }
  232. return OK;
  233. }
  234.  
  235. int
  236. main(int argc, char argv[])
  237. {
  238. struct datarun run;
  239. char infile_name[] = "Ilya-tipical.csv";
  240.  
  241. if (GetRunAttributes(infile_name, &run) == ERR)
  242. {
  243. fprintf(stderr, "Couldn't set up Data Run...quitting\n");
  244. return 1;
  245. }
  246. if (SetupDynamicData(&run) == ERR)
  247. {
  248. fprintf(stderr, "Couldn't Allocate Dynamic Data...quitting\n");
  249. return 1;
  250. }
  251. if (LoadHeightData(&run) == ERR)
  252. {
  253. fprintf(stderr, "Couldn't Load Height Data....quitting\n");
  254. FreeDynamicData(&run);
  255. return 1;
  256. }
  257. /*
  258.   * etc...
  259.   */
  260. FreeDynamicData(&run);
  261. printf ("\nDone");
  262. return 0;
  263. }
  264.  
  265. #undef MAXSTRINGLENGTH
  266. #undef ERR
  267. #undef OK
Ilya
P.S.: Please help
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
Valmian is offline Offline
82 posts
since Sep 2003
Oct 9th, 2003
0

Re: struct error (!?)

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
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
Valmian is offline Offline
82 posts
since Sep 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Please help me out, need help
Next Thread in C Forum Timeline: compiling error wtf!?!?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC