CC++C++17Introduction to C++Language FeatureLearn C++

How To Debug A C++ Program?

A C++ IDE and compiler are development tools for coding, preprocessing, compiling, assembly, linking, loading and running C/C++ programs. A C++ program building process involves these stages and on runtime there is also debugging option. So, what is this debugging? What is a bug? How can I debug my codes in C++ Builder? In this article, we’ll walk you through debugging, how an integrated debugger works, the difference between debug and release mode, how to debug your programs in a C++ example using the substr C++ method, and many more.

What is debugging and how to debug a C++ program?

Debugging is the process of finding and resolving bugs – unwanted or unplanned behavior – in your software applications or hardware systems. Bugs are defined as defects or problems that prevent the correct or intended operation in a running system or application.

Many programming languages and software development tools also offer programs to aid in debugging, known as debuggers. Many different Integrated Development Editors (IDEs) in C++ provide very professional methods to debug applications. Debugging involves interactive debugging, control flow analysis, unit testing, integration testing, log file analysis, monitoring at the application or system level, memory dumps, and profiling.

How To Debug A C++ Program An image of an artificial bug

RAD Studio, C++ Builder provides an integrated debugging environment that enables you to debug multi-device applications as well as native Win32 applications. In addition, you can use the debugger to debug an application running on a remote machine that does not have RAD Studio installed.

RAD Studio, C++ Builder includes integrated debuggers for every supported target platform. The IDE automatically uses the appropriate debugger based on the active project type. Debugging is supported for multi-device applications within a project and, where possible, the debuggers share a common user interface.

How to use debug a C++ Program with an integrated debugger ?

The integrated debuggers let you find and fix both run-time errors and logic errors in your RAD Studio C++ or Delphi application. Using the debuggers, you can step through code, set breakpoints and watches, and inspect and modify program values. As you debug your application, the debug windows are available to help you manage the debug session and provide information about the state of your application. There is a very detailed debugging methods list.

How to debug a C++ program in debug mode?

When applications that you develop can be compiled in two different types, Release and Debug mode. Debug mode is used when we develop application. Release mode is generally used only when we release and deploy the final executable application. A professional programmer mostly develops in debug mode although, for some big changes they will often swap to test the final application in release mode. Debug mode adds extra code and data to allow the debugger to do its work and map parts of the executable application’s internal runtime compiled output to lines of your program. It also includes extra checks and balances to help detect difficult to find bugs such as ‘stack overflow’ errors and similar. Debug mode programs are optimized to help make the debugging process as powerful and useful as possible. Release mode doesn’t include the extra data to make the debugger work and is optimized for size and pure execution speed to ensure the final release of your programs is as fast and efficient as possible.

All this means that a program compiled in Debug should not normally be the version you finally send to your users except under quite narrow sets of circumstances such as trying to get the customer to reproduce a bug or crash which will then display additional helpful information from the extra debug release’s information.

How to debug a C++ program in C++ Builder?

To debug your application first be sure that your application is set Build Configuration in Projects Window to Debug mode. This will allow you debug your application in details.

How To Debug A C++ Program The project manager dialog

Now, we can use debug configuration, we should just run with debugging mode by pressing this Run with Debugging button or press F9 key.

How To Debug A C++ Program Running your app in debug mode

Note that your projects options changes in Release and Debug configurations. Generally most options are same in Release and Debug configurations, although the two sets of project options – release and debug – can be completely different if you want them to be. Be sure that your project options are suitable to your debugging setup.

How to debug a C++ program with the Debug Inspector?

The Debug Inspector enables you to examine various data types such as arrays, classes, constants, functions, pointers, scalar variables, and interfaces. To use the Debug Inspector, select Run > Inspect.

This video demo of the debugger is a helpful overview.

  • Stepping – Step by Step Debugging Through Code
  • Evaluate/Modify – Investigate Expressions
  • Breakpoints – Pause and Check
  • Watches – Tracking Values
  • Exceptions – Displaying the stack trace

For example, you can debug this substr C++ example,

You can add break points to 11th and 15th lines as below, you can add s1 and s2 variables as string to the watchlist, then you can use step trace by using F8 key, and each step will show the value of s1 and s2 variables as below,

How to debug a C++ program a program stopped at a breakpoint in the IDE

RAD Studio comes with a lot of features to help with debugging, now let’s see all Debug Windows in RAD Studio, C++ Builder.

How to debug C++ program with RAD Studio Debug Windows?

Each window provides one or more right-click context menus. The F1 Help for each window provides detailed information about the window and the context menus.

Debug WindowDescription
Breakpoint ListDisplays all of the breakpoints currently set in the Code Editor or CPU window.
Call StackDisplays the current sequence of function calls.
Watch ListDisplays the current value of watch expressions based on the scope of the execution point.
Local VariablesDisplays the current function’s local variables, enabling you to monitor how your program updates the values of variables as the program runs.
ModulesDisplays processes under control of the debugger and the modules currently loaded by each process. It also provides a hierarchical view of the namespaces, classes, and methods used in the application.
Thread StatusDisplays the status of all processes and threads of execution that are executing in each application being debugged. This is helpful when debugging multi-threaded applications. For Windows Vista, the Threads Status includes a Wait Chain column that lists thread blockages and deadlocks.
Event LogDisplays messages that pertain to process control, breakpoints, output, threads, and module.
CPU Windows IndexDisplays the low-level state of your program, including the assembly instructions for each line of source code and the contents of certain registers.
FPUDisplays the contents of the Floating-point Unit and SSE registers in the CPU.

How to debug a C++ program with the Debug Desktop?

The Debug Desktop is the layout that the IDE uses when you are running your application in Debug mode (by pressing F9 in the default key mapping). There is a default Debug Desktop, but you can alternatively select any of the saved desktops to be the Debug Desktop.

How to debug a C++ program the debug desktop view menu

To set the Debug desktop:

  1. Choose either of the following:
  2. In the Set Debug Desktop dialog box, click the down-arrow and select the layout you want to be the Debug Desktop.
  3. Click OK.

How to debug a C++ program with remote debugging?

Remote debugging lets you debug an application running on a remote computer – a different one to the computer on which you are running RAD Studio. Your computer must be connected to the remote computer through a TCP/IP network connection.

Two remote debugging solutions are now available:

  • The new remote debugging solution is also used for multi-device debugging.
  • The old-style remote debugging solution required that you install a remote debugger on the remote machine. After you create and copy the required application files to the remote computer, you can connect to the remote computer and begin debugging.

Note: The old-style debugging solution does not apply to multi-device applications.

How to debug C++ program using Multi-Device debugging?

Multi-device debugging involves the use of a target platform that is not the native 32-bit Windows. Your interaction with the debugger is virtually the same with multi-device debugging as it is with Windows debugging.

How to debug macOS, iOS and Android mobile C++ program apps?

  • For the macOS and iOS platforms, you must install and run the Platform Assistant on the target machine and create a working connection profile on the development system.
  • For Android applications, you do not use the Platform Assistant or a connection profile, but you must download the USB driver for your Android device, enable USB debugging, and add an Android SDK to RAD Studio.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?