0

I am getting this error:

The variable "thename" is being used without being initialized

Here's my code:

char *thename;
cm++;
sprintf(thename, "tutmap%d.map", cm);
4
  • 3
    @DeadMG answering his question is another option, although I agree with you about his C++ abilities. Commented Jun 4, 2011 at 16:01
  • 1
    Thumb rule : Always initialize pointer variables; make it char *thename = NULL; Then work yourself why you are getting a segmentation fault :) Commented Jun 4, 2011 at 16:03
  • This question should be tagged C. So that people who write C code can comment on it. Commented Jun 4, 2011 at 18:42
  • You have a good compiler: it's saving you from making a bad figure of yourself in front of your teacher. Always pay attention to what your compiler tells you, be it warnings or errors. Assume it is being helpful. Commented Jun 4, 2011 at 18:51

6 Answers 6

8

You are using the variable without initializing it, and running this code will be Undefined Behavior.

Perhaps you meant:

char thename[42];
cm++;
sprintf(thename, "tutmap%d.map", cm);

If you have snprintf, here's a function which documents that you guarantee ("assert") the buffer to be large enough, but then also checks the buffer length and aborts if you made an error:

template<int N>
void fixed_sprintf(char (&array)[N], char const *format, ...) {
  va_list args;
  va_start(args, format);
  int used = vsnprintf(array, N, format, args);
  va_end(args);
  if (used == N - 1) {
    throw whatever_exception_type_you_like("buffer too small");
    // or even:
    abort();
  }
}

The "fixed" means "fixed-size", not "opposite of broken". :)

Sign up to request clarification or add additional context in comments.

6 Comments

I always liked to include “demons flying out of your nose” as part of Undefined Behavior. OK, we all know that particular outcome is unlikely but it's still a wonderful/horrible image…
Using fixed-size char buffers is like asking for buffer overflows.
@delnan: Not when the size you need is fixed.
@Fred: what if it's a platform with >103 bit integers?
@ybungalobill: That is a chance I will have to take. Use 10+sizeof(STRINGIZE(INT_MIN))+1 if you're truly paranoid.
|
2

You've declared a pointer to a C string but not allocated any memory for it.

If you want stack allocated memory use

char thename[buffer_length];

If you prefer heap allocation use

char *thename = malloc(buffer_length);

I know the question is tagged C++ but your code looks more like C to me. Hence the use of malloc.

Comments

1

It's pretty easy to understand:

char *thename;

does not initialise thename, and then:

sprintf(thename, "tutmap%d.map", cm);

uses it. With dire results, in this case.

Comments

1

Instead of writing:

char *thename;
cm++;
sprintf(thename, "tutmap%d.map", cm);

Write:

cm++;
std::stringstream stream;     
stream << "tutmap" << cm << ".map";
std::string const name = stream.str();
// and if you really need char* then:
char const* thename = name.c_str();

2 Comments

I don't like: char const* thename = name.c_str();. It makes the code hard to maintain as you have an implicit requirement (that is not documented) that name can not be modified (or read in some cases (name[0] may cause a copy in COW implementations)) without thename becoming invalid. The only time it is safe to use c_str() is when you pass it as a parameter to a function.
@Martin: Ok made the name const as well.
0

thename is a pointer that has not been initialized. Change thename to an array of char that will fit the size of the complete string generated by sprintf.

Comments

0
char *thename = new char[21]; //21 is used for maximum string length of 20
cm++;
sprintf(thename, "tutmap%d.map", cm);

7 Comments

Why new? Conventionally, a char array of size 20 has a maximum string length of 19.
To clarify what Fred is saying, the array of 20 chars can hold a string of max-length 19 because of the terminator NUL byte at the end (which must be present, even if it's not “in” the string).
OK, updated length problem. Using new or the other method depends on the situation the code is running. The old heap and stack story. None of them is better. It just depends!
@FarshidZaker: Except for small, fixed sizes, prefer a local array by default and change to dynamic as needed.
@FarshidZaker: If you know it's needed, then use it, as I said. You should still prefer a std::string (or a better type) to a raw char array by default, in that case, however.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.