473,972 Members | 49,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4211
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<typena me out, typename in> out lexical_cast(co nst in &i)
{
std::stringstre am ss;
ss << i;
out temp;
ss >> temp;
return temp;
}
then do

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

you can also do the reverse

std::string s = "5678";
int z = lexical_cast<in t>(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<typena me out, typename in> out lexical_cast(co nst in &i)
{
std::stringstre am ss;
ss << i;
out temp;
ss >> temp;
return temp;
}

but g++ told me that:
In function `out lexical_cast(co nst 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<st d::string>(x);

you can also do the reverse

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


Jun 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2758
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 or link option I should use when I compile/link a multithreading program with c++(GCC) under AIX 4.3. any suggestion and help will be appreciated. Thinks in advance.
1
1835
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 am doing wrong... typedef void TIdxMultiFunc (int i, std::vector<double> f); void TestFn(int i, std::vector<double> f) { double t; // execution does not get this far
26
4839
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: (void)sprintf(ptr_testing, "This is my string"); This in itself isn't where the actual error occurs. The error occurs at any later point, in the same function where the sprintf() call is made, where I try to assign a value to one of the...
9
3104
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 "stack and heap space". So I'm running in a very memory constricted environment. The program "speaks" a proprietary protocol which sends ASCII strings back and forth from the device to the server. Within the past couple of months we've been...
4
2020
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 the same position leads to a segmentation fault when the code is executed. Does anyone know what this could be ? Are segmentation fault errors always memory allocation errors ? Thanks
27
6748
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 namespace std;
23
1733
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
4066
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
14120
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 filetype to invoke the C compiler. Here's a link http://www.microsoft.com/express/download/#webInstall The download is 2.6 megs, which is near a reasonable size for a compiler, but then setup.exe wants to download 87 megs of dot net framework...
0
10346
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11805
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11399
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11555
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10900
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.