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;
}
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.
Get Answers For Free
Most questions answered within 1 hours.