I have been getting following warning while compiling the C++ source code.
variable ‘tab’ set but not used [-Wunused-but-set-variable]
I have a snippet of code: The compile shows above mentioned warning, please suggest me why compile show me warning. When I running program, I get: Segmentation fault (core dumped)
EX.1
const int n = 10000;
int main() {
char tab[n][n];
for(int x = 0; x < n; x++)
for(int y = 0; y < n; y++)
tab[x][y] = x + y;
But If I use global variable, my program good run.
EX.2
const int n = 10000;
char tab[n][n];
int main() {
for(int x = 0; x < n; x++)
for(int y = 0; y < n; y++)
tab[x][y] = x + y;
Once more please suggest me why compile show me warning with Ex.1
I Assume that your bottom t[n][n] really is tab[n][n]
You never read the variable. Some compiler might try to optimize this variable out since it doesn't seem to affect anything.
If the variable is at a global scope, then the compiler has a harder time to determine if it is used somewhere else or not so it doesn't complain.
if you read from it somewhere then the msg will go away
But of course you'll get the msg for variable a now