*** 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
  strcat(CATNAME,type);
  strcat(CATNAME,".root");

  char DATANAME[50]  = "./Results/Data_tree/";
  // Se genera el path que contiene el catálogo con los datos de las fuentes
  strcat(DATANAME,type);
  strcat(DATANAME,"_tree.root");

  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");
  B->SetAddress(&source.List);
  C->SetAddress(&source.Pivot_Energy);

  // Se crea el catalogo para ROOT
  TFile *dat = new TFile(DATANAME,"RECREATE");
  TTree *tree = new TTree("Data","AGN Power Law Spectrum and Redshift");
  tree->Branch("Source_C",&src_branch.Name,"Name[20]/C:Type[3]/C");
  tree->Branch("Source_F",&src_branch.Pivot_Energy_,"Pivot_Energy_/f:Spectral_Index_/f:Flux1000_/f:Z_/f");
  tree->Branch("Source_coord",&src_branch.RA,"RA/f:DEC/f");

  //tree->Branch("Arrays",&arr_branch.Detections,"Detections[29]/O:Signifs[29]/F");

  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();
  fprintf(fDAT,"Ind\tName\t\tRA(deg)\tDEC\tType\tFlux1000\t\tPivot_Energy\t\tIndex\t\tz\n");
  cout << size << endl;

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

	  A->GetEntry(j);
	  strcpy(src_branch.Name,source.Name);
	  strcpy(src_branch.Type,source.Type);
	  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;
	  tree->Fill();
	  tree->Show(j);

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

	  //cout << src_branch.Name << " y " << src_branch.List << " y " << src_branch.Type << endl;
	  fprintf(fDAT,"%3d\t%s\t\t%9.2f\t\%9.2f\t\%s\t%4.4e\t\t%9.3f\t\t\t%9.3f\t\t%3.3f\n",j+1,src_branch.Name,src_branch.RA,src_branch.DEC,src_branch.Type,src_branch.Flux1000_,src_branch.Pivot_Energy_,src_branch.Spectral_Index_,src_branch.Z_);



  }
  tree->Print();


  fclose(fDAT);
  fclose(fR);
  //delete fDAT;
  //delete fR;
  dat->Write();
  //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’