RE: [Dev-C++] itoa - atoi : another implementation
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-04-02 21:34:46
|
See below. /Per W On Wed, 2 Apr 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: Per Westermark [mailto:pw...@ia...] > > Sent: Wednesday, April 02, 2003 10:13 PM > > To: Ioannis Vranos > > Cc: 'Dev-C++' > > Subject: RE: [Dev-C++] itoa - atoi : another implementation > > > > > > You might have been in even more hurry :-) > > > > How many characters do you allocate if the integer was zero, > > or negative? > > > > Yes you are right, I forgot the <=0 stuff. Well I leave it as an exercise > for home. :) > > > > > > > And then of course - itoa() with a sprintf() is of very > > little use, since > > it doesn't solve reentrancy problems, > > > What do you mean. > All environments doesn't have a reentrant sprintf() that you might call from an ISR. > > > > doesn't support variable number > > base, > > > No it doesn't. > > > > and doesn't allow linking without the formatted RTL code. > > > What do you mean. The implementation of sprintf() is normally very large. On an environment that links clib static, check size of binary with use of itoa() with static allocation or buffer as input param and compare it with sprintf() and you'll see the difference. In a lot of embedded environments, itoa() is king, because of that. If a microcontroller have 8kB program area, itoa() consumes 100 byte and sprintf() consumes 2kB, the choice is quite easy. > > > > > It isn't even a good optimization of number of source code > > characters, since each call MUST be either: > > free(itoa(i1)); > > which is an expensive NOP instruction, or: > > > It is supposed to be used where free store usage is affordable (almost > everywhere these days). No specific constraint requirements were provided by > the OP. > > > > > char *p = itoa(i1); > > use_string(p); > > free(p); > > which isn't much shorter than > > char buf[20]; > > sprintf(buf,"%d",i1); > > use_string(buf); > > > > Yes it isn't. That's why I said itoa() is not needed. > > > > > > Whith a static buffer in itoa() you get code length improvement > > with: > > use_string(itoa(i1)); > > > The above is not thread safe (since as I saw you try to embarrass me). :) > You must place a locking mechanism. That's why most implementations of itoa takes a buffer as second parameter. > > > > and with a semi-standard implementation of itoa you get code length > > improvement AND extra number bases: > > char buf[30]; > > use_string(itoa(i1,buf,3)); // trinary output > > > I do not know any standard or semi-standard impleentation of itoa(). Even in > K&R2 it is not what you are talking about. A number of impelemntations of itoa: Developer Studio: char *_itoa(int value, char *string, int radix); note: M$ adds the leading _ on a lot of functions, such as _open(), _getcwd(), ... QNX (www.qnx.com): char* itoa(int value, char* buffer, int radix); www.cplusplus.com/ref/cstdlib/itoa.html: char* itoa(int value,char* buffer,int radix); have note: Not defined in ANSI-C. Supported by some compilers. http://developer.novell.com/ndk/doc/libc/index.html?page=/ndk/doc/libc/libc_enu/data/sdk959.html char *itoa(int value,char *buffer,int radix); note: They classifies it as ANSI www.uclibc.org (Small open clib for embedded systems) char *itoa(int i); note: uses static buffer > > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL: http://run.to/noicys > > > > > ------------------------------------------------------- > This SF.net email is sponsored by: ValueWeb: > Dedicated Hosting for just $79/mo with 500 GB of bandwidth! > No other company gives more support or power for your dedicated server > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |