473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

sprintf causing segmantation fault

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
10 4163
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

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

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
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
Saurabh wrote:
memset[myStr,'\0',50];


Take this line out of your code.

Jun 6 '06 #7

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave | last post by:
I have a multithrading program(daemon server) written by c++(GCC) under AIX4.X, I get segmentation fault problem when it runs several hours with heavy load. I want to know which compilation option...
1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
26
by: steve | last post by:
Well I've been working all morning and have finally found the source of my "bus error (signal 10)" errors. The source is odd. The error occurs in any function where I make the function call: ...
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
4
by: dschulenburg | last post by:
I have a function that runs, but gets a segmentation fault if I make very minor changes. For example adding a print statment somwhere into the function works, adding the same print statment twice at...
27
by: Jess | last post by:
Hello, the following code failed with "segmentation fault". Could someone tell me why it failed please? Thanks! #include<iostream> #include<string> #include<cstring> #include<cstddef> using...
23
by: sam_cit | last post by:
Hi Everyone, I have the following program unit, #include <stdlib.h> int main() { char *p = (char*)malloc(100); if(p==NULL)
1
by: Olaf | last post by:
Hi, does exist a safe version/way of sprintf to prevent a buffer overflow by using in this manner? char* format = "0x%0.4X\n"; char buf; sprintf(buf, format, number);
173
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.