Re: Re: memory problem?

From: Axel Naumann <Axel.Naumann_at_cern.ch>
Date: Thu, 23 Nov 2006 16:52:29 +0100


Hi Chiara!

Your array Short_t tofarray[size][nbins] is 16000*1500*2 bytes big, that's 48Mbytes. There are two ways to get memory, stack and heap. By declaring it without "new" you allocate these 48MB on the stack. And that's too much - stack size is limited, you can adjust that by telling the linker, but you didn't, and usually allocating 48MB on the stack is not a good idea. So you could allocate it on the heap, using Short_t *tofarray = new Short_t[size][nbins].

You could. But having an array that big usually is a hint that you're not writing smart enough code ;-) You might want to reduce the size of that array. Do you really need to access the _whole_ array at some point? Or is just a subsection enough? Can you reduce the loops? Like here:

Double_t Average_BAD_CODE() {
  Double_t v[10000][10000];
  for (int x=0; x<10000; ++x)
    for (int y=0; y<10000; ++y)
      v[x][y]=GetValueFromSomewhere(x,y);

  Double_t ret = 0.;
  for (int x=0; x<10000; ++x)
    for (int y=0; y<10000; ++y)
      ret += v[x][y];

  return ret/10000/10000;
}

Instead, this is better:

Double_t Average_BETTER_CODE() {
  Double_t ret = 0.;
  for (int x=0; x<10000; ++x)
    for (int y=0; y<10000; ++y)
      ret += GetValueFromSomewhere(x,y);

  return ret/10000/10000;
}

Cheers, Axel.

Chiara Zampolli wrote:

> PS: obviously, I miscalled chiara.cxx as a class, but it is a main....
> 
> 
> Chiara Zampolli wrote:

>> Dear rooters,
>>
>> I think I have some problems with memory allocation in compiling a
>> class (or something like this), and I really hope you could help me...
>> You can find a simplified version of my class in the attached file
>> chiara.cxx, which is to be compiled with the attached MakefileSimple.
>> As you can see, the compilation succeeds, but then, when you try to
>> run the executable, you get a "segmentation fault" error:
>>
>> [zampolli_at_pctof1 Chiara]$ ./chiara
>> Segmentation fault
>> [zampolli_at_pctof1 Chiara]$
>>
>> (to be noted that this happens also if the "if" of line 17 is not
>> commented, so if the array - on which the problem may depend - should
>> not be created during the execution...). On the contrary, this does
>> not happen if I set the size variable to 1600, or if I use a macro
>> (test.C) within a root session (instead of the executable "chiara").
>> Could you please help me in understanding the problem?
>> I am using the root v5-13-04 version, on a SL 4.2 pc.
>>
>> Thank you a lot.
>> Best Regards,
>>
>> Chiara
>>
>> ------------------------------------------------------------------------
>>
>> #---------------------------------------------------------------------
>> #
>> #
>> TARGET := chiara
>> SOURCES := chiara.cxx
>> #BINS := chiara chiara.sh
>>
>> #---------------------------------------------------------------------
>> # you shouldn't need to change anything below this line. #
>> OBJECTS := $(TARGET).o $(filter-out $(TARGET).o, \
>> $(filter %.o, $(SOURCES:%.cxx=%.o)) \
>> $(filter %.o, $(SOURCES:%.c=%.o)))
>>
>> #---------------------------------------------------------------------
>> # Third party libraries #
>> ROOT_CFLAGS := $(filter-out -I% -D%, $(shell root-config --cflags))
>> ROOT_CPPFLAGS := $(filter-out $(ROOT_CFLAGS), $(shell root-config
>> --cflags))
>> ROOT_LDFLAGS := $(filter-out -l%, $(shell root-config --libs))
>> ROOT_LIBS := $(filter -l%, $(shell root-config --libs)) -lNew -lVMC
>> -lGeom -lEG -lMinuit
>>
>> #---------------------------------------------------------------------
>> # Compilers and such
>> # CPP := g++ -E
>> CPPFLAGS := $(ROOT_CPPFLAGS)
>> CC := gcc -c CFLAGS := $(ROOT_CFLAGS)
>> CXX := g++ -c CXXFLAGS := $(CFLAGS)
>> LD := g++ LDFLAGS := $(ROOT_LDFLAGS)
>> LIBS := $(ROOT_LIBS)
>>
>> #---------------------------------------------------------------------
>> # Pattern rules # %.o:%.cxx
>> $(CXX) $(CPPFLAGS) $(CXXFLAGS) $<
>> %.o:%.c
>> $(CC) $(CPPFLAGS) $(CFLAGS) $<
>> %:%.o
>> $(LD) $^ $(LDFLAGS) $(LIBS) -o $@
>>
>> #---------------------------------------------------------------------
>> # Targets
>> all: $(TARGET)
>>
>> clean:
>> rm -rf $(TARGET) $(OBJECTS)
>>
>> show:
>> @echo "TARGET : $(TARGET)"
>> @echo "SOURCES : $(SOURCES)"
>> @echo "OBJECTS : $(OBJECTS)"
>> @echo "-----------------"
>> @echo "ROOT_CPPFLAGS : $(ROOT_CPPFLAGS)"
>> @echo "ROOT_CFLAGS : $(ROOT_CFLAGS)"
>> @echo "ROOT_LDFLAGS : $(ROOT_LDFLAGS)"
>> @echo "ROOT_LIBS : $(ROOT_LIBS)"
>> @echo "-----------------"
>> @echo "CPP : $(CPP)"
>> @echo 'CPPFLAGS : $(CPPFLAGS)'
>> @echo "CXX : $(CXX)"
>> @echo 'CXXFLAGS : $(CXXFLAGS)'
>> @echo "LD : $(LD)"
>> @echo "LDFLAGS : $(LDFLAGS)"
>> @echo "LIBS : $(LIBS)"
>>
>> $(TARGET):$(OBJECTS)
>>
>> #---------------------------------------------------------------------
>> #
>> # EOF
>> #
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>>
>> #include <Riostream.h>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> #include "TFile.h"
>> #include "TTree.h"
>> #include "TKey.h"
>> #include "TFile.h"
>>
>> //int main(int argc, char **argv){
>> int test(){
>> // static const Int_t size=1600;
>> static const Int_t size=16000;
>> static const Int_t nbins=1500;
>> Short_t tofarray[size][nbins];
>> TFile *fileout = new TFile("outArrayC.root","OPEN");
>> cout << " qui ok inzio fileout = " << fileout << endl; TTree *
>> tree = 0x0;
>> TIter next(fileout->GetListOfKeys());
>> TKey *key;
>> while ((key=(TKey*)next())){
>> cout << " loooooop over keys.... " << endl;
>> const char * namekey = key->GetName();
>> if (strcmp(namekey,"T")==0){
>> cout << " found tree " << endl;
>> tree = (TTree*) fileout->Get("T");
>> break;
>> }
>> }
>> fileout =0x0;
>> delete fileout;
>> tree =0x0;
>> delete tree;
>> key =0x0;
>> delete key;
>>
>> return 1;
>> }
>> ------------------------------------------------------------------------
>>
>>
>> #include <Riostream.h>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> #include "TFile.h"
>> #include "TTree.h"
>> #include "TKey.h"
>> #include "TFile.h"
>>
>> int main(int argc, char **argv){
>> //int ciccino(){
>> // static const Int_t size=1600;
>> static const Int_t size=16000;
>> static const Int_t nbins=1500;
>> Int_t flag=0;
>> //if (flag !=0){
>> Short_t tofarray[size][nbins];
>> //}
>> TFile *fileout = new TFile("outArrayC.root","OPEN");
>> cout << " qui ok inzio fileout = " << fileout << endl; TTree *
>> tree = 0x0;
>> TIter next(fileout->GetListOfKeys());
>> TKey *key;
>> while ((key=(TKey*)next())){
>> cout << " loooooop over keys.... " << endl;
>> const char * namekey = key->GetName();
>> if (strcmp(namekey,"T")==0){
>> cout << " found tree " << endl;
>> tree = (TTree*) fileout->Get("T");
>> break;
>> }
>> }
>> fileout =0x0;
>> delete fileout;
>> tree =0x0;
>> delete tree;
>> key =0x0;
>> delete key;
>>
>> return 1;
>> }

>
> Received on Thu Nov 23 2006 - 16:52:40 MET

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:02 MET