Question

I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...

I'm having a warning in my visual studio 2019, Can anyone please solve this warning.

Severity   Code   Description   Project   File   Line   Suppression State
Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.  

Here is the C++ code were I'm having the warning.

// Sstack.cpp

#include "SStack.h"

// Constructor
SStack::SStack(int cap) : Capacity(cap), used(0)
{
   DynamicStack = new string[Capacity];
}

// Copy Constructor
SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)
{
   DynamicStack = new string[Capacity];

   for (int i = 0; i < used; i++)

       DynamicStack[i] = s.DynamicStack[i]; ( Line where I,m getting the above warning)
}


//destructor
SStack::~SStack()
{
   delete DynamicStack;
}

// function to insert s on top of stack
// Precondition: the stack is not full.
void SStack::push(const std::string s)
{
   DynamicStack[used] = s;
   used++;
}

// function to remove and return the top element of the stack
// Precondition: the stack is not empty.
string SStack::pop()
{
   used--;
   string data = DynamicStack[used];
   return data;
}

// function to return the top element of the stack
// Precondition: the stack is not empty.
string SStack::top() const
{
   return DynamicStack[used - 1];
}

// function to return true if Stack is empty else return false
bool SStack::IsEmpty() const
{
   return used == 0;
}

//printing all the elements in the stack
void SStack::print() const
{
   // loop to display the elements from top to bottom
   for (int i = used - 1; i >= 0; i--)
   {
       cout << DynamicStack[i] << endl;
   }
}

// function to return the number of elements currently in the stack
int SStack::size() const
{
   return used;
}

// function to return the maximum element that can be in the stack
int SStack::getCapacity() const
{
   return Capacity;
}

// Postcondition: The stack returned is the union of s1 and s2.
SStack operator +(const SStack& s1, const SStack& s2)
{
   // create a copy of stacks s1 and s2
   SStack copy_s1(s1), copy_s2(s2);

   // create a resultant stack which is union of s1 and s2
   SStack result(s1.getCapacity() + s2.getCapacity());

   // loop to push elements from copy of s1 to result
   while (!copy_s1.IsEmpty())
   {
       result.push(copy_s1.pop());
   }

   // loop to push elements from copy of s2 to result
   while (!copy_s2.IsEmpty())
   {
       result.push(copy_s2.pop());
   }

   return result;
}

//Return true if the two stacks contain exactly the same element values in the same order.
bool equals(const SStack& s1, const SStack& s2)
{
   // create a copy of stacks s1 and s2
   SStack copy_s1(s1), copy_s2(s2);

   // loop over stacks s1 and s2
   while (!copy_s1.IsEmpty() && !copy_s2.IsEmpty())
   {
       // if top element of s1 != s2, stacks are not equal
       if (copy_s1.pop() != copy_s2.pop())
           return false;
   }

   // after the loop if stack s1 or s2 is not empty, stacks are not equal
   if (!copy_s1.IsEmpty() || !copy_s2.IsEmpty())
       return false;

   return true; // stacks are equal
}

//Main.cpp

#include <iostream>
#include <fstream>
#include "SStack.h"

using namespace std;

int main()
{
   ifstream fin("all.last.txt");
   string name;

   if (fin.fail())
   {
       cout << "Unable to open file: all.last.txt... Exiting application" << endl;
       return 1;
   }

   int capacity, names_to_read;

   cout << "Enter the capacity of the stack: ";
   cin >> capacity;

   cout << "Enter the number of names to read from file (< " << capacity << "): ";
   cin >> names_to_read;

   SStack s1(capacity);

   cout << boolalpha;
   cout << "Testing IsEmpty: " << s1.IsEmpty() << endl << endl;
   while (!fin.eof() && s1.size() < names_to_read)
   {
       getline(fin, name);
       s1.push(name);
   }

   fin.close();
   cout << "Stack S1:" << endl;
   cout << "Capacity of Stack: " << s1.getCapacity() << endl;
   cout << "Size of Stack: " << s1.size() << endl;
   cout << "Elements of stack: " << endl;
   s1.print();

   SStack copyS1(s1);
   cout << endl << "Testing copy constructor\nCopy of Stack S1:" << endl;
   cout << "Capacity of Stack: " << copyS1.getCapacity() << endl;
   cout << "Size of Stack: " << copyS1.size() << endl;
   cout << "Elements of stack: " << endl;
   copyS1.print();

   cout << "S1 == copyS1: " << equals(s1, copyS1) << endl << endl;

   SStack s2(50);

   cout << "Enter 10 strings: " << endl;
   for (int i = 0; i < 10; i++)
   {
       cin >> name;
       s2.push(name);
   }

   cout << "Stack S2: " << endl;
   cout << "Capacity of Stack: " << s2.getCapacity() << endl;
   cout << "Size of Stack: " << s2.size() << endl;
   cout << "Elements of stack: " << endl;
   s2.print();


   SStack unionStack = s1 + s2;

   cout << endl << "Testing Union of stacks s1 and s2" << endl;
   cout << "Capacity of Stack: " << unionStack.getCapacity() << endl;
   cout << "Size of Stack: " << unionStack.size() << endl;
   cout << "Elements of stack: " << endl;

   while (!unionStack.IsEmpty())
   {
       cout << unionStack.top() << endl;
       unionStack.pop();
   }


   return 0;
}

Homework Answers

Answer #1

The real problem in your code is that  DynamicStack[i] = s.DynamicStack[i], uses uninitialized memory. This is

because _alloca allocates memory from the stack, but does not initialize it.

This warning indicates that the readable extent of the specified buffer might be smaller than the index used to read

from it. Attempts to read data outside the valid range leads to buffer overrun.

If used > Capacity or if used > s.size(), then behaviour of the program is undefined.

You can also use ' #pragma warning(suppress : 6385)', and in the present case this is probably the best option.

Know the answer?
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
Similar Questions
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks ---------------------------------- main.cc ---------------------------- #include #include #include #include "numbers.h" using namespace std; int main() { Numbers N1, N2;       for(size_t i = 2; i < 16;...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
Something is either messed up in my operator overload <<, covertopostfix function, or my main output....
Something is either messed up in my operator overload <<, covertopostfix function, or my main output. Cannot figure it out. please help. Please comment your changes too. Program below is supposed to be outputting like this: InFix is:   A+B-C Post fix is:   A B + C - InFix is:   A+C Post fix is:   A C + InFix is:   x*(y+z)-(w+t) Post fix is:   x y z + * w t + - InFix is:   A+B*(C+D)-E/F+G+H Post fix is:   A B C...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT