Thread: C++ Code Error?
View Single Post
  #7   Spotlight this post!  
Unread 19-01-2003, 15:35
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Quote:
Originally posted by Adam Shapiro

Sample:
char *display;
long number=30001;
ltoa(number,display,5);
MessageBox(hWnd,display,"test",MB_OK);
-->CRASH!!
Adam,

Same problem. "char *display" allocates only enough memory to hold the pointer. "char" tells the compiler what kind of data the pointer points to (for type checking). "*" tells the compiler to allocate a 32-bit word for the pointer. "display" is a pointer to the pointer that the compiler/linker uses internally. ltoa() blindly uses the address contained in display as the destination address for the move. The problem is that display probably doesn't point to a valid chunk of memory that you can use. So at run-time, the memory managment hardware in the processor will catch this and return a trap #13 (a/k/a GP fault) to windows. This is a very common mistake in c/c++ coding (yes, I've made this mistake more than a few times ).

Replace the "char *display;" line to "char display[sizeof(long)*8+1];" and it should work.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org