Skip to main content

Posts

How to use gdb in Ubuntu Terminal

Here's a practical, minimal guide for using GDB in Ubuntu terminal — from launching to setting breakpoints and navigating your code. 🧰 Step 1: Compile with Debug Symbols Use -g and disable optimizations ( -O0 preferred for debugging): bash Copy Edit g++ -g -O0 -o myprog myprog.cpp 🚀 Step 2: Launch GDB bash Copy Edit gdb ./myprog You'll see a (gdb) prompt. 🎯 Step 3: Set Breakpoints You can set breakpoints in three main ways : ✅ a. At a function gdb Copy Edit (gdb) break main ✅ b. At a specific file and line gdb Copy Edit (gdb) break myprog.cpp:42 ✅ c. At a method in a class gdb Copy Edit (gdb) break MyClass::doSomething If the symbol is mangled (C++), GDB usually demangles automatically, especially if compiled with -g . ▶️ Step 4: Run the Program gdb Copy Edit (gdb) run 🧭 Step 5: Navigate Command Meaning next or n Step over current line step or s Step into function call finish Run until current function returns continue or c Continue until...

chatGpt Ans: For using smart pointers demonstration

Question:   suppose string fn1(){ char * cstr=... string str(cstr); free(cstr); return str; } shared_ptr<char*,&free> fn2(){ char *cstr=... shared_ptr<char*,&free> ptr=... return ptr; } unique_ptr<char*,&free> fn3(){ char *cstr=... unique_ptr<char*,&free> ptr=... return ptr; } Compare above 3 Fns wrt memory require, performance upon calling in main(){...} Answer: Let's compare the three functions `fn1()`, `fn2()`, and `fn3()` in terms of memory requirements and performance. 1. `fn1()`: ```cpp string fn1() {     char* cstr = ... // Assuming cstr is allocated dynamically, e.g., using malloc     string str(cstr);     free(cstr); // Release the memory after creating the string     return str; } ``` In this function, memory is dynamically allocated for `cstr`, and then it is used to create a `string` object. After creating the `string`, the memory is explicitly released using `free()`. This approach has an a...

gBard Ans: For using smart pointers demonstration

 Question suppose string fn1(){ char * cstr=... string str(cstr); free(cstr); return str; } shared_ptr<char*,&free> fn2(){ char *cstr=... shared_ptr<char*,&free> ptr=... return ptr; } unique_ptr<char*,&free> fn3(){ char *cstr=... unique_ptr<char*,&free> ptr=... return ptr; } Compare above 3 Fns wrt memory require, performance upon calling in main(){...} Answer: Sure, I can compare the three functions you have provided. Function Memory required Performance fn1() Allocates memory for the char* pointer and the string object. The string object will be copied when the function returns. fn2() Allocates memory for the char* pointer and the shared_ptr object. The shared_ptr object will keep a reference to the char* pointer, so the memory will not be deallocated until the shared_ptr object goes out of scope. fn3() Allocates memory for the char* pointer and the unique_ptr object. The unique_ptr object will take ownership of the char* point...