1

I'm trying to make a dynamic matrix that is based on an integer pointer to pointer, this variable is allocated dynamically, but I'm having a little trouble using it, I don't know if I am using it wrong or something, but the outcome is not as expected. Is it something wrong that I'm doing or am I not thinking this through?

void pkn(int n, int k)
{
   int chec = checagemInicial(n,k);
   if(chec == 1)
   {
       return;
   }

   int mat[n];
   int **resultado = NULL, **newMem;
   int posicoes = 0;
   initiateArray(mat, n);//initialize the matrix with one's


   while(functionthatchecksthings1(mat, n, k) == 0)//this 2 function works no need to worry
   {
       if(functionthatchecksthings2(mat, n , k))
       {//i think the problem might be or here
          newMem = realloc(resultado, ((sizeof(int)*n)+(posicoes*sizeof(int)*n)));
          if(newMem)
          {//or here
                resultado = newMem;  
          } 

          int i = 0;
          for(i; i<n;i++)
          {//or here but don't know where
              resultado[posicoes][i] = mat[i];
          }
          posicoes++;
       }

       somaArray(mat, k, n-1);
   }
   //or in the very least case i'm printing it wrong
   printaArray(resultado, n, posicoes);

   system("pause");

   free_matrix(posicoes, resultado);    
}

This one is the function that prints the pointer, it might be wrong here too, but I seriously don't know.

void printaArray(int **ar, int n, int p)
{
    int i = 0, j;
    for(i; i < p; i++)
    {
        j = 0;
        for(j; j < n; j++)
        {
            printf("%d ",(ar[i][j]));
        }
        printf("\n");
    }

    printf("%d\n", p);
}

I would like to thank everyone and I'm sorry if I haven't made myself clear, or if I've said something wrong/misspelled anything

Troianos77
  • 53
  • 8
  • Does this example help? https://stackoverflow.com/questions/32392837/how-to-point-on-2d-3d-space-on-a-pre-allocated-memory/32392951#32392951 – Davislor Sep 09 '15 at 00:56
  • 1
    Why are you using realloc on this line? `newMem = realloc(resultado, ((sizeof(int)*n)+(posicoes*sizeof(int)*n)));` You didn't even allocate`resultado` before this line. – Lucas Piske Sep 09 '15 at 01:03
  • 1
    If I'm not mistaken realloc, allocs memory space, almost like malloc if the first parameter was not allocated before – Troianos77 Sep 09 '15 at 01:05
  • 2
    That's correct, @Troianos77 but for `realloc` to behave that way, the pointer must be NULL to begin with. You never initialise `resultado` to NULL. – paddy Sep 09 '15 at 01:17
  • The other problem is that you are allocating a single block of memory and then using it as if it is a 2D array. It's not. It's an array of pointers. You must initialise those pointers -- either to point at locations inside the memory block you allocated, or to point at some other part of memory. – paddy Sep 09 '15 at 01:20
  • How am i supposed to point those blocks to something then give a value to each space in the memory? I thought that by doing this: if(newMem) {//or here resultado = newMem; } int i = 0; for(i; i – Troianos77 Sep 09 '15 at 01:52
  • 2
    in helps immensely with the clarity/readability and understandability of the code when meaningful variable (and parameter) names are used. As it it, I read through the code the first time and still did not know the meaning/usage of 'n', 'k', 'p' and was still slightly doubtful about the parameter 'ar'. – user3629249 Sep 09 '15 at 01:56
  • this code block: ` if(newMem) {//or here resultado = newMem; } ` has the problem that what should be checked is if realloc() failed and handling a failure (call perror() handle cleanup, call exit()) otherwise, set resultado = newMem; and continue the program. The current code continues even if the call to realloc() fails – user3629249 Sep 09 '15 at 02:02
  • Your realloc calculation makes no sense, and you never initialize any pointers in the `resultado` array. To do `resultado[posicoes][i]`, you must make `resultado[posicoes]` point somewhere first. – M.M Sep 09 '15 at 02:05
  • the pointer `int **resultado` is on the stack and contains total trash. so passing it to realloc( resultado, ...) will corrupt the heap and can lead to a seg fault event. strongly suggest initializing 'resultado' via: `int **resultado = NULL;` passing a NULL pointer to realloc() is not a problem. – user3629249 Sep 09 '15 at 02:06
  • @M.M if i am not mistaken the 2 parameter must be the memory size that your pointer has+what you want, i always want n*int, and it always has posicoes*int*n bytes, and how am i supposed to make resultado[posicoes] point to something, than make each i for resultado[posicoes] point to something. user3629249 already fixed that, i forgot to write it here, sorry – Troianos77 Sep 09 '15 at 02:17
  • @Troianos77 look up some examples of how to allocate an array of pointers. You will need a loop that makes several allocation calls, one for each row. – M.M Sep 09 '15 at 02:17
  • 1
    the overall handling of 'resultado' is not correct. It should be: resulttadp = malloc( x*sizeof(int) ); if resuldado is NULL, handle error and exit otherwise, for( int i=0; i – user3629249 Sep 09 '15 at 02:18
  • By the way, it is much easier to make a dynamic matrix without using an array of pointers – M.M Sep 09 '15 at 02:19
  • 1
    continue: And resultado needs to be expanded on each loop interation (use realloc) and a new array needs to be pointed to by resultado[posicoes] = malloc( x*sizeof( int ) with the same checking for a failure of the call to realloc and the call to malloc – user3629249 Sep 09 '15 at 02:21
  • ok thanks, i've really forgot that you needed to allocate memory to each pointer from the pointer's pointer – Troianos77 Sep 09 '15 at 22:41

0 Answers0