how to debug c++ program before main()
set a break at _start()
- 'start'
- The name of the main procedure can vary from language to language. With C or C++, the main procedure name is always main, but other languages such as Ada do not require a specific name for their main procedure. The debugger provides a convenient way to start the execution of the program and to stop at the beginning of the main procedure, depending on the language used.
- The ‘start’ command does the equivalent of setting a temporary breakpoint at the beginning of the main procedure and then invoking the ‘run’ command.
- Some programs contain an elaboration phase where some startup code is executed before the main procedure is called. This depends on the languages used to write your program. In C++, for instance, constructors for static and global objects are executed before main is called. It is therefore possible that the debugger stops before reaching the main procedure. However, the temporary breakpoint will remain to halt execution.
- Specify the arguments to give to your program as arguments to the ‘start’ command. These arguments will be given verbatim to the underlying ‘run’ command. Note that the same arguments will be reused if no argument is provided during subsequent calls to ‘start’ or ‘run’.
- It is sometimes necessary to debug the program during elaboration. In these cases, using the start command would stop the execution of your program too late, as the program would have already completed the elaboration phase. Under these circumstances, insert breakpoints in your elaboration code before running your program
Dynamic Memory Management : Quick Reference
In C++, the new() and delete() operators provide built-in language support for dynamic memory allocation and deallocation. This feature has several benefits:- – Reduces common programmer errors: it is easy to forget to multiply the number of objects being allocated by sizeof when using malloc(), e.g., // oops, only 2 1/2 int’s! int *a = (int *) malloc (10);
- – Enhances source code clarity: generally, there is no need to: (1) declare operator new() and delete(), (2) explicitly use casts, or (3) explicitly check the return value.
- – Improves run-time efficiency: (1) users can redefine operator new() and delete() globally and also define them on a perclass basis and (2) calls can be inlined.