Skip to main content

Posts

Showing posts from August, 2024

Exploring Thread Creation in C++11 Using Callable Objects

Creating threads in C++11 provides the ability to execute multiple tasks simultaneously, which greatly improves the efficiency and responsiveness of your programs. In this comprehensive guide, we will delve into five different approaches to creating threads using callable objects. Each method will be explained in detail, accompanied by practical examples to illustrate their usage and benefits. By the end of this exploration, you will have a solid understanding of how to effectively manage concurrent execution in your C++11 applications. 1. Function Pointer A function pointer is a straightforward method for creating a thread. You provide a function along with its arguments to the constructor. Example: # include <iostream> # include <thread> // Function to be executed by the thread void fun ( int x) { while (x++ < 15 ) { std::cout << x << std::endl; } } int main () { // Create a thread that runs the function 'fun' with a...

Shallow Copy & Deep Copy in C++

Certainly! Exploring the concepts of shallow and deep copying in C++ is essential for understanding how objects are duplicated. Let's delve into these topics with comprehensive explanations and illustrative examples to clarify their differences and uses. Shallow Copy A shallow copy of an object copies all the member field values. This works fine if the object contains only primitive data types. However, if the object contains pointers to dynamically allocated memory, the shallow copy will copy the pointer values, not the actual data they point to. This means both the original and the copied object will point to the same memory location, which can lead to issues like double deletion or unintended modifications. Example of Shallow Copy # include <iostream> using namespace std; class Box { private : int length; int breadth; int height; int *p; public : // Function that sets the dimensions void set_dimensions ( int length1, int breadth1, ...

Local Classes in C++: A Deep Dive

Understanding Local Classes in C++: A Deep Dive A class declared within a function is known as a local class in C++. These classes are specific to the function in which they are declared and offer several unique characteristics and constraints. What is a Local Class? A local class is a class defined within a function. For instance, in the following example, the Test  class is local to the fun()  function: #include<iostream>  using namespace std;  void fun()    {      class Test  // local to fun      {          // members of the Test class     };  }  int main()  {      return 0;  } Interesting Facts About Local Classes Scope Limitation A local class type name can only be used within the enclosing function. For example, in the following program, the LocalClass is valid within myFunction() , but not in main() . #include <iostream> void myFunct...

Reasons for a C++ Program Crash

C++ programs may crash unexpectedly for various reasons. Here are some typical causes of such crashes: Segmentation Fault A segmentation fault is a major cause of program crashes. It occurs when: Attempting to access a memory location that doesn’t exist. Trying to write to a read-only memory location. Accessing protected memory locations, such as kernel memory. Example: int main() {     char *text;     // Stored in the read-only part of the data segment     text = "ABC";     // Problem: trying to modify read-only memory     *(text + 1) = 'n';     return 0; }   Stack Overflow Stack overflow happens due to non-terminating recursion, which exhausts the stack memory. Example: #include <stdio.h> void functionRecursive(int num)  {     if (num =...