0

Such question has been asked earlier but i am not able to get it to work. I have an array of structs and i am copying data from that array to other and passing it to a function. So the data in that array can be modified but when i run i get a seg fault and by gdb it is pointed to point where the function is called. Here is my code: The Struct

typedef struct tex{
       vertex3f pos;
       vertex2f texCoord;
       vertex3f normal;
    }vertex;

The making an array of structs

passVert = (vertex*)malloc(sizeof(verticesPos[4]));
memcpy(passVert,verticesPos,sizeof(verticesPos[4]));
calcNormals(indices,indexCount,&(passVert),vertexCount); //here the gdb shows the fault

The function:

void calcNormals(const unsigned int* pIndices,unsigned int indexCount,vertex **pVertices, unsigned int vertexCount){
   for(unsigned int i = 0; i < indexCount; i += 3){
      unsigned int index0 = pIndices[i];
      unsigned int index1 = pIndices[i + 1];
      unsigned int index2 = pIndices[i + 2];
      vertex3f v1; 
      subVertices(&v1,pVertices[index1]->pos, pVertices[index0]->pos);
      vertex3f v2;
      subVertices(&v2,pVertices[index2]->pos, pVertices[index0]->pos);
      vertex3f normal;
      crossProduct(&normal,v1,v2);
      normalize(&normal,normal);
      addVertices(&(pVertices[index0]->normal),pVertices[index0]->normal,normal);
      addVertices(&(pVertices[index1]->normal),pVertices[index0]->normal,normal);
      addVertices(&(pVertices[index2]->normal),pVertices[index0]->normal,normal);
   }
   for(unsigned int i = 0; i < vertexCount; i++){
      normalize(&(pVertices[i]->normal),pVertices[i]->normal);
   }
}

PS: I am learning opengl concepts, so i used the guide by ogldev and it resembles it. I am doing it in plain C.

EDIT:

vertex* passVert;

vertex verticesPos[4];
     makeCoords(&verticesPos[0],-1.0f, -1.0f, 0.5773f,0.0f,0.0f);
     makeCoords(&verticesPos[1],0.0f, -1.0f, -1.15475f,0.5f,0.0f);
     makeCoords(&verticesPos[2],1.0f, -1.0f, 0.5773f,1.0f,0.0f);
     makeCoords(&verticesPos[3],0.0f,1.0f,0.0f,0.5f,1.0f);

the makeCoords function just initialize the verticesPos to the parameters passed, it only initializes pos and texCoord member of vertex struct and:

typedef struct vertices3{
    float x;
    float y;
    float z;
}vertex3f;

typedef struct vertex2f{
   float x;
   float y;
}vertex2f;

EDIT:

Now i check to print out all the four elements it was supposed to copy in passVert i did something like this instead of calling calcNormals

for(int i = 0; i < 4; ++i){
           printf("%f %f %f %f %f\n\n",passVert[i].pos.x,passVert[i].pos.y,passVert[i].pos.z,passVert[i].texCoord.x,passVert[i].texCoord.y);
        }

and it printed the data which was in verticesPos which it was supposed to copy. It printed out:

-1.000000 -1.000000 0.577300 0.000000 0.000000

0.000000 -1.000000 -1.154750 0.500000 0.000000

1.000000 -1.000000 0.577300 1.000000 0.000000

0.000000 1.000000 0.000000 0.500000 1.000000

which i wanted it to copy. The number of indices which is being passed is an array which is unsigned int indices[] = {0,3,1,1,3,2,2,3,0,0,1,2}; and count of 12 is being passed for indexCount and 4 is being passed for vertexCount in calcNormals function

  • Try to remove `[4]` from each `sizeof` operation. – Sam Protsenko Mar 05 '15 at 19:33
  • First [don't cast the result of `malloc()`](http://stackoverflow.com/a/605858/1983495). Then please post what `verticesPos` is, how it is declared, and you must check that `malloc()` didn't return `NULL` before passing the pointer to `memcpy()`. – Iharob Al Asimi Mar 05 '15 at 19:33
  • 1
    How are `passVert` and `verticesPos` defined? – Anto Jurković Mar 05 '15 at 19:35
  • 1
    `(vertex*)malloc(sizeof(verticesPos[4]));` reveals a lot of problem. why you allocate memory for `verticesPos[4]` but you cast it into `vertex*`? – Jason Hu Mar 05 '15 at 19:35
  • @iharob: it seems like he's just allocating memory only for 1 element of array and then tries to work with the whole array, getting seg fault when trying to access memory that doesn't belong to his process. – Sam Protsenko Mar 05 '15 at 19:36
  • i guess `verticesPos` is a `vertex` array? and sizeof it returns `sizeof vertex`? but what `memcpy(passVert,verticesPos,sizeof(verticesPos[4]));` means then. – Jason Hu Mar 05 '15 at 19:36
  • @SamProtsenko it doesnt work – user3734435 Mar 05 '15 at 19:40
  • @AntoJurković vertex verticesPos[4], vertex* passVert – user3734435 Mar 05 '15 at 19:42
  • @HuStmpHrrr but i tried to print out the data in passVert instead of calling the calcNormals function, it printed correct data. – user3734435 Mar 05 '15 at 19:45
  • @user3734435 you mean the content in `verticesPos` and `passVert` compares equal? then it just means there are more bugs underneath. – Jason Hu Mar 05 '15 at 19:50
  • @HuStmpHrrr i have just added a edit, see it please if u can help – user3734435 Mar 05 '15 at 20:03
  • @iharob i checked it is not returning null before passing it to memcpy, i have also added the details u were asking for. – user3734435 Mar 05 '15 at 20:06
  • This: `passVert = (vertex*)malloc(sizeof(verticesPos[4]));` looks very much not at all like allocating memory for an array. Perhaps it should be more along the lines of `passVert = malloc(4 * sizeof(vertex));` (or, slightly better, assuming a definition like `vertex *verticesPos;`, `passVert = malloc(4 * sizeof(*verticesPos));`. – twalberg Mar 05 '15 at 20:44
  • @twalberg i tried it changing it to `passVert = malloc(4 * sizeof(vertex));` and also the memcpy parameter but still getting seg fault and gdb is still pointing at calcNormals – user3734435 Mar 06 '15 at 07:29

0 Answers0