Draw histograms in same pad

I have a function that looks something like this:

void drawHistVector(std::vector<std::vector<double> >& data){
    TCanvas* c = new TCanvas();
    TH1D* h = new TH1D("asd","asd",data[0].size(),0,1000);
    int i=0;
    for (int i=0;i<data.size();i++){
        for (int j=0;j<data[0].size();j++){
            h->SetBinContent(j+1,data[i][j]);
        }
        h->DrawCopy("same");
    }
}

I tried several different ways of drawing (e.g. also h->Draw(“same”)) but I either see only one histogram (i guess the last one) or I get a segmentation fault.
What is the correct way to draw multiple histograms into one pad?

your code fills only one histogram … why do you need to put the Drawing in a loop ?
Anyway the first Draw should be done without SAME

it is inside a loop, because i have the data for multiple histograms in a vector<vector>. I thought it is fine to use the same histogram for drawing, because I wont need the TH1Ds afterwards.

I replaced the

 h->DrawCopy("same");

with

        if (i==0) {  h->DrawCopy(); }
        else {h->DrawCopy("same");}

and now it works (ie. there is no segfault anymore). I still dont really understand and somehow I remember that in a different situation it was ok to use “same” also for the first one. However, thanks for the help

SAME means “draw the histogram on the existing pad”… the way you wrote your macro there is no existing pad at the 1st Draw(). That’s why you need to make the 1st Draw() without SAME to create the pad.