*** glibc detected *** ERROR

Hello everyone!

I have been fighting with my code for a couple of days and I cannot find the reason why my code is ALWAYS giving me this error while executing one process:

*** glibc detected *** /usr/local/root/bin/root.exe: free(): invalid next size (fast): 0x08d03828 ***

After it gives a lot of backtrace and memory map and root crashes. The code is just reading other TTrees and other .dat files and create another TTree with its data. The code is (sorry because its long…):

#include <Signif.h>
#include <Signif.cxx>

//#include <funct_array.C>

void data_tree(char type[15] = “extragalactic”, char deg[2] = “20”){

  char CATNAME[50]  = "./Fermi_Sources/";
  // Se genera el path que contiene el catálogo con los datos de las fuentes

  char DATANAME[50]  = "./Results/Data_tree/";
  // Se genera el path que contiene el catálogo con los datos de las fuentes

  int n_array = 29;
  if (deg == "50")
    n_array = 13;

  catalog_t source;
  source_branch src_branch;
  arrays_branch arr_branch;

  TFile *f = new TFile(CATNAME,"READ");
  TTree *A = (TTree*)f->Get("A");
  TBranch *B = A->GetBranch("catalog");
  TBranch *C = A->GetBranch("catalog2");

  // Se crea el catalogo para ROOT
  TFile *dat = new TFile(DATANAME,"RECREATE");
  TTree *tree = new TTree("Data","AGN Power Law Spectrum and Redshift");


  char GOODFERMI[50]  = "/home/tarekh/workspaceR/R-Project/good_fermi.dat";
  char DATSOURCES[50]  = "./Results/Data_tree/sources.dat";
  char kk[5];
  FILE *fR = fopen(GOODFERMI,"r");
  FILE *fDAT = fopen(DATSOURCES,"w");

  fscanf(fR,"%s%s%s%s",&kk, &kk, &kk, &kk);

  const int size = A->GetEntries();
  cout << size << endl;

  Float_t RAA, DECC;
  for(int j=0; j<(size);j++){

	  src_branch.Pivot_Energy_ = source.Pivot_Energy;
	  src_branch.Spectral_Index_ = source.Spectral_Index;
	  src_branch.Flux1000_ = source.Flux1000;
	  src_branch.Z_ = source.Z;
	  fscanf(fR,"%s%s%f%f",&kk, &kk, &RAA, &DECC);
	  src_branch.RA = RAA;
	  src_branch.DEC = DECC;

	  //cout << src_branch.Pivot_Energy_ << " y " << src_branch.Spectral_Index_ << " y " << src_branch.Flux1000_ << endl;

	  //cout << src_branch.RA << " y " << src_branch.DEC << endl;

	  //cout << src_branch.Pivot_Energy << endl;

	  //cout << src_branch.Name << " y " << src_branch.List << " y " << src_branch.Type << endl;


  //delete fDAT;
  //delete fR;
  //delete f;
  //delete dat;


// Estructura del catalogo
struct catalog_t {
char List[8];
char Name[20];
char Type[3];
float Pivot_Energy; //Energy at which error on differential flux is minimal [MeV]
float Spectral_Index; //Best fit power law slope
float Flux1000; //Integral flux from 1 to 100 GeV [cm-2.s-1]
float Z; // Source redshift

struct source_branch {
char Name[20];
char Type[3];
Float_t RA;
Float_t DEC;
Float_t Pivot_Energy_; //Energy at which error on differential flux is minimal [MeV]
Float_t Spectral_Index_; //Best fit power law slope
Float_t Flux1000_; //Integral flux from 1 to 100 GeV [cm-2.s-1]
Float_t Z_; // Source redshift

struct arrays_branch {
Bool_t Detections[29];
Float_t Signifs[29];


I read somewhere that kind of error is due to erasing variables, but I just try to remove all of them, or add as much as possible to erase everything and there is no way of making it not to crash…

If anyone can help me I would be delighted!! thank you all for your help!

Ok, the problem was the size of the char called “kk”. It was too small…

But couldn’t ROOT or C++ find a way to show the error? argh…

The problem was not that your “kk” was too small. The problem was that you tried to pack/read too many characters into it (i.e. a “run-time error”). In future, use something like this (note: 4 = 5 - 1, search “man fscanf” for “field width”):

char kk[5]; // place for 5 characters

fscanf(fR, “%4s%4s%4s%4s”, kk, kk, kk, kk); // “%4s” = read at most 4 characters’ string plus a ‘\0’

fscanf(fR, “%4s%4s%f%f”, kk, kk, &RAA, &DECC); // “%4s” = read at most 4 characters’ string plus a ‘\0’