কম্পিউটার

কিভাবে C &C++ এ সেগমেন্টেশন ত্রুটি খুঁজে পাবেন? (GDB ব্যবহার করে)


বিভাজন ত্রুটি রানটাইম ত্রুটিগুলির মধ্যে একটি, যা মেমরি অ্যাক্সেস লঙ্ঘনের কারণে ঘটে, যেমন অবৈধ অ্যারে সূচক অ্যাক্সেস করা, কিছু সীমাবদ্ধ ঠিকানা নির্দেশ করা ইত্যাদি। এই নিবন্ধে, আমরা GDB টুল ব্যবহার করে এই ধরনের ত্রুটি কীভাবে সনাক্ত করতে পারি তা দেখব। .

আসুন আমরা ত্রুটিটি সনাক্ত করার জন্য কোড এবং সংশ্লিষ্ট পদক্ষেপগুলি দেখি৷

উদাহরণ

#include <stdio.h>
main() {
   int* ptr = NULL;
   *ptr = 1; //trying to access unknown memory location
   printf("%p\n", ptr);
}

'gcc –g program_name.c' ব্যবহার করে কোড কম্পাইল করুন, এবং './a.out' ব্যবহার করে চালান

আউটপুট

soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ ./a.out
Segmentation fault (core dumped)

বিভাজন ত্রুটি ঘটেছে৷

লিখুন ‘gdb ./a.out core’

soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ gdb ./a.out core
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<https://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
/home/soumyadeep/Cpp_progs/core: No such file or directory.
(gdb)

'r' টাইপ করুন এবং এন্টার টিপুন।

Starting program: /home/soumyadeep/Cpp_progs/a.out

Program received signal SIGSEGV, Segmentation fault.
0x000055555555465e in main () at 1230.find_seg_error.c:5
5 *ptr = 1; //trying to access unknown memory location
(gdb)

তাই আমরা সফলভাবে ত্রুটি পেয়েছি, এখন GDB প্রস্থান করুন

(gdb) quit
A debugging session is active.

Inferior 1 [process 2794] will be killed.

Quit anyway? (y or n) y

  1. C++ ব্যবহার করে স্টপিং স্টেশনের সংখ্যা খুঁজুন

  2. কিভাবে C++ ব্যবহার করে OpenCV-এ একটি উপবৃত্ত আঁকবেন?

  3. কিভাবে C++ ব্যবহার করে OpenCV-এ একটি লাইন আঁকবেন?

  4. কিভাবে C++ প্রোগ্রাম ব্যবহার করে একটি প্রোগ্রাম চালু করবেন?