variable ‘something’ set but not used [-Wunused-but-set-variable]
Axel Freyn
axel-freyn@gmx.de
Tue Jul 12 11:20:00 GMT 2011
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
More information about the Gcc-help
mailing list