Hi
snprintf is working fine when i am not using append function . But as soon as i use append function snprintf crashes . Please suggest
why snprintf is crashing after append . What is special with append function which is crashing this program
Below is the program.
#include<string>
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char *s;
string name="ESACPE ";
name.append("hello '\\' ");
cout<<snprintf(s,30,name.c_str());
getch();
return 0;
}

Recommended Answers

All 6 Replies

After that think again. It seems you have wrote absolutely senseless code with this awkward jumble of std::string and C-string oriented snprintf ;)...

hi
I know that it is awkward that i am jumbling C++ string snprintf But my point here is that this same code is working fine when i am not calling append function of std::String and it crashes when i try append function . Why code is crashing only after call of append and snprintf ?

As opposed to say crashing when you do this?

char *s;
string name="ESACPE ";
cout<<snprintf(s,30,name.c_str());

Who knows, dumb luck perhaps.
It's certainly got the potential to crash at any moment.

> name.append("hello '\\' ");
The fact that this made it crash does NOT imply that your previous attempt was bug-free.


Try say
char s[100]; // was char *s

Thanks yes now it is working fine .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.