How Can I Correctly Use sprintf to Define Histogram Titles in My Method?

  • Thread starter Thread starter neu
  • Start date Start date
  • Tags Tags
    Method
Click For Summary
The discussion focuses on a method for filling histograms in a C++ program, specifically using the sprintf function to create dynamic histogram titles based on a string identifier (histoID). The user encounters segmentation violations during execution, believed to stem from the use of sprintf. Key points include the need to initialize the histo1_name array to avoid random data and ensure proper string termination with a null character. Additionally, it is highlighted that sprintf is a C function that requires a C-style string, which means the C++ string type must be converted appropriately, possibly using a method like getData or a cast to char*. These insights aim to resolve the segmentation fault issue and improve the implementation of the histogram title generation.
neu
Messages
228
Reaction score
3
Basically I've made a method to fill histograms and I need to use sprintf to define histogram titles with a wildcard (%s) that I call to be a string such as proton, antiproton etc depending on what dat I want to plot.

So anyway, here's an example of what I've done:

void PythiaMC_Analysis::fillMM_Histos(string histoID, double deltaPseudo) {

char histo1_name[100];
sprintf(histo1_name,"%s_Matched_deltaEta",histoID);
Fill(histo1,deltaPseudo);

}

Which I'd call after defining the histoID to be proton, antiproton or whatever. I've simplified the code to one plot for the sake of brevity.

SO basically I'm getting segmentation violations to do with this method, I have deduced that it occurs at the first occurance of sprintf. Am I using it incorrectly?

Any help would be much appreciated.
 
Technology news on Phys.org


You should set at least the first element of histo1_name[] to 0, otherwise it is likely to be created full of random data (at least in a release build), a \0 is needed to mark the end of a C string.

Also sprintf is a c function, the %s matches a 'C' \0 terminated string, it doesn't know anything about the c++ string data type you are using.
Your string library probably has a getData method or can automatically convert it with something like (char*)histoID.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
8K
Replies
15
Views
5K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K