ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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).
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.