Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #91582 > unrolled thread

Intentionally unused variable, suppress warnings for just one?

Started bymathog <dmathog@gmail.com>
First post2016-08-02 14:30 -0700
Last post2016-08-03 10:46 -0400
Articles 7 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  Intentionally unused variable, suppress warnings for just one? mathog <dmathog@gmail.com> - 2016-08-02 14:30 -0700
    Re: Intentionally unused variable, suppress warnings for just one? Keith Thompson <kst-u@mib.org> - 2016-08-02 14:47 -0700
      Re: Intentionally unused variable, suppress warnings for just one? mathog <dmathog@gmail.com> - 2016-08-02 15:14 -0700
      Re: Intentionally unused variable, suppress warnings for just one? David Brown <david.brown@hesbynett.no> - 2016-08-03 14:17 +0200
    Re: Intentionally unused variable, suppress warnings for just one? gordonb.sapr8@burditt.org (Gordon Burditt) - 2016-08-02 16:52 -0500
      Re: Intentionally unused variable, suppress warnings for just one? David Brown <david.brown@hesbynett.no> - 2016-08-03 14:21 +0200
      Re: Intentionally unused variable, suppress warnings for just one? James Kuyper <jameskuyper@verizon.net> - 2016-08-03 10:46 -0400

#91582 — Intentionally unused variable, suppress warnings for just one?

Frommathog <dmathog@gmail.com>
Date2016-08-02 14:30 -0700
SubjectIntentionally unused variable, suppress warnings for just one?
Message-ID<nnr3ga$pjj$1@dont-email.me>
Greetings,

This is probably off topic, but somebody here is likely to know how to 
do this, so...

In some code there is a function:

   SOME_STRUCTURE_TYPE a_structure;
   int a_result = a_function(&a_structure);

where "a_result" is never referred to again in the rest of the code.  It 
is there so that if while debugging this section is entered the return 
value from "a_function" may be examined, as it contains information that 
is not stored in "a_structure".  This information is only relevant while 
debugging, hence the reason it is not referenced normally. At the moment 
there are about 10 of these.  When compiled each generates a warning 
from the compiler (gcc) that the variable is set and not referenced. 
These are the only warnings generated.  I'm pretty sure that this 
warning can be disabled for the entire compiler run, but that isn't 
wanted since if this happens anywhere else in the code the warning does 
indicate a problem which should be corrected.

What I want to do is suppress the warning only for these 10 lines.

If there is something in the gcc's preprocessor that will do this 
locally, that would be great.  That way there is little possibility of 
having the debug variable change the way the code runs.  Otherwise I 
guess what is needed is some "use it but don't do anything with it" code 
to apply to "a_result".  I considered adding a line like:

    do_nothing(a_result);

where do_nothing() is from another source code module, so the compiler 
could not tell if "a_result" was being used or not.  That isn't ideal 
though because it changes the way the code works, if only slightly by 
moving things around in memory.

Suggestions for a better way of doing this?

Thanks,

David Mathog

[toc] | [next] | [standalone]


#91584

FromKeith Thompson <kst-u@mib.org>
Date2016-08-02 14:47 -0700
Message-ID<lneg663kt5.fsf@kst-u.example.com>
In reply to#91582
mathog <dmathog@gmail.com> writes:
> This is probably off topic, but somebody here is likely to know how to 
> do this, so...
>
> In some code there is a function:
>
>    SOME_STRUCTURE_TYPE a_structure;
>    int a_result = a_function(&a_structure);
>
> where "a_result" is never referred to again in the rest of the code.
[reasons snipped]

> What I want to do is suppress the warning only for these 10 lines.

This seems to work for gcc, and may work for other compilers as well:

    SOME_STRUCTURE_TYPE a_structure;
    int a_result = a_function(&a_structure);
    (void)a_result;

(I might wonder whether you really should be ignoring the result, but
there's not enough information to speculate.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#91586

Frommathog <dmathog@gmail.com>
Date2016-08-02 15:14 -0700
Message-ID<nnr629$5cl$1@dont-email.me>
In reply to#91584
Keith Thompson wrote:
> This seems to work for gcc, and may work for other compilers as well:
>
>      SOME_STRUCTURE_TYPE a_structure;
>      int a_result = a_function(&a_structure);
>      (void)a_result;

That works for me.

Thanks!

David Mathog

[toc] | [prev] | [next] | [standalone]


#91597

FromDavid Brown <david.brown@hesbynett.no>
Date2016-08-03 14:17 +0200
Message-ID<nnsndi$j3s$1@dont-email.me>
In reply to#91584
On 02/08/16 23:47, Keith Thompson wrote:
> mathog <dmathog@gmail.com> writes:
>> This is probably off topic, but somebody here is likely to know how to 
>> do this, so...
>>
>> In some code there is a function:
>>
>>    SOME_STRUCTURE_TYPE a_structure;
>>    int a_result = a_function(&a_structure);
>>
>> where "a_result" is never referred to again in the rest of the code.
> [reasons snipped]
> 
>> What I want to do is suppress the warning only for these 10 lines.
> 
> This seems to work for gcc, and may work for other compilers as well:
> 
>     SOME_STRUCTURE_TYPE a_structure;
>     int a_result = a_function(&a_structure);
>     (void)a_result;
> 
> (I might wonder whether you really should be ignoring the result, but
> there's not enough information to speculate.)
> 

Casting a value to void like this is a common idiom for "ignore this
value" - I have used it on several compilers to suppress warnings like this.

gcc also has the __attribute__((unused)) extension that tells the
compiler that you will not be using that variable.  But the (void) trick
is more portable and, IMHO, more convenient.

But note that unless optimisation is disabled entirely, the compiler
will through away the result "a_result" - it will not be visible from
the debugger (unless you are happy looking at generated assembly and cpu
registers).  So you might be better doing this:

	volatile int a_result = a_function(&a_structure);

This will suppress the warning, and also tell gcc that you really want
the variable to be created and set even though you do not (apparently)
do anything with it.

[toc] | [prev] | [next] | [standalone]


#91585

Fromgordonb.sapr8@burditt.org (Gordon Burditt)
Date2016-08-02 16:52 -0500
Message-ID<67Gdney-OMymizzKnZ2dnUU7-cvNnZ2d@posted.internetamerica>
In reply to#91582
>   SOME_STRUCTURE_TYPE a_structure;
>   int a_result = a_function(&a_structure);
> 
> where "a_result" is never referred to again in the rest of the code.  It 
> is there so that if while debugging this section is entered the return 
> value from "a_function" may be examined, as it contains information that 
> is not stored in "a_structure".  This information is only relevant while 
> debugging, hence the reason it is not referenced normally. At the moment 

One option I have used occasionally:  provide a command-line switch
(assuming for the moment a UNIX-style command line), say a dash
followed by a Japanese Yen symbol, which will cause the program to
print those values.  Now they are used.

The actual problem I was trying to solve was the compiler optimizing
out "version strings" from a source-code-control system.  It was
sometimes convenient to actually use the option rather than using
the UNIX "strings" program on the executable.

If it's a GUI program, really use the variables in another way,
such as displaying them if the user clicks on the "o" in
the word "Copyright" on the "About" page.

[toc] | [prev] | [next] | [standalone]


#91599

FromDavid Brown <david.brown@hesbynett.no>
Date2016-08-03 14:21 +0200
Message-ID<nnsnjl$jub$1@dont-email.me>
In reply to#91585
On 02/08/16 23:52, Gordon Burditt wrote:
>>   SOME_STRUCTURE_TYPE a_structure;
>>   int a_result = a_function(&a_structure);
>>
>> where "a_result" is never referred to again in the rest of the code.  It 
>> is there so that if while debugging this section is entered the return 
>> value from "a_function" may be examined, as it contains information that 
>> is not stored in "a_structure".  This information is only relevant while 
>> debugging, hence the reason it is not referenced normally. At the moment 
> 
> One option I have used occasionally:  provide a command-line switch
> (assuming for the moment a UNIX-style command line), say a dash
> followed by a Japanese Yen symbol, which will cause the program to
> print those values.  Now they are used.
> 
> The actual problem I was trying to solve was the compiler optimizing
> out "version strings" from a source-code-control system.  It was
> sometimes convenient to actually use the option rather than using
> the UNIX "strings" program on the executable.

That sounds like a job for a "volatile const", or perhaps an
__attribute__((used)), for gcc or compatible compilers.

> 
> If it's a GUI program, really use the variables in another way,
> such as displaying them if the user clicks on the "o" in
> the word "Copyright" on the "About" page.
> 

[toc] | [prev] | [next] | [standalone]


#91604

FromJames Kuyper <jameskuyper@verizon.net>
Date2016-08-03 10:46 -0400
Message-ID<nnt03i$hqc$1@dont-email.me>
In reply to#91585
On 08/02/2016 05:52 PM, Gordon Burditt wrote:
...
> One option I have used occasionally:  provide a command-line switch
> (assuming for the moment a UNIX-style command line), say a dash
> followed by a Japanese Yen symbol, which will cause the program to
> print those values.  Now they are used.
> 
> The actual problem I was trying to solve was the compiler optimizing
> out "version strings" from a source-code-control system. ...

If you can be certain your program makes no use of the version string,
any sufficiently sophisticated compiler will be able to figure it out
too. The only way to make sure such strings remain embedded in the
resulting object or executable files is to actually use them - for
instance, in the way you described in the previous paragraph.

Since you frequently seem to forget, let me reiterate that I refuse to
give you or anyone else permission to quote any text I write without
including proper attribution for that text

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web