944,005 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 630
  • C++ RSS
Jan 1st, 2009
0

Please tell why this code is crashing

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tom_jerry042 is offline Offline
4 posts
since Jan 2009
Jan 1st, 2009
0

Re: Please tell why this code is crashing

Read this
http://www.daniweb.com/forums/announcement8-3.html

Then allocate some memory for s before trying to write data.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 1st, 2009
0

Re: Please tell why this code is crashing

After that think again. It seems you have wrote absolutely senseless code with this awkward jumble of std::string and C-string oriented snprintf ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jan 2nd, 2009
0

Re: Please tell why this code is crashing

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:tring and it crashes when i try append function . Why code is crashing only after call of append and snprintf ?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tom_jerry042 is offline Offline
4 posts
since Jan 2009
Jan 2nd, 2009
0

Re: Please tell why this code is crashing

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 2nd, 2009
0

Re: Please tell why this code is crashing

Thanks yes now it is working fine .
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tom_jerry042 is offline Offline
4 posts
since Jan 2009
Jan 2nd, 2009
0

Re: Please tell why this code is crashing

Initialize your character pointer.

C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 31
Solved Threads: 7
Light Poster
iDeveloper is offline Offline
49 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: simple visual studio express problem
Next Thread in C++ Forum Timeline: Segmentation Default Error when Initializing Class object





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC