0

I found a lot of similar questions concerning this error, but the answers I found did not help me.

My program works for eleven data points which I read from a list "data.dat" without errors. If I increase the number of data points I get this error I don't understand. The file with my data consists of three columns of float numbers.

Here is a short version of my code.

    #define DIM 3

    #include <stdlib.h>
    #include <math.h>
    #include <stdio.h>
    #include <string.h>

    // Recursive function to build tree
    void my_function(FILE* outFile, double* r, int* idxList, unsigned long int* Nodes, int *node, int n, unsigned long int myKey)
    {
         unsigned long int myKey1 = myKey;

         for(int q=0; q<8; q++)
         {
              fprintf(outFile, "%d %d %d\n", q, n, *node);
              myKey1 = (myKey << 3)|q;
              Nodes[*node] = myKey1; 
              *node += 1;    
              if(*node < 20)
              {
                   my_function(outFile, r, idxList, Nodes, node, n, myKey1);
              }
         }
    }

    int main(int argv, char** argc)
    {
         FILE *inFile;
         inFile = fopen("data.dat", "r");

         FILE *outFile;
         outFile = fopen("result.dat", "w");

         unsigned int n = 15; // Number of points
         unsigned int N = n*DIM; // Elements in array

         // Array for points
         double *r = NULL;
         r = (double*)malloc(sizeof(double)*N);

         unsigned long int *Nodes = NULL;
         Nodes = (unsigned long int*)malloc(sizeof(unsigned long int)*3*n);
         for(int i=0; i<3*n; i++)
         {
              Nodes[i] = 0b0;
         }

         int *idxList = NULL;
         idxList = (int*)malloc(sizeof(int)*3*n);
         memset(idxList, -1, sizeof(int)*3*n); // Set all entries to -1

         for(int k=0; k<N; k++)
         {
              fscanf(inFile, "%lf", &r[k]);
         }

  int node = 0;
 unsigned long int myKey = 0b1;

         my_function(outFile, r, idxList, treeNodes, &node, n, myKey);

         // Close files
         fclose(inFile);
         fclose(outFile);

         // Clean up
         free(r);
         free(Nodes);
         free(idxList);
    }

The program works, but if I put more data points to the list, I get the following error.

*** Error in `./myProgram': munmap_chunk(): invalid pointer: 0x0000000000f74810 Aborted (core dumped)

If I comment the section

 // Close files
 fclose(inFile);
 fclose(outFile);

 // Clean up
 free(r);
 free(Nodes);
 free(idxList);

of my code, the error disappears. How can I fix this error? Any suggestions?

Gilfoyle
  • 3,282
  • 3
  • 47
  • 83
  • 1
    If you comment out `my_function`, does it work? If so, then you're asking us to debug code we can't see. – David Schwartz Nov 14 '16 at 10:40
  • This is an error memory, can you share `my_function` code? – developer Nov 14 '16 at 10:40
  • You don't seem to have any error checking. Check not only that the `malloc` calls return a non-null pointer but the `fopen` calls as well. Oh and you should probably read [this discussion about casting the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858). – Some programmer dude Nov 14 '16 at 10:44
  • @DavidSchwartz The function 'my_function' is quite big. So I tried to condense it. I hope that helps to find the error. – Gilfoyle Nov 14 '16 at 11:11
  • @Samuel Do you still have the error with `my_function` commented out? Are you 100% sure the error isn't there? Test it to be sure. – David Schwartz Nov 14 '16 at 11:12
  • @DavidSchwartz If I comment out `my_function` I get no error. But I don't get why the program works if I comment out the last sections of my code where I free the memory and close my files. `my_function' is a large function. Where could I probably start to find the error? – Gilfoyle Nov 14 '16 at 11:19
  • 1
    @Samuel that means error is with "my_function" . In free , may be you are trying to free some corrupted or invalid memory.. – jjm Nov 14 '16 at 11:24
  • @Samuel You could try commenting out parts of `my_function`. You could add `assert`s. You could use a debugging tool like valgrind. But the bug is likely in code you haven't shown us. (Before you ask for help, always make sure the code you share is capable or reproducing the error!) – David Schwartz Nov 14 '16 at 11:27
  • @DavidSchwartz I found out that when I comment out the part `Nodes[*node] = myKey1;` in `my_function` the error disappears. I wanted the variable `node` to change size for every iteration of my recursive function. But in my opinion the variable `node` gets to big for the size of my array `Nodes`. Is the incrementation of the variable `node` done wrong? – Gilfoyle Nov 14 '16 at 12:32
  • @Samuel If you're not sure, add an `assert` that `*node` is in range. It looks to me like `*node* will be `-1`, which is obviously out of range. – David Schwartz Nov 14 '16 at 18:08

0 Answers0