473,320 Members | 1,857 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,320 software developers and data experts.

Sprintf memory leak ?

41
Hi,

I am using sprintf like so where i want my numeric to be converted to a string
char string[100];
int result;
ret = sprintf(string, "%d", result);

does any one know if this will cause a memory leak ????


Another question.

is I have the following scenario -1234 is a numeric
I want to convert this to a string like so "-1234"

What would be the best c function to do this ????
Jan 19 '07 #1
8 8162
willakawill
1,646 1GB
Hi,

I am using sprintf like so where i want my numeric to be converted to a string
char string[100];
int result;
ret = sprintf(string, "%d", result);

does any one know if this will cause a memory leak ????


Another question.

is I have the following scenario -1234 is a numeric
I want to convert this to a string like so "-1234"

What would be the best c function to do this ????
hi you are unlikely to create a memory leak with this function unless you are using an integer with over 99 digits (which is impossible)
In MS Visual c++ you can use a newer version, sprintf_s() which avoids overflow by including an extra parameter, the maximum characters to be written:
Expand|Select|Wrap|Line Numbers
  1. sprintf_s(string, 100, "%d", result);
sprintf() will work for positive or negative numbers so:
Expand|Select|Wrap|Line Numbers
  1. sprintf(string, "%d", -1234);
will work
Jan 19 '07 #2
nano2
41
hi you are unlikely to create a memory leak with this function unless you are using an integer with over 99 digits (which is impossible)
In MS Visual c++ you can use a newer version, sprintf_s() which avoids overflow by including an extra parameter, the maximum characters to be written:
Expand|Select|Wrap|Line Numbers
  1. sprintf_s(string, 100, "%d", result);
sprintf() will work for positive or negative numbers so:
Expand|Select|Wrap|Line Numbers
  1. sprintf(string, "%d", -1234);
will work
Thanks fro that information..

Just one thing I want to append the minus sign onto the contents thats stored within the result variable.

so for eg. result = 1234.5
I want to have the following o/p " -1234.5" i.e the minus before the value ..
Any idea how i can achieve this
????????????
Jan 22 '07 #3
horace1
1,510 Expert 1GB
Thanks fro that information..

Just one thing I want to append the minus sign onto the contents thats stored within the result variable.

so for eg. result = 1234.5
I want to have the following o/p " -1234.5" i.e the minus before the value ..
Any idea how i can achieve this
????????????
spintf() will convert the sign as well as the numeric value, e.g. if you have code so
Expand|Select|Wrap|Line Numbers
  1.     char string[100];
  2.     int result =-1234;
  3.     int ret = sprintf(string, "%d", result);
  4.  
string will contain "-1234"
Jan 22 '07 #4
Banfa
9,065 Expert Mod 8TB
Thanks fro that information..

Just one thing I want to append the minus sign onto the contents thats stored within the result variable.

so for eg. result = 1234.5
I want to have the following o/p " -1234.5" i.e the minus before the value ..
Any idea how i can achieve this
????????????
Either invert the value of the variable before using sprintf i.e.

result = -result;

or artifcially add a minus sign to the format string i.e.

sprintf(string, "-%d", result);

although this will only work for positive values of result negative values will result in 2 '-' signs in the output.


Also note in you initial post you define result as an int, however in this post you have assigned 1234.5 to it which is not an integer value.
Jan 22 '07 #5
nano2
41
Either invert the value of the variable before using sprintf i.e.

result = -result;

or artifcially add a minus sign to the format string i.e.

sprintf(string, "-%d", result);

although this will only work for positive values of result negative values will result in 2 '-' signs in the output.


Also note in you initial post you define result as an int, however in this post you have assigned 1234.5 to it which is not an integer value.
Thanks for your help
if i use sprintf(string,"-%lf",result)

for values like so 123 , 23214.23124 , 2414.2
Will this cause meomy leaks do you know ?

Thanks
Jan 22 '07 #6
nano2
41
Can sprintf be used for long floats ?

eg result = 4.999999
sprintf(str, "-%lf", result);
Jan 23 '07 #7
LSB
8
Thanks for your help
if i use sprintf(string,"-%lf",result)

for values like so 123 , 23214.23124 , 2414.2
Will this cause meomy leaks do you know ?

Thanks
sprintf will NOT cause memory leak if you will use it to convert regular C data types. No way. :)
If you already have some memory leak, try to find another source of that.
Jan 23 '07 #8
LSB
8
Can sprintf be used for long floats ?

eg result = 4.999999
sprintf(str, "-%lf", result);
For Microsoft's compiler it will be:
f -- double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
For e or E you will have exactly 3 digits after the decimal point.
They also have g and G format, described as being in fact
f or e format, whichever is more compact for the given value and precision
Jan 23 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

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: ...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.