Encrypting your link and protect the link from viruses, malware, thief, etc! Made your link safe to visit.

Write a code related to memory management and garbage collection and find it's efficiency and complexity. (in dev-c++/Data structures) (Please provide proper explanation of code's efficiency and complexity for beginner)

 Solution

Step 1

Code related to memory management

Memory in your C++ program is divided into two parts − The stack − All variables declared inside the function will take up memory from the stack. The heap − This is unused memory of the program and can be used to allocate the memory dynamically when program runs. C++ allows us to allocate the memory of a variable or an array in run time. This is known as dynamic memory allocation.

We can allocate and then deallocate memory dynamically using the new and delete operators respectively.

New operator

The new operator allocates memory to a variable. For example code ,

// declare an int pointer

int* pointVar;

// dynamically allocate memory

// using the new keyword

pointVar = new int;

// assign value to allocated memory

*pointVar = 45;

Delete Operator

Once we no longer need to use a variable that we have declared dynamically, we can deallocate the memory occupied by the variable.

For this, the delete operator is used. It returns the memory to the operating system. This is known as memory deallocation.

The syntax for this operator is

delete pointerVariable;

Consider the code:

// declare an int pointer

int* pointVar;

// dynamically allocate memory

// for an int variable

pointVar = new int;

 

// assign value to the variable memory

*pointVar = 45;

// print the value stored in memory

cout << *pointVar; // Output: 45

// deallocate the memory

delete pointVar;

Here, we have dynamically allocated memory for an int variable using the pointer pointVar.

C++ Dynamic Memory Allocation Example

#include <iostream>

using namespace std;

int main() {

    // declare an int pointer

    int* pointInt;

    // declare a float pointer

    float* pointFloat;

    // dynamically allocate memory

    pointInt = new int;

    pointFloat = new float;

    // assigning value to the memory

    *pointInt = 45;

    *pointFloat = 45.45f;

    cout << *pointInt << endl;

    cout << *pointFloat << endl;

    // deallocate the memory

    delete pointInt, pointFloat;

    return 0;

}

 

Output

 

45

45.45

Step 2

Code related to garbage collection

Garbage collection is a form of automatic memory management. The garbage collector attempts to reclaim garbage, or memory used by objects that will never be accessed or mutated again by the application. One of more widely used libraries that provides this function is Hans Boehm's conservative GC.

C++ is old fashioned. C++ has no garbage collection. At first, garbage collection breaks one of the key principles of C++: "Don't pay for something you don't use." That means, if you don't need garbage collection your C++ runtime should not waste its time cleaning up the whole garbage. My second point is more sophisticated. We have RAII in C++ and therefore the totally deterministic destruction of objects. RAII stand for Resource Acquisition Is Initialization. Probably, the most important idiom in C++ says that a resource should be acquired in the constructor of the object and released in the destructor of the object. The key is that the destructor will be automatically called if the object goes out of scope.

The example shows the totally deterministic behaviour of RAII in C++.

#include <iostream>

#include <new>

#include <string>

class ResourceGuard{

  private:

    const std::string resource;

  public:

    ResourceGuard(const std::string& res):resource(res){

      std::cout << "Acquire the " << resource << "." <<  std::endl;

    }

    ~ResourceGuard(){

      std::cout << "Release the "<< resource << "." << std::endl;

    }

};

int main(){

  std::cout << std::endl;

  ResourceGuard resGuard1{"memoryBlock1"};

  std::cout << "\nBefore local scope" << std::endl;

  {

    ResourceGuard resGuard2{"memoryBlock2"};

  }

  std::cout << "After local scope" << std::endl;

  std::cout << std::endl;

  std::cout << "\nBefore try-catch block" << std::endl;

  try{

      ResourceGuard resGuard3{"memoryBlock3"};

      throw std::bad_alloc();

  }  

  catch (std::bad_alloc& e){

      std::cout << e.what();

  }

  std::cout << "\nAfter try-catch block" << std::endl;

  std::cout << std::endl;

}

 

ST

Search This Blog

Labels

Report Abuse

QUESTION 6 (a) The bar shown in Figure Q2(a) is subjected to tensile load of 150 Kn. If the stress in the middle portions is limited to 160 N/mm², determine the diameter of the middle portion. Find also the length of the middle portion if the total elongation of the bar is to be 0.25 mm. E E = 2.0 + 105N/mm². (12 marks) 150 KN 10 cm DIA 10 cm DIA 150 KN 45 cm Figure Q6(a) (b) A brass bar, having cross-section area of 900 mm², is subjected to axial forces as shown in Figure Q2(b), in which AB = 0.6 m, BC = 0.8 m, and CD = 1.0 m. Find the total elongation of the bar. E = 1.0 + 105N/mm2 . (8 marks) Page 4 of 5 B D 40 KN 70 KN 20 KN 10 KN Figure Q6(b) (TOTAL = 20 MARKS)

  Question: Show transcribed image text Answer:
  Introduction To find incremental revenue and cost, we subtract Current revenue and cost from Projected revenue and cost. Answer Current Situation Projected Sales and Profit Incremental Revenues and Costs Total Revenue Php1,500,000 Php1,800,000 + Php3000,000 Variable Cost 575,000 425,000 - Php 332,000 Direct Fixed Costs 625,000 700,000 + Php 75,000 Indirect Fixed Costs 100,000 100,000 No change Profit Php 200,000 Php 575,000 + Php 375,000 Profit increases by Php 375,000, thus a new camera and coffee maker must be purchased.

Contributors