Re: variable ‘something’ set but not used [-Wunused-but-set-variable]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Mahmood,
On Tue, Jul 12, 2011 at 04:11:48AM -0700, Mahmood Naderan wrote:
> dear all,
> GCC-4.6 says this warning
>     
> 
> syscall.c:1011:15: warning: variable ‘fullpath_length’ set but not used [-Wunused-but-set-variable]
> 
> 
> However in the code, I see
> 
>         int length, fullpath_length;    // warning at this line
>         int host_fd;
>         struct fd_t *fd;
> 
>         /* Read parameters */
>         pfilename = isa_regs->ebx;
>         flags = isa_regs->ecx;
>         mode = isa_regs->edx;
>         length = mem_read_string(isa_mem, pfilename, MAX_PATH_SIZE, filename);
>         if (length >= MAX_PATH_SIZE)
>             fatal("syscall open: maximum path length exceeded");
>         ld_get_full_path(isa_ctx, filename, fullpath, MAX_PATH_SIZE);
>         fullpath_length = strlen(fullpath);     // variable is used here
> 
>  
> As you can see fullpath_length is defined as 'int' and is used at the end of the code.
> Any way to fix that?

Gcc is right, the warning is appropriate.
gcc tells you that fullpath_length is not necessary, because while you
ASSIGN a value to it ("variable ... set"), you NEVER READ that value
("but not used").
And in your example code: Imagine we would simply remove
fullpath_length, and replace the last line by "strlen(fullpath);", then
your code would behave exactly as before. 
So why do you have this variable fullpath_length?

if you don't want that kind of warning, compile with the flag
-Wno-unused-but-set-variable

Axel


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux