Question

C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector.

Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below:

// file: InnerCB.h
// Header file for Inner Circular Buffer.
// See project description for details.
//

#ifndef _INNERCB_H_
#define _INNERCB_H_

class InnerCB {

public:
   // Constructor, default size is 10.
   InnerCB(int n=10) ;

   // Copy constructor
   InnerCB(const InnerCB& other) ;

   // Destructor
   ~InnerCB() ;

   // Add item to circular buffer
   void enqueue(int data) ;

   // Remove item from circular buffer
   int dequeue() ;

   // True if no space left in buffer
   bool isFull() ; 

   // True if buffer holds no items
   bool isEmpty() ;

   // return maximum number of items this buffer can hold
   int capacity() ;

   // return number of items currently held in the buffer
   int size() ;

   // overloaded assignment operator
   const InnerCB& operator=(const InnerCB& rhs) ;

   // debugging function. Prints out contents.
   void dump() ;

   // grading function used to examine private data members.
   // Do not implement!
   bool inspect (int* &buf, int &cap, int &size, int &start, int &end) ;

private :
   int *m_buffer ;   // pointer to dynamically allocate array for buffer 
   int m_capacity ;  // length of the allocated space pointed by m_buffer
   int m_size ;      // # of items in the buffer
   int m_start ;     // index of the first (oldest) item in the buffer
   int m_end ;       // index of the last (newest) item in the buffer
} ;

#endif

-------------------------------------------------------------------------------------------------------------------------------------------

// file: CBofCB.h
// Header file for Circular Buffer of Circular Buffer.
// See project description for details.
//

#ifndef _CBOFCB_H_
#define _CBOFCB_H_

#include "InnerCB.h" 

class CBofCB {

public:
   // default constructor
   CBofCB() ;

   // copy constructor
   CBofCB(const CBofCB& other) ;

   // destructor
   ~CBofCB() ;

   // add item to this data structure
   void enqueue(int data) ;

   // remove item from this data structure
   int dequeue() ;

   // returns true if cannot add more items
   bool isFull() ; 

   // returns true if no items stored in data structure
   bool isEmpty() ;

   // number of items in the data structure as a whole.
   // Note: not the number of InnerCB's
   int size() ;

   // overloaded assignment operator
   const CBofCB& operator=(const CBofCB& rhs) ;

   // debugging function, prints out contents of data structure
   void dump() ;

   // grading function. Do not implement!
   bool inspect (InnerCB** &buf, int &cap, int &size, int &start, int &end) ;

private :

   // max number of Inner Circular Buffers
   //
   static const int m_obCapacity=7 ;

   // array of pointers to InnerCB's.
   // Each entry of the array is a pointer.
   // Note: array itself is NOT dynamically allocated
   InnerCB * m_buffers[m_obCapacity] ;

   int m_obSize ;  // number of inner circular buffers in the outer CB
   int m_oldest ;  // index of the oldest circular buffer (start)
   int m_newest ;  // index of the newest circular buffer (end)
} ;

#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dynamic Memory Allocation

Dispensing memory

There are two different ways that memory gets dispensed for information stockpiling:

Aggregate Time (or static) Allocation

Memory for named factors is designated by the compiler

Correct size and sort of capacity must be known at aggregate time

For standard exhibit affirmations, this is the reason the size must be steady

Dynamic Memory Allocation

Memory assigned "on the fly" amid run time

progressively assigned space typically set in a program portion known as the stack or the free store

Correct measure of room or number of things does not need to be known by the compiler ahead of time.

For dynamic memory portion, pointers are pivotal

Dynamic Memory Allocation

We can powerfully designate storage room while the program is running, yet we can't make new factor names "on the fly"

Therefore, dynamic allotment requires two stages:

Making the dynamic space.

Putting away its location in a pointer (with the goal that the space can be accesed)

To powerfully distribute memory in C++, we utilize the new administrator.

De-designation:

Deallocation is the "tidy up" of space being utilized for factors or other information stockpiling

Accumulate time factors are consequently deallocated in light of their referred to degree (this is the same as extension for "programmed" factors)

It is the developer's business to deallocate powerfully made space

To de-distribute dynamic memory, we utilize the erase administrator

Assigning space with new

To assign space progressively, utilize the unary administrator new, trailed by the sort being dispensed.

new int;/progressively dispenses an int

new twofold;/progressively dispenses a twofold

On the off chance that making an exhibit powerfully, utilize a similar shape, however put sections with a size after the sort:

new int[40];/progressively assigns a variety of 40 ints

new double[size];/progressively assigns a variety of size copies

/take note of that the size can be a variable

These announcements above are not extremely helpful independent from anyone else, in light of the fact that the apportioned spaces have no names! In any case, the new administrator restores the beginning location of the distributed space, and this location can be put away in a pointer:

int * p;/announce a pointer p

p = new int;/powerfully distribute an int and load address into p

twofold * d;/proclaim a pointer d

d = new twofold;/progressively designate a twofold and load address into d

/we can likewise do these in single line proclamations

int x = 40;

int * list = new int[x];

drift * numbers = new float[x+10];

Notice this is one all the more method for introducing a pointer to a substantial target (and the most essential one).

Getting to powerfully made space

So once the space has been powerfully allotted, how would we utilize it?

For single things, we experience the pointer. Dereference the pointer to achieve the progressively made target:

int * p = new int; //dynamic whole number, indicated by p

*p = 10; //relegates 10 to the dynamic whole number

cout << *p; //prints 10

For powerfully made clusters, you can utilize either pointer-balance documentation, or regard the pointer as the exhibit name and utilize the standard section documentation:

twofold * numList = new double[size]; //dynamic exhibit

for (int I = 0; I < estimate; i++)

numList[i] = 0; //instate exhibit components to 0

numList[5] = 20; //section documentation

*(numList + 7) = 15; //pointer-counterbalance documentation

/implies same as numList[7]

Deallocation of dynamic memory

To deallocate memory that was made with new, we utilize the unary administrator erase. The one operand ought to be a pointer that stores the deliver of the space to be deallocated:

int * ptr = new int; //powerfully made int

/...

erase ptr; //erases the space that ptr focuses to

Note that the pointer ptr still exists in this precedent. That is a named variable subject to extension and degree decided at accumulate time. It tends to be reused:

ptr = new int[10]; //direct p toward a fresh out of the box new cluster

To deallocate a dynamic cluster, utilize this shape:

erase [] name_of_pointer;

Precedent:

int * list = new int[40]; //dynamic cluster

erase [] list; //deallocates the cluster

list = 0; //reset rundown to invalid pointer

In the wake of deallocating space, it's dependably a smart thought to reset the pointer to invalid except if you are pointing it at another legitimate target immediately.

Add a comment
Know the answer?
Add Answer to:
C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

    How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...

  • Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp....

    Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp. Add the ouput of the implementation. use recursive functions to traverse the tree - read the implementation notes on using helper functions. Please use C++ programming ////////////////////////////////////////////////////////////// #include "BSTree.h" template <typename DataType, class KeyType> BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr ) { } template < typename DataType, class KeyType > BSTree<DataType, KeyType>::BSTree () {    root = 0; } template...

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • I need help solving this question from the practice final exam given to us to prepare...

    I need help solving this question from the practice final exam given to us to prepare for the final in C++ #include <iostream> using namespace std; /* The following is code for an OrderedCollection container and its related iterator. The container has a capacity determined by a constructor parameter. The container does not grow. Code that adds elements to the container ensures that the capacity of the container is never exceeded. An attempt to add an item to a full...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Balment a la medicul Quoc that speciala a circular que has the following private data members...

    Balment a la medicul Quoc that speciala a circular que has the following private data members and public member functions. The circular que simplemented using an atay. Your submission should consist of four separate files the three source code file header file.implementation file and main program or routine and the program otput. When making the submission, please do not submit it as a file Private data members: int the tray int current size oprema prinete the first met of the...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT