By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,796 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,796 IT Pros & Developers. It's quick & easy.

sprintf causing segmantation fault

P: n/a
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

Jun 5 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.
Jun 5 '06 #2

P: n/a

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

Jun 5 '06 #3

P: n/a
Geo

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.

Jun 5 '06 #4

P: n/a

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

Jun 5 '06 #5

P: n/a
Geo

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);

Jun 5 '06 #6

P: n/a
Saurabh wrote:
memset[myStr,'\0',50];


Take this line out of your code.

Jun 6 '06 #7

P: n/a

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.

Jun 6 '06 #8

P: n/a

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.

Jun 6 '06 #9

P: n/a

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);


Jun 6 '06 #10

P: n/a
Noclambulist <No**********@gmail.com> wrote:
Geo wrote:
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


You need #include <sstream> for std::stringstream.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 6 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.