This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]