Error C4700 Uninitialized local variable used

  • Thread starter Thread starter Jeff0803
  • Start date Start date
J

Jeff0803

Guest
I made a blanket balance checking program like following.

This code worrks well.

#include <iostream>
#include <string>
#include <stack>

using namespace std;
bool openblaket(char c)
{
bool ret;

if (c == '{' || c == '[' || c == '(')
ret = true;
else
ret = false;
return(ret);
}
bool MatchBlanket(char c1, char c2)
{
if (c1 == '{' && c2 == '}') return true;
if (c1 == '[' && c2 == ']') return true;
if (c1 == '(' && c2 == ')') return true;
return false;
}
bool IsBalanced(string s)
{
bool ret = true;
stack<char>* mystack = new stack<char>;

int n = s.length();
char* char_array = new char[n + 1];

strcpy(char_array, s.c_str());

for (int i = 0; i < n; i++)
{
if (openblaket(char_array))
mystack->push(char_array);
else if (MatchBlanket(mystack->top(),char_array) == true)
mystack->pop();
}
if (mystack->size() > 0)
ret = false;
delete mystack;
mystack = NULL;
delete[] char_array;
char_array = NULL;
return(ret);
}
int main()
{
string s = "{[()]}";
if (IsBalanced(s))
cout << "balanced!";
else
cout << "Not balanced!";

}

However, if I make stack in stack memory instead of heap memory like following, error occur.

stack<char>* mystack

Error C4700 Uninitialized local variable 'mystack' used

Why should I create mystack only in heap?

Continue reading...
 
Back
Top