Board index » cppbuilder » itoa blowing up ? why?

itoa blowing up ? why?


2005-08-04 12:02:51 AM
cppbuilder9
i have a function:
[code]
char* getZipFileExtension(int number){
char *buf;
char *num;
itoa(number,buf,10);
//do some stuff with this....
}//end function
[/code]
i was testing it, and the itoa function blows up the second time
i call it. I enter the function the first time, everything works
OK, then when I come back the second time it blows up. Can
anyone tell me why this is happening? I would appreciate any help.
 
 

Re:itoa blowing up ? why?

Quote
char *buf;
...
itoa(number,buf,10);
'buf' is declared as a pointer to character, a variable which can hold the
address of an array of characters. However you did not assign the address
of an array of characters to hte variable. Therefore itoa is being asked to
write to a random address in memory. The result of doing that can be
unpleasant.
Try this instead:
char buf[33];
...
itoa(number,buf,10);
. Ed
Quote
peter wrote in message
news:42f0eaab$ XXXX@XXXXX.COM ...

i have a function:

[code]
char* getZipFileExtension(int number){
char *buf;
char *num;
itoa(number,buf,10);
//do some stuff with this....
}//end function
[/code]

i was testing it, and the itoa function blows up the
second time i call it. I enter the function the first time,
everything works OK, then when I come back the
second time it blows up. Can anyone tell me why this
is happening? I would appreciate any help.
 

Re:itoa blowing up ? why?

"peter" < XXXX@XXXXX.COM >wrote in message
Quote
i was testing it, and the itoa function blows up the second time i call
it.
You are writing to a buffer that has never been allocated at all, and the
pointer is not even intialized so it initially points to random memory. As
such, you will have undefined behavior for the rest of the process's
lifetime because you could be corrupting memory. The fact that sometimes
your code crashes and sometimes it doesn't is just a side effect of
undefined behavior, since it depends on which memory is being altered each
time.
Quote
I enter the function the first time, everything works
No, it will never work as you have written it. You need to change the code
to the following:
char* getZipFileExtension(int number)
{
char buf[12] = {0}; // <-- ALLOCATE STORAGE FOR THE BUFFER!!!
itoa(number, buf, 10);
// ....
}
Gambit
 

{smallsort}

Re:itoa blowing up ? why?

On 3 Aug 2005 09:02:51 -0700, peter wrote:
Quote
[code]
char* getZipFileExtension(int number){
char *buf;
char *num;
itoa(number,buf,10);
//do some stuff with this....
}//end function
[/code]
As several other people pointed out, you didn't allocate any space for
buf. Additionally, your function may well be returning a local
variable that will disappear any way
you could try something more like
char * getZipFileExtension(char * buf,int number)
{
itoa(number,buf,10)
return buf;
}
int main()
{
char buf[20];
getZipFileExtension(buf,3);
std::cout << buf << std::endl;
}
Although I think using boost::lexical_cast would be more sensible:
www.boost.org/libs/conversion/lexical_cast.htm
#pragma hdrstop
#include <iostreams>
#include <string>
#include <boost/lexical_cast.hpp>
#pragma argsused
int main(int argc, char* argv[])
{
std::string thing = boost::lexical_cast <std::string>(3);
std::cout << thing << std::endl;
return 0;
}
--
good luck,
liz