C++ की Debugging Techniques
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Compiler Warnings
-Wall जैसे warning flags के साथ compile करना program चलने से पहले ही कई bugs catch कर लेता है, uninitialized variables, हमेशा true वाली comparisons, या unused values जैसे issues flag करते हुए।
उदाहरण: Compiler Warnings
// 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;
}
Login to try C/C++/Java/PHP code in the editor
assert() Use करना
<cassert> से assert() macro runtime पर check करता है कि एक condition true है, अगर यह कभी false हो तो एक clear error message के साथ तुरंत program terminate करते हुए, bugs को इनके source के जितना करीब हो सके catch करते हुए।
उदाहरण: Using assert()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Debugger से Trace करना
GDB जैसा एक debugger एक program को एक specific line (a breakpoint) पर pause होने देता है, इसके variables inspect किए जा सकते हैं, और execution एक बार में एक line step through किया जा सकता है, सिर्फ print statements से कहीं ज़्यादा insight देते हुए।
उदाहरण: Tracing with a Debugger
// 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;
}
Login to try C/C++/Java/PHP code in the editor
देखने लायक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- finished program में debug print statements छोड़ देना।
- compiler warnings ignore करना, जो अक्सर real bugs की ओर point करती हैं।
- real error handling के लिए
assert()पर depend करना, जब यहNDEBUGसे disable हो जाता है।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced