Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22194

Why do I get either a double free or corruption (out) or a C malloc assertion failure?

$
0
0

I am writing a code for a physics simulation, evolving some copies of the initial state with a stochastic and a deterministic evolution, returning the the value of some observable at each timestep of the simulation. I am using Armadillo (version 11.4.3) for handling the linear algebra and C++ vector to allocate memory. I am running it on Ubuntu. The code is something like this:

vec class::run (bool verbose) const { // verbose = true => prints some trajectories and the exact result  vec observables(_num_timesteps);  int n_observable = 0;  // Allocating _N_ensemble copies of the initial state  std::vector<cx_vec> psi(_N_ensemble);  for (int i = 0; i <= _N_ensemble; ++i)    psi[i] = _initial_state;  // Exact solution and printing some of the trajectories  cx_mat rho_ex(_dim, _dim);  rho_ex = projector(_initial_state);  ofstream out_ex, traj;  if (verbose) {    out_ex.open("exact.txt");    traj.open("trajectories.txt");  }  for (double t = 0.; t <= _t_f; t += _dt) { // Time evolution    if (verbose) { // Prints and evolves the exact solution      out_ex << observable(rho_ex) << endl;      rho_ex = exact_evolution(rho_ex,t)    }    cx_mat rho(_dim, _dim, fill::zeros); // Average state    for (int i = 0; i < _N_ensemble; ++i) { // Cycle on the ensemble members      if (verbose && i < _N_traj_print) // Prints some trajectories        traj << observable(projector(psi[i])) << " ";      rho += projector(psi[i])/((double)_N_ensemble);      psi[i] = evolve(psi[i],t);    }    // Storing the observable    observables[n_observable] = observable(rho);    n_observable++;    if (verbose) traj << endl;  }  return observables;}

All the variables starting with an underscore are supposed to be member variables of the class.

The idea is to execute run(verbose)N times. For the first execution, everything works fine (as long as _N_ensemble is sufficiently big: if _N_ensemble=1000 everything is fine, if _N_ensemble=100 I get a C malloc assertion failure; see the following for more details).

The problems appear the second time I try to execute it the second time. If I run it with verbose=false, it correctly arrives at the end of the function, but when returning observables, it throws the error

double free or corruption (out)

If I run it with verbose=true, it stops during the for cycle on i with t=0, giving a C malloc assertion failure:

malloc.c:2617: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

I have no idea why it throws those errors, since all the allocation of memory is be handled by C++ vector and not by me directly, and why it only happens during the second run. Does anyone have any idea why it happens?

Interestingly, if I run the same code on MacOS, everything works fine and I don´t get those errors.

I tried clearing the allocated vectors manually, but nothing changes.


Viewing all articles
Browse latest Browse all 22194

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>