Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sprintf causing segmantation fault

Reply
Thread Tools

sprintf causing segmantation fault

 
 
Saurabh
Guest
Posts: n/a
 
      06-05-2006
Hi all,

I am working on RedHat Linux GCC 3.0.
I am trying to convert a long to string through sprintf.
but i m getting segmantation fault.
I tried snprintf also but no avail.here is the piece of code..

----------------------------
long myLong=200000;
char myStr[50];
memset[myStr,'\0',50];
sprintf(myStr,"%s",myLong);

-------------------------------------

This sprintf causes segmentation fault.

Hoping for help.

Saurabh

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-05-2006
Saurabh wrote:
> Hi all,
>
> I am working on RedHat Linux GCC 3.0.
> I am trying to convert a long to string through sprintf.
> but i m getting segmantation fault.
> I tried snprintf also but no avail.here is the piece of code..
>
> ----------------------------
> long myLong=200000;
> char myStr[50];
> memset[myStr,'\0',50];
> sprintf(myStr,"%s",myLong);

^
%s is for printing (C style) strings.

use %ld, or use iostreams.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Saurabh
Guest
Posts: n/a
 
      06-05-2006

Ian Collins wrote:
> Saurabh wrote:
> > Hi all,
> >
> > I am working on RedHat Linux GCC 3.0.
> > I am trying to convert a long to string through sprintf.
> > but i m getting segmantation fault.
> > I tried snprintf also but no avail.here is the piece of code..
> >
> > ----------------------------
> > long myLong=200000;
> > char myStr[50];
> > memset[myStr,'\0',50];
> > sprintf(myStr,"%s",myLong);

> ^
> %s is for printing (C style) strings.
>
> use %ld, or use iostreams.
>
> --
> Ian Collins.


hi Ian,
Thanks a lot.
You solved my problem.
actually I thought that the second argument to
sprintf() refers to the format you wish to convert
to,instead it refers to the format you wish to
convert from.
anyways..
thanks again.
saurabh

 
Reply With Quote
 
Geo
Guest
Posts: n/a
 
      06-05-2006

Saurabh wrote:
> Ian Collins wrote:
> > Saurabh wrote:
> > > Hi all,
> > >
> > > I am working on RedHat Linux GCC 3.0.
> > > I am trying to convert a long to string through sprintf.
> > > but i m getting segmantation fault.
> > > I tried snprintf also but no avail.here is the piece of code..
> > >
> > > ----------------------------
> > > long myLong=200000;
> > > char myStr[50];
> > > memset[myStr,'\0',50];
> > > sprintf(myStr,"%s",myLong);

> > ^
> > %s is for printing (C style) strings.
> >
> > use %ld, or use iostreams.
> >
> > --
> > Ian Collins.

>
> hi Ian,
> Thanks a lot.
> You solved my problem.
> actually I thought that the second argument to
> sprintf() refers to the format you wish to convert
> to,instead it refers to the format you wish to
> convert from.
> anyways..
> thanks again.
> saurabh



I would suggest that if you are STILL using sprinf, then you haven't
fixed the problem, it will surely break one day. Why not use
boost::lexical_cast (or you're own similar code) instead.

 
Reply With Quote
 
Saurabh
Guest
Posts: n/a
 
      06-05-2006

Geo wrote:
> Saurabh wrote:
> > Ian Collins wrote:
> > > Saurabh wrote:
> > > > Hi all,
> > > >
> > > > I am working on RedHat Linux GCC 3.0.
> > > > I am trying to convert a long to string through sprintf.
> > > > but i m getting segmantation fault.
> > > > I tried snprintf also but no avail.here is the piece of code..
> > > >
> > > > ----------------------------
> > > > long myLong=200000;
> > > > char myStr[50];
> > > > memset[myStr,'\0',50];
> > > > sprintf(myStr,"%s",myLong);
> > > ^
> > > %s is for printing (C style) strings.
> > >
> > > use %ld, or use iostreams.
> > >
> > > --
> > > Ian Collins.

> >
> > hi Ian,
> > Thanks a lot.
> > You solved my problem.
> > actually I thought that the second argument to
> > sprintf() refers to the format you wish to convert
> > to,instead it refers to the format you wish to
> > convert from.
> > anyways..
> > thanks again.
> > saurabh

>
>
> I would suggest that if you are STILL using sprinf, then you haven't
> fixed the problem, it will surely break one day. Why not use
> boost::lexical_cast (or you're own similar code) instead.


hi Geo,
I have never used boost::lexical_cast.Is this an STL component?
or are you hinting me to write my own version of sprintf()?

Thanks & Regards
saurabh

 
Reply With Quote
 
Geo
Guest
Posts: n/a
 
      06-05-2006

Saurabh wrote:
> Geo wrote:
> > Saurabh wrote:
> > > Ian Collins wrote:
> > > > Saurabh wrote:
> > > > > Hi all,
> > > > >
> > > > > I am working on RedHat Linux GCC 3.0.
> > > > > I am trying to convert a long to string through sprintf.
> > > > > but i m getting segmantation fault.
> > > > > I tried snprintf also but no avail.here is the piece of code..
> > > > >
> > > > > ----------------------------
> > > > > long myLong=200000;
> > > > > char myStr[50];
> > > > > memset[myStr,'\0',50];
> > > > > sprintf(myStr,"%s",myLong);
> > > > ^
> > > > %s is for printing (C style) strings.
> > > >
> > > > use %ld, or use iostreams.
> > > >
> > > > --
> > > > Ian Collins.
> > >
> > > hi Ian,
> > > Thanks a lot.
> > > You solved my problem.
> > > actually I thought that the second argument to
> > > sprintf() refers to the format you wish to convert
> > > to,instead it refers to the format you wish to
> > > convert from.
> > > anyways..
> > > thanks again.
> > > saurabh

> >
> >
> > I would suggest that if you are STILL using sprinf, then you haven't
> > fixed the problem, it will surely break one day. Why not use
> > boost::lexical_cast (or you're own similar code) instead.

>
> hi Geo,
> I have never used boost::lexical_cast.Is this an STL component?
> or are you hinting me to write my own version of sprintf()?
>
> Thanks & Regards
> saurabh


Hi,

No I was suggesting you write you're own lexical cast function, if you
can't use boost. You could start with something like this, you may want
to make this more robust,

template<typename out, typename in> out lexical_cast(const in &i)
{
std::stringstream ss;
ss << i;
out temp;
ss >> temp;
return temp;
}


then do

int x = 123;
std::string s = lexical_cast<std::string>(x);

you can also do the reverse

std::string s = "5678";
int z = lexical_cast<int>(s);

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      06-06-2006
Saurabh wrote:

> memset[myStr,'\0',50];


Take this line out of your code.

 
Reply With Quote
 
Prawit Chaivong
Guest
Posts: n/a
 
      06-06-2006

Old Wolf wrote:
> Saurabh wrote:
>
> > memset[myStr,'\0',50];

>
> Take this line out of your code.


Yep, Agree.
And use %d instead of %s.It makes your program think that your 'MyLong'
as a pointer to characters.

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      06-06-2006

Saurabh wrote:
> I have never used boost::lexical_cast.Is this an STL component?
> or are you hinting me to write my own version of sprintf()?
>
> Thanks & Regards
> saurabh


I would not suggest boost::lexical_cast here. The boost equivalent of
sprintf is boost::format.

lexical_cast is useful for converting the other way, i.e. string to
integer.

 
Reply With Quote
 
Noclambulist
Guest
Posts: n/a
 
      06-06-2006

Geo wrote:
> Saurabh wrote:
> > Geo wrote:
> > > Saurabh wrote:
> > > > Ian Collins wrote:
> > > > > Saurabh wrote:
> > > > > > Hi all,
> > > > > >
> > > > > > I am working on RedHat Linux GCC 3.0.
> > > > > > I am trying to convert a long to string through sprintf.
> > > > > > but i m getting segmantation fault.
> > > > > > I tried snprintf also but no avail.here is the piece of code..
> > > > > >
> > > > > > ----------------------------
> > > > > > long myLong=200000;
> > > > > > char myStr[50];
> > > > > > memset[myStr,'\0',50];
> > > > > > sprintf(myStr,"%s",myLong);
> > > > > ^
> > > > > %s is for printing (C style) strings.
> > > > >
> > > > > use %ld, or use iostreams.
> > > > >
> > > > > --
> > > > > Ian Collins.
> > > >
> > > > hi Ian,
> > > > Thanks a lot.
> > > > You solved my problem.
> > > > actually I thought that the second argument to
> > > > sprintf() refers to the format you wish to convert
> > > > to,instead it refers to the format you wish to
> > > > convert from.
> > > > anyways..
> > > > thanks again.
> > > > saurabh
> > >
> > >
> > > I would suggest that if you are STILL using sprinf, then you haven't
> > > fixed the problem, it will surely break one day. Why not use
> > > boost::lexical_cast (or you're own similar code) instead.

> >
> > hi Geo,
> > I have never used boost::lexical_cast.Is this an STL component?
> > or are you hinting me to write my own version of sprintf()?
> >
> > Thanks & Regards
> > saurabh

>
> Hi,
>
> No I was suggesting you write you're own lexical cast function, if you
> can't use boost. You could start with something like this, you may want
> to make this more robust,
>
> template<typename out, typename in> out lexical_cast(const in &i)
> {
> std::stringstream ss;
> ss << i;
> out temp;
> ss >> temp;
> return temp;
> }
>

but g++ told me that:
In function `out lexical_cast(const in&) [with out = std::string, in =
int]':
instantiated from here
error: `ss' has incomplete type
error: storage size of `ss' isn't known
>
> then do
>
> int x = 123;
> std::string s = lexical_cast<std::string>(x);
>
> you can also do the reverse
>
> std::string s = "5678";
> int z = lexical_cast<int>(s);


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
access of memory beyond allocation, not causing segmentation fault... sam_cit@yahoo.co.in C Programming 21 05-13-2007 11:18 AM
an array of pointers, causing segmentation fault Jess C++ 27 04-27-2007 06:20 PM
calling a function (passed as a pointer) causing segmentation fault sandwich_eater@hotmail.com C++ 1 07-22-2005 07:58 PM
multiple rb_require()s causing segmentation fault zarawesome@gmail.com Ruby 3 11-30-2004 08:54 PM
Segmantation fault with multithreading Dave C++ 0 06-25-2003 02:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57