April 7th, 2003, 11:00 PM
Program only runs in the debugger?
I don't know about the experience of the forums here nor do I know if there is some simple reason people would know why a program only runs from the MSVC debugger.
Just to clarify, this program is a "Game Demo" that is for a college class, using DirectX 9 (Hoping to get into the HLSL soon) I havent gone quite to the point of putting in OutputDebugString("Got this far!\n"); all throughout the program yet. A little more background if you are still reading. The way it works is to initialize D3D, DSound, and DInput, then it initializes the "Game State" currently jumping right into the "Playing State" , which causes it to load all the graphics, etc, then it draws the world, and allows character interaction etc. After initializing that, it jumps right into the message pump, with the last step in the loop being to update the game state. The breakdown occurs after loading the graphics. I was hoping this would be a simple oh it runs in the debugger but not without it's probably xy or z setting. If not, I guess its on to OutputDebugString("blah\n"); and sysinternals debug string catcher...
Thanks in advance
April 8th, 2003, 12:19 AM
Well, what can I say I am impatient. I went through and did lots and I mean LOTS of OutputDebugString("I am here!\n"); and char Debug; sprintf(Debug, "Relevent pointers are %d and %d and %d\n", pointer1, pointer2, pointer 3); OutputDebugString(Debug);
EAventually it turned out to be an array bounds check by a really stupid piece of code on my part. I am using a simple rect algorithm to determine which "tiles" of the world are visible. If they are not visible I set their verticies all to 0,0, then not draw them. The algorithm function returns an integer array with visible tiles in an order that works in the program. If a particular tile is not visible its number is -1. Well, I was trying to set the -1 tile to a 0,0 rect.
So I take it that the msvc debugger has an array bounds check feature built in?
Also, now that I wrote tons of those statements mentioned above, I would like my own singular function like printf to output debug strings. I looked at the function definition to sprintf and it was and I quote sprintf(char*, const char*, ...) So whats with the ... they dont teach that in any C/C++ courses! Can I use that to make a say dprintf for debugprintf? How do I pull in the varibles/data in the ...?
April 8th, 2003, 11:08 AM
I don't quite understand the question.
The printf() family of functions create a formatted string and then do something with it. printf writes the string to stdout. fprintf writes it to a file.
sprintf stores it in a char array. The first parameter (char*) points to the char array that is to receive the formatted string. The second parameter (const char*) is the format string which contains format variables to mark where the variable values go and what type they are to be. Then a variable-length/type list of variables follows.
sprintf(buffer,"The value of %s at line %d is %d\n",var_name,line_no,var);
%s marks a string, so var_name needs to be a char*.
%d marks a decimal value, so line_no and var need to be ints.
\n means "end of line"
OK, I see now. The ellipses ("...") refer to a variable-length list of variable names that are to be used in the format string.
No data type is specified for those variables, BTW, because their data type is implicitly defined by their corresponding format variables. This leads to interesting bugs, since if you use the wrong type of format variable, then that variable will be interpreted as that data type. Debug hint: if the program seems to go through the motions alright but gives you really wacko output, then double-check the format variables.
This is an integral part of the printf family of functions, which are an integral part of C and is taught in every C class that I've ever heard of. In fact, the sprintf function was what really won me over from Pascal, where formatted output had to be done with multiple lines of code.
You probably haven't encountered it yet in C++, because they use iostreams to "protect" you from printf. Personally, I think we need protection from iostreams.
There are ways in C to write functions with variable-length argument lists, but I was only exposed to that once in class about twelve years ago.
You could use sprintf everywhere you need it, or use C++'s function overloading feature to create a set of functions by the same name but with the different combinations of parameters that you need. Within each function, do an sprintf specific to that combination of parameters and then make the call to display it.
Not an elegant solution, but some of the functions should get used more than once.
Last edited by dwise1_aol; April 8th, 2003 at 11:20 AM.
April 8th, 2003, 11:24 AM
The format variable for pointers is %p, which will display the memory address assigned to the pointer.