GCC warning "set but not used "

3.3k views Asked by At

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

2

There are 2 answers

2
Eric On BEST ANSWER

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

int a = t[n][n];

But of course you'll get the msg for variable a now

0
tinyfiledialogs On

you are filling up an array, but never use/read this array. the array expires at the end of the function.

when it is global, the compiler suppose you are going to access this array somewhere else. so it doesn't complain.