← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 3 of 17

C++ की Debugging Techniques

एक C++ program debug करना print-statement tracing, compiler warnings, assert() checks, और GDB जैसे proper debugger को combine करता है unexpected behavior की वजह locate और समझने के लिए।
Syntax
cpp
#include <cassert>

assert(condition);   // aborts with an error if condition is false

std::cout << "variable = " << variable << std::endl;

Print-Statement Debugging

सबसे simple debugging technique code में key points पर temporary cout statements insert करना है variable values print करने और confirm करने के लिए कि logic की कौन-सी branches actually execute होती हैं।

उदाहरण: Print-Statement Debugging

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print to the console with cout
	std::cout << "x is: " << x << std::endl;
	// Check whether x > 0
	if (x > 0) {
		// Print to the console with cout
		std::cout << "Entered positive branch" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Compiler Warnings

-Wall जैसे warning flags के साथ compile करना program चलने से पहले ही कई bugs catch कर लेता है, uninitialized variables, हमेशा true वाली comparisons, या unused values जैसे issues flag करते हुए।

उदाहरण: Compiler Warnings

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int x;
	x = 5;
	// Print to the console with cout
	std::cout << x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

assert() Use करना

<cassert> से assert() macro runtime पर check करता है कि एक condition true है, अगर यह कभी false हो तो एक clear error message के साथ तुरंत program terminate करते हुए, bugs को इनके source के जितना करीब हो सके catch करते हुए।

उदाहरण: Using assert()

cpp
// Include std::cout and std::cin
#include <iostream>
#include <cassert>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	assert(age >= 0);
	// Print to the console with cout
	std::cout << "Assertion passed" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Debugger से Trace करना

GDB जैसा एक debugger एक program को एक specific line (a breakpoint) पर pause होने देता है, इसके variables inspect किए जा सकते हैं, और execution एक बार में एक line step through किया जा सकता है, सिर्फ print statements से कहीं ज़्यादा insight देते हुए।

उदाहरण: Tracing with a Debugger

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare total and set it to 0
	int total = 0;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		total += i;
	}
	// Print to the console with cout
	std::cout << total << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

देखने लायक Common Bug Categories

Common C++ bugs में loops में off-by-one errors, uninitialized variables, delete के बाद dangling pointers, और exact equality के लिए floating-point numbers compare करना शामिल है, debug करते वक्त पहले check करने लायक सब।

उदाहरण: Common Bug Categories to Watch For

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print to the console with cout
		std::cout << arr[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. finished program में debug print statements छोड़ देना।
  2. compiler warnings ignore करना, जो अक्सर real bugs की ओर point करती हैं।
  3. real error handling के लिए assert() पर depend करना, जब यह NDEBUG से disable हो जाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.