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
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Complete the following C program to solve the parenthesis matching problem using a stack. We studied...
Complete the following C program to solve the parenthesis matching problem using a stack. We studied the problem and the algorithm in class. Given a sequence of chars (symbols), check if each “(”, “{”, or “[” is paired with a matching “)”, “}”, or “[”. For example, correct: ( )(( )){([( )])}     correct: (( )(( ))){([( )])}   incorrect: )(( )){([( )])}     incorrect: ({[ ])}     incorrect: (   #include <stdio.h> #include <stdlib.h> #define size 100 void push(char *s, int* top, char element);...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
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;...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with...
I need a few unit tests done on my SelectionSort program in Visual Studio 2019 with MSTest. My program is below. I just need screen shots of the unit test code in Visual Studio 2019 with MSTest. Please and thank you. using System; namespace SelectionSortAlgorithm { public class SelectionSort { public static void Main(string[] args) { int[] dataSet = new int[5] { 2, 99, 27, 68, 3 }; // 5 element array initialized int n = 5; // variable declar...
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.          ...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT