0

This line:

strcat(query,*it);

(where *it is an iterator to a string)

Keeps giving me this error:

no matching function for call to ``strcat(char[200], const std::basic_string, std::allocator >&)`'

I guess it's because strcat takes in a char* while *it is a string. How do I convert it from a string to a char* to make it work with strcat() ?

I've tried strcat(query,(*it).c_str()) but that just gives me a runtime error.

Edit: sorry, it should be converted to a const char*

4
  • Please clarify: what is the runtime error? Commented Nov 12, 2008 at 10:35
  • Unfortunately, I am not sure what the runtime is. I am using Dev-c++ to compile and the runtime is "mysql.exe has encountered an error and needs to close" Commented Nov 12, 2008 at 10:38
  • (1) Make sure where the runtime error really occurs. since mysql.exe fails, I'd expect it's not exactly crashing on that line (2) How is "query" declared? (3) Inspect the resulting query string (output to log file, debug trace, or simple in the editor). Is it OK? Commented Nov 12, 2008 at 14:27
  • Have you tried stepping into/or over the strcat() call in a debugger? What's getting passed to it? Commented Nov 12, 2008 at 18:53

9 Answers 9

7

strcat(query,(*it).c_str()) should work. What's the runtime error? Are you sure that query is null-terminated before you make the call?

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

2 Comments

Yup, I am sure query is null-terminated. I also tried it with printf("%s", (*it).c_str() );
@Steve: How are you initializing the iterator?
2

Buffer overflow?

char   query[200] = {0}; // Make sure this array initialized before
                         // you start concatenating strings onto it.

for (it = vec.begin();it != vec.end();++it)
{
   if ((strlen(query) + it->length() + 1) >= 200)
   {
       logError("Buffer oveflow detected.";
       break;
   }
   strcat(query, it->c_str());
}

Comments

2

Use the debugger, Luke!

(*it).c_str() Sure as hell should be a valid argument for strcat, assuming that your iterator is valid, and assuming that query is a null-terminated string, so should that. The quickest way to find out which of them are misbehaving is to watch it do so and inspect the values of it and query at runtime.

Comments

1

You need to be more specific about which runtime error you get. Calling c_str() on the string should be the correct solution. As always with strcat() and other classic C string functions without bounds checking, you must be careful not to pass it too long of an input.

3 Comments

Too long input is no problem. If it fits in your adress space, strcat should work. What is a problem, is a string with no terminating '\0'.
Unfortunately, I am not sure what the runtime is. I am using Dev-c++ to compile and the runtime is "mysql.exe has encountered an error and needs to close"
@gnud: Huh? If the first argument here is char query[200], and the std::string is 2000 characters long, then surely that is a problem?
1

When you're sure about the length of the buffer you're strcatting in (e.g. 200), better use strncat; this will rule out the Buffer Overflow mentioned by @Martin. Otherwies check for the total length before concatenating (this is a precondition for its use!)

Queries typically become way longer than 200 characters, by the way. If you're not sure about the length of the resulting query, fall back on a dynamic string, like std::string.

Comments

1

Since you've ruled out that query is not null-terminated, it seems the consensus is that the problem is likely to be one of the following:

  1. buffer overflow - the buffer pointed to by query is not large enough to have (*it).c_str() concatenated to it
  2. the itereator, it, is invalid. This can happen in several ways, including:

    • not being properly initialized;
    • has the value of someContainer.end();
    • or the container has been modified in some way that invalidates an existing iterator

You should be able to determine what's going on with a debugger. Also, I'm sure if you post more code, that shows how query and it are defined and used, you'll get a definitive answer here, too (how's that for remote debugging).

Comments

0

Try this (I assume that the runtime error is because of a NULL/invalid pointer):

for (...; it != str.end(); ++it)
...
   if (!it->empty())
   {
      strcat(query, it->c_str());
   }

EDIT: Sorry, c_str() never returns NULL, which I temporarily forgot, so it is always safe. Unless the query buffer is not long enough to be able to contain all of the concatenated strings of course (or there is some other issue, like iterator beyond .end(), the container modified during the loop, or something similar).

2 Comments

c_str() does not return a null pointer, even if the string is empty.
Oops, was a bit too fast there... Another possible cause in the line of this is however that the iterator is not checked for .end() (or a multithreading/reentrancy error)
0

Is *it pointing at a valid string in all cases? Could it be pointing at end() in the last iteration? Or may be the container it points into got modified, invalidating *it.

Comments

0

If the application is in release mode, trace the application by putting the message boxes or by generating the interrupt 3. (_asm int 3;) in particular places. And if you have put the interrupt it exe will popup a debug message. Attach the process to Visual Studio to debug it. Hope this way we can know the place of crash.

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.