Дан фрагмент кода:
int A[5]={1,3,-5,4,2}, n=5,*p,*q,a=0,b=0,c=0;
for(p=A+n-1;p>=A;p--)
if(p>A+1 && p<A+3)
q=p;
a=*q; b=*(q-1);
задача такая:
Вычислить значения всех переменных в заданном фрагменте программы при выполнении каждой строки.
но когда я пытаюсь запустить отладку, выдает такую ошибку:
С4703-используется потенциально неинициализированная локальная переменная-указатель «q»

В чем проблема и как ее можно решить?
задан 29 апр 2021 в 10:56
![]()
7
Все просто, вы не инициализировали указатели p и q
Замените строку инициализации, на такую:
int A[5]={1,3,-5,4,2}, n=5,*p = nullptr,*q = nullptr,a=0,b=0,c=0;
ответ дан 29 апр 2021 в 12:09
Awesome ManAwesome Man
6643 золотых знака13 серебряных знаков31 бронзовый знак
1
I’m writing a project for a class and I have to have my program read arithmetic expressions from an input file and evaluate them. Unfortunately whenever I try to implement my ternaryCondition.h header my debug throws three
subexpression.cpp(75):error C4703: potentially uninitialized local pointer variable ‘first’ used
subexpression.cpp(75):error C4703: potentially uninitialized local pointer variable ‘second’ used
subexpression.cpp(75):error C4703: potentially uninitialized local pointer variable ‘third’ used
This is my second time working with C++ so I feel like I’m just missing something entirely.
I’ve tried disabling /sdl checks but when I do that I find that my program can no longer read line by line through my input file and evaluate the expressions.
This is the subexpressions.cpp thats throwing the error up to the 75th line where the error occurs:
#include <iostream>
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "operand.h"
#include "plus.h"
#include "minus.h"
#include "times.h"
#include "divide.h"
#include "greaterThan.h"
#include "lessThan.h"
#include "equal.h"
#include "and.h"
#include "or.h"
#include "negation.h"
#include "ternaryCondition.h"
#include <sstream>
SubExpression::SubExpression(Expression* left, Expression* right)
{
this->left = left;
this->right = right;
}
SubExpression::SubExpression(Expression* first, Expression* second, Expression* third)
{
this->first = first;
this->second = second;
this->third = third;
}
SubExpression::SubExpression(Expression* left)
{
this->left = left;
}
Expression* SubExpression::parse(stringstream& in)
{
Expression* left;
Expression* right;
Expression* first;
Expression* second;
Expression* third;
char operation, paren;
bool isTernary = false;
left = Operand::parse(in);
cin >> operation;
right = Operand::parse(in);
if (operation == ':')
{
first = left;
second = right;
left = Operand::parse(in);
cin >> operation;
right = Operand::parse(in);
if (operation == '?')
{
third = right;
isTernary = true;
}
}
cin >> paren;
if (isTernary == true)
{
return new TernaryCondition(first, second, third);
//THE LINE ABOVE IS LINE 75 WHERE THE ERROR IS BEING THROWN
}
switch (operation)
{
And this is the ternaryCondition.h header in case that could be causing issues:
class TernaryCondition : public SubExpression
{
public:
TernaryCondition(Expression* first, Expression* second, Expression* third) :
SubExpression(first, second, third)
{
}
int evaluate()
{
return third->evaluate() ? first->evaluate() : second->evaluate();
}
};
The point of this part of my code is so that the program can calculate expressions like
( ( ( z < ( 50 + aa ) ) & ( bb ! ) ) * ( ( 3 / cc ) | ( 1 : 0 ? ( z > aa ) ) , z = 4 , aa = 2 , bb = 4 , cc = 2 ;
I’m sorry if I submitted this in a improper format, this is my first time posting.
ADDED THE subexpression.h HEADER FILE:
class SubExpression : public Expression
{
public:
SubExpression(Expression* left, Expression* right);
SubExpression(Expression* left);
SubExpression(Expression* first, Expression* second, Expression* third);
static Expression* parse(stringstream& in);
protected:
Expression* left;
Expression* right;
Expression* first;
Expression* second;
Expression* third;
};
I’m writing a project for a class and I have to have my program read arithmetic expressions from an input file and evaluate them. Unfortunately whenever I try to implement my ternaryCondition.h header my debug throws three
subexpression.cpp(75):error C4703: potentially uninitialized local pointer variable ‘first’ used
subexpression.cpp(75):error C4703: potentially uninitialized local pointer variable ‘second’ used
subexpression.cpp(75):error C4703: potentially uninitialized local pointer variable ‘third’ used
This is my second time working with C++ so I feel like I’m just missing something entirely.
I’ve tried disabling /sdl checks but when I do that I find that my program can no longer read line by line through my input file and evaluate the expressions.
This is the subexpressions.cpp thats throwing the error up to the 75th line where the error occurs:
#include <iostream>
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "operand.h"
#include "plus.h"
#include "minus.h"
#include "times.h"
#include "divide.h"
#include "greaterThan.h"
#include "lessThan.h"
#include "equal.h"
#include "and.h"
#include "or.h"
#include "negation.h"
#include "ternaryCondition.h"
#include <sstream>
SubExpression::SubExpression(Expression* left, Expression* right)
{
this->left = left;
this->right = right;
}
SubExpression::SubExpression(Expression* first, Expression* second, Expression* third)
{
this->first = first;
this->second = second;
this->third = third;
}
SubExpression::SubExpression(Expression* left)
{
this->left = left;
}
Expression* SubExpression::parse(stringstream& in)
{
Expression* left;
Expression* right;
Expression* first;
Expression* second;
Expression* third;
char operation, paren;
bool isTernary = false;
left = Operand::parse(in);
cin >> operation;
right = Operand::parse(in);
if (operation == ':')
{
first = left;
second = right;
left = Operand::parse(in);
cin >> operation;
right = Operand::parse(in);
if (operation == '?')
{
third = right;
isTernary = true;
}
}
cin >> paren;
if (isTernary == true)
{
return new TernaryCondition(first, second, third);
//THE LINE ABOVE IS LINE 75 WHERE THE ERROR IS BEING THROWN
}
switch (operation)
{
And this is the ternaryCondition.h header in case that could be causing issues:
class TernaryCondition : public SubExpression
{
public:
TernaryCondition(Expression* first, Expression* second, Expression* third) :
SubExpression(first, second, third)
{
}
int evaluate()
{
return third->evaluate() ? first->evaluate() : second->evaluate();
}
};
The point of this part of my code is so that the program can calculate expressions like
( ( ( z < ( 50 + aa ) ) & ( bb ! ) ) * ( ( 3 / cc ) | ( 1 : 0 ? ( z > aa ) ) , z = 4 , aa = 2 , bb = 4 , cc = 2 ;
I’m sorry if I submitted this in a improper format, this is my first time posting.
ADDED THE subexpression.h HEADER FILE:
class SubExpression : public Expression
{
public:
SubExpression(Expression* left, Expression* right);
SubExpression(Expression* left);
SubExpression(Expression* first, Expression* second, Expression* third);
static Expression* parse(stringstream& in);
protected:
Expression* left;
Expression* right;
Expression* first;
Expression* second;
Expression* third;
};
- Remove From My Forums
-
Question
-
I am building mesa 8.0.4 on Windows 8 with VS2012 express and have a couple of compiler errors. One type of them is
error C4703, potentially uninitialized local pointer variable ‘iter’ used
This error occurs at line 6916 of ${MESASRC}srcglusgilibutilmipmap.c. The variable iter is declared as
const GLubyte *iter;
I can fix this error by initializing this variable like
const GLubyte *iter=NULL;
I wonder why the C++ compiler in VS2012 is so critical? It might be a warning instead of an error.
Answers
-
The recommended practice is to leave the warnings on and to fix the code rather than to ignore or suppress the warnings.
C4703 is a warning, not an error. The
SDL Checks option tells the compiler to treat warnings associated with security issues as errors. You can treat all warnings as errors with the
/WX flag.These checks make it easier to write good code: they allow the compiler to help detect problems that you don’t want to leave in your code. SDL requires the 4700 series warnings be fixed because uninitialized pointers can lead to security errors. See the
SDL Process docs,Appendix E.
If you turn this off then you will need to go to extra effort to confirm that your code is correct since the compiler won’t be helping find likely errors. If you are depending on legacy code which you can’t reasonably fix, and if you have validated that
the actual usage is safe then you can disable individual warnings with the
/wd compiler flag.By far the best course would be to fix the code not to generate the warning rather than suppressing or ignoring the warning.
—Rob
-
Marked as answer by
Monday, August 27, 2012 9:33 AM
-
Marked as answer by