0

I have two issues that I believe are related. I am trying to create a dynamic array of variables and then access different specific members of the array. The code segment that is causing problems is:

        double *F_inertia, *F_drag, *F_KN;
        i = 0;
        F_inertia = new double[i];
        F_drag = new double[i];
        F_KN = new double[i];
        t = 0; 
        x = 0;

        for (z = 0; z >= -d; z = z-8) {
          F_drag[i] = ((0.5 * rho * Cd * Diam * ux * fabs(ux)));
          F_inertia[i] = (rho * Cm * Vol * ax); 
          F_KN[i] = (F_drag[i] + F_inertia[i])/1000;
          cout << i << "\t" << F_KN[i]<< endl;
          i++;
        }
               cout << F_KN[1] << endl;

        delete[] F_inertia;
        delete[] F_drag;
        delete[] F_KN;     

The line cout << i << "\t" << F_KN[i]<< endl; is outputting the values correctly, and all values are correctly being written to a file (earlier in the code). However, when I use the line cout << F_KN[9] << endl; as a test to see if that position holds the correct value, it prints out a 0, which is not the same value as the i = 1tst position in the for loop. Note: the values for F_KN[5]-F_KN[8] do print correctly! Just not any of the lower positions! I'm very confused by this!!

Secondly, after the code compiles and runs, this comes up, after giving me the correct values (except for the first problem):

*** glibc detected *** ./monopile_deflection: free(): invalid next size (fast): 0x0000000001365260 ***
monopile_deflection: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted (core dumped)

From reading I know this has to do with incorrectly allocating an array or trying to access an array value that doesn't exist, but I can't see where the problem is?

Thank you in advance for any help or advice!

user3460758
  • 817
  • 6
  • 14
  • 22
  • 3
    `F_inertia = new double[i];` with `i = 0;` feels strange. see http://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory – Ferenc Deak Oct 16 '14 at 11:35
  • Ok, without that though I was getting a warning that i may be uninitialised? I'm using i simply to keep track of which position in the array each value of F_KN, F_inertia, and F_drag are in. – user3460758 Oct 16 '14 at 11:43
  • Do I not need to dynamically allocate the array then? – user3460758 Oct 16 '14 at 11:44
  • @user3460758: fritzone was not suggesting to you to dump the initialisation altogether; he was suggesting to fix it! – Lightness Races in Orbit Oct 16 '14 at 11:56

1 Answers1

0
i = 0;
F_inertia = new double[i];
F_drag = new double[i];
F_KN = new double[i];

The amount of memory allocated by these lines does not magically change when you increment i later in the program.

So, you allocated three zero-sized arrays then started writing to its non-existent elements.
You corrupted plenty of memory there.

Either:

  • determine an appropriate value for i in a separate loop that you run before your allocation, or
  • use a std::vector<double> which can automatically grow for you.
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989