Please tell why this code is crashing

Thread Solved
Reply

Join Date: Jan 2009
Posts: 4
Reputation: tom_jerry042 is an unknown quantity at this point 
Solved Threads: 1
tom_jerry042 tom_jerry042 is offline Offline
Newbie Poster

Please tell why this code is crashing

 
0
  #1
Jan 1st, 2009
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;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Please tell why this code is crashing

 
0
  #2
Jan 1st, 2009
Read this
http://www.daniweb.com/forums/announcement8-3.html

Then allocate some memory for s before trying to write data.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Please tell why this code is crashing

 
0
  #3
Jan 1st, 2009
After that think again. It seems you have wrote absolutely senseless code with this awkward jumble of std::string and C-string oriented snprintf ...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: tom_jerry042 is an unknown quantity at this point 
Solved Threads: 1
tom_jerry042 tom_jerry042 is offline Offline
Newbie Poster

Re: Please tell why this code is crashing

 
0
  #4
Jan 2nd, 2009
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 ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Please tell why this code is crashing

 
0
  #5
Jan 2nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: tom_jerry042 is an unknown quantity at this point 
Solved Threads: 1
tom_jerry042 tom_jerry042 is offline Offline
Newbie Poster

Re: Please tell why this code is crashing

 
0
  #6
Jan 2nd, 2009
Thanks yes now it is working fine .
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 49
Reputation: iDeveloper is an unknown quantity at this point 
Solved Threads: 7
iDeveloper iDeveloper is offline Offline
Light Poster

Re: Please tell why this code is crashing

 
0
  #7
Jan 2nd, 2009
Initialize your character pointer.

  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC