0
tPeca* criarPecas(FILE *pFile, int tam){
    int i = 0,linhaV,colunaV,j = 0;
    char ***elemento = (char***)malloc(tam*sizeof(char**));; 
    tPeca *pecaJogo = (tPeca*)malloc(tam*sizeof(tPeca));
    if(pecaJogo==NULL)
        return NULL;
    for(i=0;i<tam;i++){
            j=0;
        fscanf (pFile, "%[^;]", pecaJogo[i].nome);  
        fscanf (pFile, ";%d", &pecaJogo[i].qtd);    
        fscanf (pFile, ";%d", &linhaV);
                pecaJogo[i].linha = linhaV;
        fscanf (pFile, ";%d", &colunaV);
                pecaJogo[i].coluna = colunaV;
        **elemento[i] = (char**)malloc(linhaV * sizeof(char*));
        *elemento[i][j] = (char*)malloc(colunaV * sizeof(char));
        j++;
    }
    return pecaJogo;
}

*** elemento is a pointer of a matriz, i think that i have problem with malloc... I received Segmentation Fault

Rhuan Caetano
  • 285
  • 3
  • 9
  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](http://stackoverflow.com/questions/how-to-ask) – Eregrith Jun 17 '15 at 13:56
  • 3
    Also, standard warning: [Do not cast the result of malloc](http://stackoverflow.com/q/605845/1151654) – Eregrith Jun 17 '15 at 13:57
  • I'm curious as to why you're checking `pecaJogo` for `NULL`, but not `elemento`. And it looks like you have at least one too many levels of pointer redirection going on. A 1D matrix (array) needs one, a 2D matrix needs two, etc. – lurker Jun 17 '15 at 14:36

1 Answers1

1

These two statements are where I guess you ran into your problem:

**elemento[i] = (char**)malloc(linhaV * sizeof(char*));
*elemento[i][j] = (char*)malloc(colunaV * sizeof(char));  

You created char *** above, and attempted to create an array of pointers:

char ***elemento = (char***)malloc(tam*sizeof(char**));;

Should be:

//this step creates an array of pointers
char ***elemento = malloc(tam*sizeof(char*));
//Note: there is no need to cast the return of [m][c][re]alloc in C  
//      The rules are different in C++ however.

Now you can put elemento in a loop to allocate pointer space for each of the pointers you created:

//this step creates an array pointers for each element of the array created above: 
for(i=0;i<tam;i++) //assuming size is also tam (you specified nothing else)
{
     elemento[i] = malloc(tam*sizeof(char *));//each ith element will 
                                              //now have tam elements of its own.
} 

Next, you allocate memory at each location:

for(i=0;i<tam;i++)
{
    for(j=0;j<tam;j++)
    {
        elemento[i][j] = malloc(someValue*sizeof(char));
        //Note: sizeof(char) == 1, so could be:
        //elemento[i][j] = malloc(someValue);
    }
}

Now you have a fully allocated 3D array.

Putting it all together, (A simple 2D example)
When you create memory for a multi-dimensional array, you must create a combination of array of pointers, and memory for each. For 2D example, (used for an array of strings perhaps) you could do this:

char ** allocMemory(char ** a, int numStrings, int maxStrLen)
{
    int i;
    a = calloc(sizeof(char*)*(numStrings), sizeof(char*));//create array of pointers
    for(i=0;i<numStrings; i++)
    {
      a[i] = calloc(sizeof(char)*maxStrLen + 1, sizeof(char));//create memory at each location
    }
    return a;
}

You must also create method to free memory:

void freeMemory(char ** a, int numStrings)
{
    int i;
    for(i=0;i<numStrings; i++)
        if(a[i]) free(a[i]);
    free(a);
}

Usage:

char **array = {0};
...
array = allocMemory(array, 10, 80);
... 
freeMemory(array, 10);

Will create memory, and addresses sufficient to contain 10 arrays of 80 character strings (arrays of char), then free it.

This could be expanded to 3D by adding another layer (for loop) of pointer creation, as shown at top of post). In this implementation, the inner most loop always creates the actual memory for each of the address locations you create.

ryyker
  • 22,849
  • 3
  • 43
  • 87