Question

In: Computer Science

Write a C++ program to maintain a fixed size library of 1024 documents, whose size randomly...

Write a C++ program to maintain a fixed size library of 1024 documents, whose size randomly ranges between 2MB to 3MB.

Solutions

Expert Solution

he thing to remember with C++ memory management is ownership. If the LoadAndSetupData data is not going to take ownership of the string, then it's still your responsibility. Since you can't delete it immediately (because of the asynchronicity issue), you're going to have to hold on to those pointers until such time as you know you can delete them.

Maintain a pool of strings that you have created:

If you have some point in time where you know that the queue has been completely dealt with, you can simply delete all the strings in the pool.
If you know that all strings created after a certain point in time have been dealt with, then keep track of when the strings were created, and you can delete that subset. - If you can somehow find out when an individual string has been dealt with, then just delete that string.

class StringPool
{
struct StringReference {
char *buffer;
time_t created;
} *Pool;

size_t PoolSize;
size_t Allocated;

static const size_t INITIAL_SIZE = 1024;

void GrowBuffer()
{
StringReference *newPool = new StringReference[PoolSize * 2];
for (size_t i = 0; i < Allocated; ++i)
newPool[i] = Pool[i];
StringReference *oldPool = Pool;
Pool = newPool;
delete[] oldPool;
}

public:

StringPool() : Pool(new StringReference[INITIAL_SIZE]), PoolSize(INITIAL_SIZE)
{
}

~StringPool()
{
ClearPool();
delete[] Pool;
}

char *GetBuffer(size_t size)
{
if (Allocated == PoolSize)
GrowBuffer();
Pool[Allocated].buffer = new char[size];
Pool[Allocated].buffer = time(NULL);
++Allocated;
}

void ClearPool()
{
for (size_t i = 0; i < Allocated; ++i)
delete[] Pool[i].buffer;
Allocated = 0;
}

void ClearBefore(time_t knownCleared)
{
size_t newAllocated = 0;
for (size_t i = 0; i < Allocated; ++i)
{
if (Pool[i].created < knownCleared)
{
delete[] Pool[i].buffer;
}
else
{
Pool[newAllocated] = Pool[i];
++newAllocated;
}
}
Allocated = newAllocated;
}

// This compares pointers, not strings!
void ReleaseBuffer(char *knownCleared)
{
size_t newAllocated = 0;
for (size_t i = 0; i < Allocated; ++i)
{
if (Pool[i].buffer == knownCleared)
{
delete[] Pool[i].buffer;
}
else
{
Pool[newAllocated] = Pool[i];
++newAllocated;
}
}
Allocated = newAllocated;
}

};


Related Solutions

Write a C/C++ program to maintain a fixed size library of 1024 documents, whose size randomly...
Write a C/C++ program to maintain a fixed size library of 1024 documents, whose size randomly ranges between 2MB to 3MB.
Many documents use a specific format for a person's name. Write a program whose input is:...
Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName and whose output is: lastName, firstInitial.middleInitial. If the input has the form: firstName lastName the output is: lastName, firstInitial. Ex: If the input is: user_input = input() splits = user_input.split(" ") if(len(splits) == 3): first_name = splits[0] middle_name = splits[1] last_name = splits[2] print(last_name + ", " + first_name[0] + "." + middle_name[0] + ".") elif(len(splits) == 2): first_name = splits[0]...
Code in C Write a program whose inputs are three integers, and whose outputs are the...
Code in C Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. Ex: If the input is: 7 15 3 the output is: largest: 15 smallest: 3 Your program must define and call the following two functions. The LargestNumber function should return the largest number of the three input values. The SmallestNumber function should return the smallest number of the three input values. int...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Write a C program whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Write a program Write a program whose inputs are three integers, and whose output is the...
Write a program Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 C++ please
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT