malloc problems

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

malloc problems

 
0
  #1
Apr 28th, 2008
Hi everyone, i'm back with another problem that i cant seem to work out.

i'm trying to read in a .obj file which is basicaly a list of vertices and facets in this format

v 0.00 0.00 0.00
f 0 0 0

i dont know before hand how many vertices and facets the object has so i'm looping round once counting each occurance of v and f then looping around a second time and extracting the data.

once i know how many v's and f's there are i then malloc the correct amount of memory required to store them.

i close the file and reopen it to get back to the start of the file (not the best way i know)

i then read in the lines again and split them up using strtok to get each number and then i convert to float

this is the code i have so far

The problem is that my runs through fine the first time and prints out the ammount of v's and f's then crashes when doing the second loop and i cant seem to figure out why.

any help would be gratefully appreciated

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct
  5. {
  6. float **vertices;
  7. int **facets;
  8. }object;
  9.  
  10. object cube;
  11. int loadObject(object objectin);
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. loadObject(cube);
  16. getchar();
  17. }
  18. int loadObject(object objectin)
  19. {
  20. int i;
  21. unsigned int v = 0; //ammount of vertices
  22. unsigned int f = 0; //ammount of facets
  23. unsigned int vt = 0; //ammount of texture vertices
  24. unsigned int lncount = 0; //ammount of lines in the file
  25. unsigned int count = 0;
  26. float x = 0;
  27. float y = 0;
  28. float z = 0;
  29. char *pch; //character pointer
  30. float **vertices;
  31. int **facets;
  32. int n;
  33. int eof = 1;
  34.  
  35.  
  36. char line[100];
  37.  
  38.  
  39. FILE *infile;
  40.  
  41. infile = fopen("object.obj", "r"); //open file for reading
  42.  
  43. do
  44. {
  45. if (fgets(line, sizeof line,infile)== NULL)
  46. {
  47. eof = 0;
  48. }
  49. else
  50. {
  51. ++lncount;
  52. if(strncmp(line,"v",1)== 0)
  53. {
  54. ++v;
  55. }
  56. else if (strncmp(line, "f",1) == 0)
  57. {
  58. ++f;
  59. }
  60. else if (strncmp(line, "vt", 2) == 0)
  61. {
  62. ++vt;
  63. }
  64. }
  65.  
  66. }while (eof != 0);
  67. printf("f = %d v = %d",f,v);
  68.  
  69. vertices = malloc(v*5*sizeof(float));
  70.  
  71. v = 0;
  72.  
  73. fclose(infile);
  74. infile = fopen("object.obj", "r"); //open file for reading
  75.  
  76. for (i = 1; i <= lncount; ++i)
  77. {
  78. fgets(line, sizeof line,infile);
  79. if (strncmp(line,"v",1) == 0)
  80. {
  81. pch = strtok(line," v");
  82. count = 0;
  83. while (pch != NULL)
  84. {
  85. ++count;
  86. printf("%s\n",pch);
  87. switch (count)
  88. {
  89. case 1:
  90. x = atof(pch);
  91. break;
  92. case 2:
  93. y = atof(pch);
  94. break;
  95. case 3:
  96. z = atof(pch);
  97. break;
  98. }
  99. pch = strtok (NULL, " v");
  100. }
  101. vertices[v][0] = x;
  102. vertices[v][1] = y;
  103. vertices[v][2] = z;
  104. ++v;
  105. }
  106. }
  107.  
  108. for (i = 0; i < v; ++i)
  109. {
  110. printf("%f", vertices[i][0]);
  111. printf("%f", vertices[i][1]);
  112. printf("%f", vertices[i][2]);
  113. }
  114. }
Last edited by midimatt; Apr 28th, 2008 at 10:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: malloc problems

 
-1
  #2
Apr 29th, 2008
>>i close the file and reopen it to get back to the start of the file (not the best way i know)
Just call rewind() function or fseek()


>>if(strncmp(line,"v",1)== 0)
using strncmp() like that is too time consuming. Since the letter you want is at the beginning of the line just compare the first character
if( line[0] == 'v' )
The loop starting on line 43 is incorrect because it will read the last line too many times. It should be coded like this:
  1. while( fgets(line, sizeof(line), infile) )
  2. {
  3.  
  4. }

>>vertices = malloc(v*5*sizeof(float));
You can't allocate vertices like that because vertices is a 2d array and you are only allocating the first dimension. There are a couple ways you can do this, but if you want to keep the 2d syntax as shown on lines 101-103 then you have to allocate each line separately
  1. int i = 0;
  2. // allocate v number of rows
  3. vertices = malloc( v * sizeof(vertices *));
  4. for(i = 0; i < v; i++)
  5. {
  6. vertices[i] = malloc(5 * sizeof(float));
  7. }

The other way to handle that is to make vertices a single dimension array, but the math on lines 101-103 becomes more complicated.
  1. char *vertices;
  2.  
  3. vertices = malloc(v*5*sizeof(float));
  4.  
  5. ...
  6. vertices[v*4+0] = x;
  7. vertices[v*4+1] = y;
  8. vertices[v*4+2] = z;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: malloc problems

 
0
  #3
May 3rd, 2008
considering line 80 to 104,
You alredy have the data in line & you know exactly what format the data is. So, why dont you use sscanf().

And for the malloc problem

1) You may follow what dragon said & loop twice over the file
2) use a linked list. That would make you change a lot of code
3) or Use realloc, but be carefull though, cause you are using a pointer to a pointer to a float

The first looks better to me.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC