LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2006, 11:51 PM   #1
syseeker
Member
 
Registered: Aug 2003
Posts: 66

Rep: Reputation: 15
sprintf value disappear


Hi,

I would like to convert two integer variables into string (contiguously)...So I did

char strA[1], strB[1];
sprintf(strA, "%u", intA);
sprintf(strB, "%u", intB);

printf("StrA = %s", strA); //debug
printf("StrB = %s", strB); //debug

Notice that value of strA becomes nothing, but strB has the correct value(intB). How should I overcome this problem?

Thanks
 
Old 02-16-2006, 12:10 AM   #2
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
Originally Posted by syseeker
Hi,

I would like to convert two integer variables into string (contiguously)...So I did

char strA[1], strB[1]; /* char strA[5], strB[5]; ??? */
sprintf(strA, "%u", intA);
sprintf(strB, "%u", intB);

printf("StrA = %s", strA); //debug
printf("StrB = %s", strB); //debug

Notice that value of strA becomes nothing, but strB has the correct value(intB). How should I overcome this problem?

Thanks
Cheers,
GH.
 
Old 02-16-2006, 12:14 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by syseeker
char strA[1], strB[1];
You need a longer character array. A C-style string of length n is n characters terminated by the NULL character (total n+1 characters); so allocating just 1 character isn't enough.

(nevermind)
 
Old 02-16-2006, 12:35 AM   #4
syseeker
Member
 
Registered: Aug 2003
Posts: 66

Original Poster
Rep: Reputation: 15
Hmmm.... I solved the problem by creating a func , refer from http://www.eskimo.com/~scs/cclass/int/sx5.html

char *itoa(int n)
{
char *retbuf = malloc(25);
if(retbuf == NULL)
return NULL;
sprintf(retbuf, "%d", n);
return retbuf;
}


Thanks for your prompt reply!
 
Old 02-16-2006, 01:20 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
OK -

1. You realize the original problem was that you weren't declaring a buffer large enough to hold the correct value, don't you?

2. It's worth pointing out that the reason you saw strB (but not strA), was that strB was *corrupting* strA because of the buffer overflow. The write to strA was undoubtedly corrupting something, too - you just didn't happen to see it. Both instances were "bad".

3. The cleanest solution is arguably to do exactly what spooon and freegianghu suggested - simply declare a larger buffer. If you declare them as local variables ("e.g. char strA[5], strB[5]"), then the size is right there in front of you, and cleanup is automatic.

4. Is "strA[5]" big enough? Only if you're sure the number isn't going to exceed four digits (0..9999).

5. What's wrong with your "*itoa()"?

It works (you can't argue with success ;-)).

But I would object to three things:
a) Why "malloc(25)"? What 32-bit "int" value is going to take 24 digits?

b) If you "malloc()" the data, you need to be sure that *somebody* does
a corresponding "free()". Otherwise you've got a memory leak.

That's one of the benefits C++ classes have over C functions: you can
encapsulate that kind of "protocol" entirely within the class itself.

c) Finally, the system already *has* a function "itoa()".
If it's got the same semantics as your function, why not just use the library?
If it's got different semantics, why don't you name it something else?

IMHO .. PSM
 
Old 02-16-2006, 04:54 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
good points.
advice:
don't mess about with malloc until you really need to, or know what you are doing.
why do people allocate such small buffers?

if you are playing with this sort of stuff don't mess about
why not char buffer[1024]?
how many MB have you got?
it saves all sorts of grief.
 
Old 02-16-2006, 05:47 AM   #7
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Finally, the system already *has* a function "itoa()".
Where is it (what header)?

Quote:
why not char buffer[1024]?
Because it doesn't make sense? If you need an int-array with 20 elements, do you write:
int a[ 1024 ];
(I hope you don't!)
Even an unsigned 64-bit integer needs at most 20 decimal digits (plus sign, if desired), so
char buffer[ 22 ];
is enough, even on systems that treat int as 64-bit (Linux and Windows don't).
 
Old 02-16-2006, 05:53 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Because it doesn't make sense?
Does it hurt?
How many MB on the average PC?
Are you writing a kernel module or just messing about playing with char buffers?
It helps save beginner programmers continually core dumping.
 
Old 02-17-2006, 04:20 AM   #9
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by bigearsbilly
Does it hurt?
How many MB on the average PC?
I does hurt if you do that kind of stuff in a recursive function; it's simply a senseless waste of memory, no matter how much memory you've got.
Further, if someone reads your code, he will think: "Why does he reserve so much memory? There is probably a reason for this, so did I miss something?"

Quote:
It helps save beginner programmers continually core dumping.
Even beginners can use their minds; they know that a 32-bit number doesn't need 1023 decimal digits. Teaching them to reserve "more memory than needed, just to be sure" does not lead to safer programs. Instead, it leads to a "don't think, use your intuition"-attitude. I suppose you agree that this is very bad.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
programming-segmentation fault(sprintf) ramakrishna sura Linux - General 1 12-20-2005 04:12 AM
IP disappear after reboot... frankpretec Linux - Networking 1 04-07-2005 01:37 PM
will everything disappear, when i install? iurodivii Linux - Newbie 6 01-29-2005 03:25 AM
sprintf not working in char driver module snigglyfox Programming 2 04-27-2004 01:25 PM
Why do some commands disappear??? mandelchan Linux - General 8 04-22-2004 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration