Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: runtime error free(); invalid next size (fast);

967 views
Skip to first unread message
Message has been deleted

Alf P. Steinbach

unread,
Feb 7, 2017, 12:59:40 AM2/7/17
to
On 07.02.2017 04:16, the...@hotmail.com wrote:
> #include <iostream>
> #include <fstream>
> #include <cmath>
> #include "params.h"
>
> // Define and allocate arrays
> double* rho_prev = new double[npnts]; // time step t-1
> double* rho = new double[npnts]; // time step t
> double* rho_next = new double[npnts]; // time step t+1
> double* x = new double[npnts]; // x values
>
> // Open output file
> std::ofstream fout(outfilename);
>
> // Declare function for timesteps
> void timestep();
>
> // Function for main computation
> void myLoops() {
> //some stuff
>
> // Take timesteps
> timestep();
>
> // Close file
> fout.close();
> std::cout << "Results written to " << outfilename << std::endl;
>
> // Deallocate arrays
> delete[] rho_prev;
> delete[] rho;
> delete[] rho_next;
> delete[] x;
> }
>
> // Function to take timesteps
> void timestep() {
> //some stuff
> }
>
>
> So I am having a problem with this code.

Well, to get more specific help you need to post a reproducible example.

Some easily recognizable issues with the above:

• Global variables.
They allow distant parts of the code to affect each other, in ways
that one can't grok. Nothing can be relied on where these are involved.
In your case perhaps deallocation is performed multiple times but
allocation is performed just once, maybe, but even if that's the case
there can still be many hidden problems: just get rid of all globals.

• Raw `new` and `delete`.
It's not smart to try to reinvent the automobile, trying to get
everything right, including the precision-machined engine parts and the
very special alloys used there. Instead just buy, borrow or steal an
automobile that works. The `std::vector` one is not so sexy but it's
dependable, works all right and is essentially free.

Cheers & hth.,

- Alf

Louis Krupp

unread,
Feb 7, 2017, 2:59:28 AM2/7/17
to
On Mon, 6 Feb 2017 19:16:24 -0800 (PST), the...@hotmail.com wrote:

>#include <iostream>
>#include <fstream>
>#include <cmath>
>#include "params.h"
>
>// Define and allocate arrays
>double* rho_prev = new double[npnts]; // time step t-1
>double* rho = new double[npnts]; // time step t
>double* rho_next = new double[npnts]; // time step t+1
>double* x = new double[npnts]; // x values

Where is npnts assigned a value? If it's a global variable and you
haven't assigned it a value before you do the allocation, it will be
zero, you'll allocate a bunch of zero-length arrays, and the first
time you try to store anything into one of them you'll trash the
memory links that make dynamic allocation possible and you'll get the
error you describe below:

>So I am having a problem with this code. I included "some stuff", which really is a bunch of loops that I have already tested and there's nothing wrong with them. In fact, this entire code (which is a piece of a larger code I modularized) doesn't show any problems when I'm compiling. It compiles just fine. The problem is, when I am running the executable I get a bunch of garbage like "*** glibc detected *** ./wave1dmod: free(): invalid next size (fast): 0x0000000000733010 ***" and some other nonsense after that. I don't know how to fix this. I know it most likely has to do with the double* arrays I put at the top but I don't know what to do with them so that this error stops appearing. Please help.

As Alf said, there are easier, safer ways to do this in C++.

Louis

Paavo Helde

unread,
Feb 7, 2017, 3:39:53 AM2/7/17
to
On 7.02.2017 5:16, the...@hotmail.com wrote:
>
> // Define and allocate arrays
> double* rho_prev = new double[npnts]; // time step t-1
> double* rho = new double[npnts]; // time step t
> double* rho_next = new double[npnts]; // time step t+1
> double* x = new double[npnts]; // x values

>
> So I am having a problem with this code. I included "some stuff", which really is a bunch of loops that I have already tested and there's nothing wrong with them. In fact, this entire code (which is a piece of a larger code I modularized) doesn't show any problems when I'm compiling. It compiles just fine. The problem is, when I am running the executable I get a bunch of garbage like "*** glibc detected *** ./wave1dmod: free(): invalid next size (fast): 0x0000000000733010 ***" and some other nonsense after that. I don't know how to fix this. I know it most likely has to do with the double* arrays I put at the top but I don't know what to do with them so that this error stops appearing. Please help.

The most probable cause for such errors is that your code contains
buffer overruns (writing behind the end of the allocated arrays). This
is a typical bug of C-style code like here (replacing malloc with new[]
is just syntactic sugar).

In C++ we have ways to write safer code. Use std::vector and e.g.
push_back for populating them. You might also consider using at()
instead of [], this will turn out-of-array access from UB into a
well-defined exception.

hth
Paavo



Scott Lurndal

unread,
Feb 7, 2017, 8:27:53 AM2/7/17
to
the...@hotmail.com writes:
>#include <iostream>
>#include <fstream>
>#include <cmath>
>#include "params.h"
>

>So I am having a problem with this code. I included "some stuff", which rea=
>lly is a bunch of loops that I have already tested and there's nothing wron=
>g with them. In fact, this entire code (which is a piece of a larger code I=
> modularized) doesn't show any problems when I'm compiling. It compiles jus=
>t fine. The problem is, when I am running the executable I get a bunch of g=
>arbage like "*** glibc detected *** ./wave1dmod: free(): invalid next size =
>(fast): 0x0000000000733010 ***" and some other nonsense after that. I don't=
> know how to fix this.

This error is caused because you're modifying data outside the bounds
of the array (which steps on bookkeeping data kept by malloc/free).
0 new messages