0
int fCount = 0; char frameNum[7]; sprintf(frameNum, %06u", fCount); int fCount = 0; char frameNum[6]; sprintf(frameNum, %06u", fCount); 

Q1. Which is correct, 6 or 7?

Q2. I am using VC6 and the file is sample.cpp.
I think sprintf is C. Is there a better way? I need the char string right justified and with padded zeros.

Please don't tell me to use a newer compiler. I need to use VC6 for now.

3
  • 3
    The problem with using VC6 is that we can tell you what would be correct C++, but none of us have much of a clue what has to be done to make VC6 accept your code. It is in a world of its own. Commented Sep 9, 2011 at 18:44
  • 1
    Note that the "6" in "%06u" is the minimum field width, not the exact field width. So if you change fCount to 1000000, you'll overflow the character buffer. Commented Sep 9, 2011 at 19:07
  • That is exactly my problem. The code was >10 years old. They were dealing with Kilobytes. Now I am dealing with Gigabytes, crazy things is happening. It does not scale well. Commented Sep 9, 2011 at 19:15

4 Answers 4

3

First one is correct. There is no memory leak, by the way.

--

I think sprintf is C. Is there a better way?

Yes. C++ way:

std::stringstream ss; //#include <sstream> ss << fCount; std::string frameNum = ss.str(); 
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for a correct answer. VC6 is older, and may require the use of a strstream instead.
It should be noted that while there is no memory leak, there is potential stack corruption, which can cause pretty much anything to happen.
0

7, as sprintf will append a null byte '\0' to the end of the string.

Comments

0

Neither will be a memory leak - the data is on the stack!

1 Comment

Will using 6 instead of 7 create a problem? Take it to the extreme, will using a 4 or 5 create a problem? Sorry I am not good enough to tell the difference between a heap and a stack. I need to read up on that.
0

Q1. Which is correct, 6 or 7?

Neither. The 6 in the format string is a minimum width, so 7 characters will not be enough if fCount >= 1000000. The smallest size that won't overflow for any input is std::numeric_limits<int>::digits10 + 2 (to allow for all the decimal digits, the terminating character, and the sign if the input is negative). Assuming that VC6 provides <numeric_limits>; otherwise sizeof(int)*3 + 2 is a reasonable upper bound. If you want to be sure, call snprintf and check the return value.

Q2. I am using VC6 and the file is sample.cpp. I think sprintf is C. Is there a better way? I need the char string right justified and with padded zeros.

In most cases you're better off using C++ strings and streams, which manage their own memory and won't overflow unless you do something very strange.

std::ostringstream s; s << setw(6) << setfill('0') << fCount; std::string frameNum = s.str(); 

I'm fairly sure VC6 supports these, but it's been over a decade since I had the misfortune to battle with that compiler, and I've done my best to forget exactly how limited it was. I know you asked me not to, but I will say: use a newer compiler. The language has changed a lot in the last 15 years.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.