itoa

M

Mark

Gavin said:
Mark said:
well, ok, i could use std::strings, but printf won't accept them, so i
have to convert them where ever i want to use them, right?

Not if you do you input and output using streams (like cin and cout) -
see below.

i've always tried to advoid std:strings and other libraries i didn't
program myself... always thinking there is too much baggage, or they
aren't compatible with other functions and such.

The facilities of the C++ standard library (strings, streams,
containers, algorithms, etc.) are fully compatible with each other by
design and provide commonly needed functionality out of the box.

running this quick test,

#include <cstdlib>
#include <iostream>

using namespace std;

char * itoa(int i)
{
static char buff[35];
return _itoa(i,buff,10);
}

int main(int argc, char *argv[])
{
printf("%s %s %s", itoa(1), itoa(2), itoa(3));
system("PAUSE");
return EXIT_SUCCESS;
}

it prints "1 1 1"

now if i'm not mistaken, since the buffer is in the same memory
location each time the function is called (because it's static)... all
3 pointers should point to the same location in memory, which is
probably why they are printing the same thing.

now what i'm thinking really happens, is that itoa is called 3 times,
the pointers are returned, and THEN the "%s %s %s" is evaluated, using
what happens to be the same pointer, rather than evaluating each %s and
corresponding parameter at the same time....

which all makes very good sense to me now. i feel clever for figuring
that out .. i hope i'm right :D

Yes you are right. All the arguments to a function are fully evaluated
_before_ the function itself is called. Parsing "%s %s %s" and
inserting the character sequences obtained by dereferencing the pointer
arguments happens _inside_ the printf function, so by the time it
happens, the three itoa calls are complete.

So what actually gets printed is whatever was stored in the buffer by
the last of the three calls to itoa. In your case, the last call must
have been itoa(1). That's allowed - the compiler does not have to
evaluate the function arguments in the order they apppear in your code.
It can evaluate them in any order it likes.
are there any other print functions that actually take std:strings as
parameters?
or is there a way i could do "my string" + 5 + my_other_string?

#include <string>
#include <iostream>

int main()
{
std::string my_other_string = "my other string";
std::cout << "my string" << 5 << my_other_string << "\n";

return 0;
}

Gavin Deane

Thanks Gavin :)

Nice to know some people are a little more polite.
 
D

davidrubin

Baxter said:
Try:

char * itoa(int i)
{
static char buff[35];
return _itoa(i,buff,10);
}

We routinely ask this question as part of the interview process at my
company, and this approach is well below par. Firstly, '_itoa' is not
standard. However, if you are referring to '_itoa' as defined on MSDN
(http://msdn.microsoft.com/library/d...i64toa.2c_._itow.2c_._i64tow.2c_._ui64tow.asp)
the second comment is that the function is not built for testability.
For example,

Return Value
Each of these functions [_itoa] returns a pointer to string.
There is no error return.

How can you prove the correctness of a function that does not indicate
error conditions? Lastly, the function is not thread-safe.

A good starting point is

int itoa(char *buffer, std::size_t length, int value);
// Store into the specified 'buffer' of the specified 'length'
// the ASCII representation of the specified 'value'.
// Return 0 on success, and a non-zero value otherwise.
// In particular, return a non-zero value if 'length' is not
// large enough to represent 'value'.

This contract allows for a wide variety of black-box and white-box
testing. Now, how would you implement this?
 
D

Dietmar Schindler

Mark said:
running this quick test,

#include <cstdlib>
#include <iostream>

using namespace std;

char * itoa(int i)
{
static char buff[35];
return _itoa(i,buff,10);

static char b, buff[3][35];
sprintf(buff[b%=3], "%i", i);
return buff[b++];
}

int main(int argc, char *argv[])
{
printf("%s %s %s", itoa(1), itoa(2), itoa(3));
system("PAUSE");
return EXIT_SUCCESS;
}

it prints "1 1 1"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top