PDA

View Full Version : Visual C++ question (not OpenGl related, sorry)



Kranomano
02-04-2001, 02:55 PM
Could someone help me out here? In VC++ 6.0, in some apps im making, GL or not, itoa crashes whenever called! For instance, if I were to compile and run the following in one such project:

char *num = itoa(50, NULL, 10);it would just lock up, in both debug and release builds. Any idea whats going on?

lpVoid
02-04-2001, 05:42 PM
try this....

char num[256];
itoa( 50, num, 10 );

If you absolutely need a pointer...

char *pNum = num;
printf( "%s\n", pNum );

Somewhat of a workaround instead of a solution, but maybe it helps....

Dave

[This message has been edited by lpVoid (edited 02-04-2001).]

02-04-2001, 07:27 PM
I believe in itoa that you must have the appropriate space in the buffer to hold the converted string... so passing null is very bad..... instead try this

char *num = new char[256]; //this is not necessarily the best way

itoa(50, num, 10);

or better

char num[256];
itoa(50, num, 10);

256 was picked arbitrailly... use whatever you need .....

the reason the 2nd is better is because dynamic mem is expensive...

Kranomano
02-06-2001, 01:10 PM
Ok. char *whatever = itoa(num, NULL, 10) is what ive used since the beginning of time without trouble... guess I'll switch. Thanks.

Michael Steinberg
02-06-2001, 01:43 PM
What should itoa be doing? If you pass a pointer to it, it can't allocate memory for it. The only possibility would be that you pass a pointer to a pointer.

02-06-2001, 06:59 PM
itoa converts an integer to a string

Deiussum
02-07-2001, 05:49 AM
Originally posted by Kranomano:
Ok. char *whatever = itoa(num, NULL, 10) is what ive used since the beginning of time without trouble... guess I'll switch. Thanks.

Then you've been lucky since the beginning of time. itoa needs you to give it space to use. It does not dynamically allocate the memory for you and that's what it would have to do if you were to use it that way. Think about it for a second. If you didn't specify the memory, where in memory is it going to set that pointer to point to?

One of the keys to mastering C/C++ is understanding how memory is used.

Kranomano
02-07-2001, 03:21 PM
Yeah I've been understanding that more and more lately. I've been programming with C++ for about 2 years now, on and off, and it hasnt been until recently that I've had to deal with memory managment. That's mainly because what I was doing before was a hell of a lot less advanced than OpenGL programming.