I'll try to answer your first five questions although I cannot guarantee that I will have time to answer the remaining 99.995 questions that will arise for you after adding further lines to your Glaurung learning project
One important note: my explanations are meant to be simple, but do not necessarily cover all aspects of the requested topics. To go deeper you will definitely need either some literature or "google".
Uri Blass wrote:
Code: Select all
int main(int argc, char *argv)
1)When I compile it I get warning C4995\6
'setbuf': This function or variable may be unsafe. Consider using setvbuf instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Does it mean that Glaurung is written wrongly or maybe I do not need to care about these warning or maybe there are not going to be warning with the full glaurung program.
1) add the -D_CRT_SECURE_NO_WARNINGS preprocessor switch to your project settings if you want to make these warnings go away without changing the existing code. With Visual Studio Express 2005 (that's what I expect you might be using, 2008 would be similar): "Project properties" -> "Configuration properties" -> "C/C++" -> "Preprocessor" -> "Preprocessor definitions" -> "..." -> add the string above without the preceding "-D" -> "Ok" as often as necessary.
When writing a new engine from scratch in C++, you may decide not to use these "old-style C functions" like setbuf, fprintf and so on if you want to be perfectly safe. But I think initially you should not bother with this at all, and go on with these "old" functions as usual.
2)I am used to use int main(void)
I read the following
"The argument argc is the number of command-line arguments passed to the program. The argument argv is a pointer to an array of strings, where argv is the name you used to run your program from the command-line"
What is the meaning of this command line and can you give me step by step instruction how to run a program from the command line?
Although I cannot really imagine that you never used a command line interpreter like "command.com" (MS-DOS), "cmd" (the "modern" command.com of newer Windows) or "bash" (CygWin, originally from UNIX), from your posting I assume you didn't. I further assume you have Windows XP on your PC.
Click "Start" -> "Execute", type "cmd" and you have a command prompt. Type for example "ping -n 10 localhost". This should start the program "ping.exe" (since "ping" is usually not a "builtin" command that the interpreter "cmd" knows by itself) and passes three strings as parameters to it: "-n", "10", and "localhost". The "ping.exe" program stores these parameters such that they can be accessed by its function "main". "argc" has the value 4 (program name and 3 parameters), and the "argv" pointer array points to these four strings that are null-terminated and do physically reside in memory that is owned by the internal startup code of "ping.exe" from where the entry point function "main()" is called.
The code of main() in "ping.exe" will then loop somehow through the argv array, will recognize that "-n" has been given as option, will expect an option argument for it that is given as "10", will now understand that it shall repeat the "ping" operation 10 times, and will finally see "localhost" as program argument that it shall use as target address where ping packets shall be sent to.
3)How can the computer understand setbuf?
When I click help on the word setbuf I find that stdio.h is required header but glaurung does not use stdio.h and it seems that <iostream> is enough to compile it.
The header file "iostream", at least in case of MSVC++ Express 2005, indirectly includes "cstdio" via several steps, which in turn serves as C++ equivalent to "stdio.h". But I would never rely on such possibly "accidental" include relationships, and prefer to explicitely write down all include statements that are required by the code of a given source file, so when using "setbuf" I would also #include <cstdio> or even #include <stdio.h>. But that's not really a critical issue, one can spend very much time on such problems without getting it even close to perfect.
4)I am not sure if I understand exactly what setbuf does except the fact that it does some cleaning to the standard input and standard output files.
I guess that the target is that the program will get correct information when it read later some input from the interface or from the user and will forget some information that the user gave before starting to run the program but I am not sure.
An example when things do not work and explanation why they do not work can be productive.
setbuf(stdin, NULL) makes the standard input of the current process unbuffered. So when you read a line, say with fgets(), you immediately get an input line when it's available. Otherwise it could happen that input is collected in a buffer which will only be flushed (and then received by the reading function) when it is "full enough". In case of chess engine play, this could mean that you receive a command from the GUI at the wrong time (too late).
setbuf(stdout, NULL) does the same in the output direction. When you write a line it will immediately be put to the standard output. Without this call, lines will be buffered and "sometimes" the buffer will be flushed. In case of chess engine play, this could mean that the GUI receives a command sent by your engine at the wrong time (too late).
For testing and debugging purposes, and/or when the engine is running in console mode, it may be good to keep buffered output as default in case you write much output in a short time. In this case you must use fflush(stdout) after each sending of important commands to the GUI, like sending the engine's move.
5)I do not understand what is the purpose of the next lines of std:: with rdbuf and pubsetbuf
I do not use these commands and it can be productive if somebody explain to me what they are doing.
These do essentially the same as setbuf(stdin, NULL) resp. setbuf(stdout, NULL). But I think they are necessary only if a program uses the C++ input/output style that is based on stream objects (e.g. using "cin" or "cout").