| | |
Please tell why this code is crashing
Thread Solved
![]() |
•
•
Join Date: Jan 2009
Posts: 4
Reputation:
Solved Threads: 1
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;
}
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;
}
Read this
http://www.daniweb.com/forums/announcement8-3.html
Then allocate some memory for s before trying to write data.
http://www.daniweb.com/forums/announcement8-3.html
Then allocate some memory for s before trying to write data.
As opposed to say crashing when you do this?
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
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
•
•
Join Date: Jul 2008
Posts: 49
Reputation:
Solved Threads: 7
Initialize your character pointer.
Note that +1 is used to accommodate for the terminating null.
Make sure you deallocate your pointer too.
And finally read this: http://publications.gbdirect.co.uk/c..._handling.html
to learn how to handle character arrays.
C++ Syntax (Toggle Plain Text)
char *ptr = new char[len + 1];
Note that +1 is used to accommodate for the terminating null.
Make sure you deallocate your pointer too.
And finally read this: http://publications.gbdirect.co.uk/c..._handling.html
to learn how to handle character arrays.
Last edited by iDeveloper; Jan 2nd, 2009 at 2:07 am.
![]() |
Similar Threads
- Jumps into code on program end (Pascal and Delphi)
- serious crashing problem in Visual C++ 6.0 (C++)
- Pygame crashing (Python)
- .exe crashing on executing output !! (C)
- Windows 2003 server crashing on dial-up connection (Windows NT / 2000 / XP)
- How to free up memory used for queues? (C)
Other Threads in the C++ Forum
- Previous Thread: simple visual studio express problem
- Next Thread: Segmentation Default Error when Initializing Class object
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets







...
tring and it crashes when i try