sprintf causing segmantation fault

Discussion in 'C++' started by Saurabh, Jun 5, 2006.

  1. Saurabh

    Saurabh Guest

    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
     
    Saurabh, Jun 5, 2006
    #1
    1. Advertising

  2. Saurabh

    Ian Collins Guest

    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.
     
    Ian Collins, Jun 5, 2006
    #2
    1. Advertising

  3. Saurabh

    Saurabh Guest

    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
     
    Saurabh, Jun 5, 2006
    #3
  4. Saurabh

    Geo Guest

    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.
     
    Geo, Jun 5, 2006
    #4
  5. Saurabh

    Saurabh Guest

    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
     
    Saurabh, Jun 5, 2006
    #5
  6. Saurabh

    Geo Guest

    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);
     
    Geo, Jun 5, 2006
    #6
  7. Saurabh

    Old Wolf Guest

    Saurabh wrote:

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


    Take this line out of your code.
     
    Old Wolf, Jun 6, 2006
    #7
  8. 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.
     
    Prawit Chaivong, Jun 6, 2006
    #8
  9. Saurabh

    Earl Purple Guest

    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.
     
    Earl Purple, Jun 6, 2006
    #9
  10. Saurabh

    Noclambulist Guest

    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);
     
    Noclambulist, Jun 6, 2006
    #10
  11. Saurabh

    Marcus Kwok Guest

    Noclambulist <> 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
     
    Marcus Kwok, Jun 6, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    0
    Views:
    864
  2. Replies:
    1
    Views:
    427
  3. Jess
    Replies:
    27
    Views:
    801
    Default User
    Apr 27, 2007
  4. Replies:
    21
    Views:
    909
    Peter 'Shaggy' Haywood
    May 13, 2007
  5. Replies:
    3
    Views:
    238
Loading...

Share This Page