 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
peter Guest
|
Posted: Wed Aug 03, 2005 4:02 pm Post subject: itoa blowing up ? why? |
|
|
i have a function:
| Code: |
char* getZipFileExtension(int number){
char *buf;
char *num;
itoa(number,buf,10);
//do some stuff with this....
}//end function
|
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.
|
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Wed Aug 03, 2005 4:29 pm Post subject: 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$1 (AT) newsgroups (DOT) borland.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
|
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.
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Aug 03, 2005 5:44 pm Post subject: Re: itoa blowing up ? why? |
|
|
"peter" <pandrus (AT) attglobal (DOT) net> wrote
| 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
|
|
| Back to top |
|
 |
Liz Albin Guest
|
Posted: Wed Aug 03, 2005 7:11 pm Post subject: 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
|
|
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:
http://www.boost.org/libs/conversion/lexical_cast.htm
#pragma hdrstop
#include
#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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|