Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 05, 2004 Unneeded warning | ||||
---|---|---|---|---|
| ||||
Hello, i'm getting strange warning when there is use temp object. Here is snippet: const char* foo(void) { const char* test = " test "; return test; } int main() { const char* c; if(!(c = foo())) /* ... */ else /* ... */ return 0; } I'm getting this: if(!(c = foo())) ^ test.cpp(12) : Warning 2: possible unintended assignment link test,,,user32+kernel32/noi; Here should not be warning since temporary object is created. Comments ? Sanel |
October 05, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanel | Hello,
Sanel wrote...
> I'm getting this:
>
> if(!(c = foo()))
> ^
> test.cpp(12) : Warning 2: possible unintended assignment
The compiler warns if an assignment is used where a comparison might
have been intended. The warning message is not given when the if-
expression is written as
if((c = foo()) == false)
because of the explicit comparison operator is seen.
- Heinz
|
October 05, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanel | Sanel wrote:
> Hello,
> i'm getting strange warning when there is use temp object.
> Here is snippet:
>
> const char* foo(void)
> {
> const char* test = " test ";
> return test;
> }
>
> int main()
> {
> const char* c;
> if(!(c = foo()))
> /* ... */
> else
> /* ... */
> return 0;
> }
>
> I'm getting this:
>
> if(!(c = foo()))
> ^
> test.cpp(12) : Warning 2: possible unintended assignment
> link test,,,user32+kernel32/noi;
>
> Here should not be warning since temporary object is created.
>
> Comments ?
> Sanel
The warning is about the assigment of 'c' in the if ( !( c = foo() ) ).
To get rid of it:
c = foo ();
if ( ! c )
Btw be aware of the scope of the temp obj pointed by and returned in 'test' by the function foo (). The object does not exist anymore when de function 'foo ()' returns, so where is 'test' pointing at??
Arjan
|
October 05, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arjan Knepper | >
>Btw be aware of the scope of the temp obj pointed by and returned in 'test' by the function foo (). The object does not exist anymore when de
> function 'foo ()' returns, so where is 'test' pointing at??
This is not true. Test points on static part of program memory, so until someone does
test = "something else"
or
test = 0
it will remain the same.
Note that 'test' is declared as
const char *test=...
not as
const char test[]=...
Sanel
|
October 05, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz Saathoff | Yes,
but there should be silent implicit comparison. For example, GCC with -Wall
option
does not say anything and i am thinking that warning for obvious expression
should not be.
Am i wrong ?
Sanel
>The compiler warns if an assignment is used where a comparison might have been intended. The warning message is not given when the if- expression is written as
> if((c = foo()) == false)
>because of the explicit comparison operator is seen.
>
>
>- Heinz
>
|
October 05, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanel | Ups, here was typo. Instead 'test' should be 'c' var if that is outside foo() function. But you know what i mean. 'c' will point on the same address as 'test' in foo(). :) In article <cju7o0$1jj$1@digitaldaemon.com>, Sanel says... > >> >>Btw be aware of the scope of the temp obj pointed by and returned in 'test' by the function foo (). The object does not exist anymore when de >> function 'foo ()' returns, so where is 'test' pointing at?? > >This is not true. Test points on static part of program memory, so until someone does > >test = "something else" >or >test = 0 > >it will remain the same. > >Note that 'test' is declared as > >const char *test=... > >not as > >const char test[]=... > >Sanel > > |
October 05, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanel | Sanel wrote: > Ups, here was typo. > Instead 'test' should be 'c' var if that is outside foo() function. But you > know what i mean. 'c' will point on the same address as 'test' in foo(). > > :) Yes I understood. And you are right. I had the impression you were creating a local scoped object in foo () and returning a pointer to it. Because you talked about a "temporary object is created". I should have red more carefully. Arjan > > In article <cju7o0$1jj$1@digitaldaemon.com>, Sanel says... > >>>Btw be aware of the scope of the temp obj pointed by and returned in 'test' by the function foo (). The object does not exist anymore when de >>> function 'foo ()' returns, so where is 'test' pointing at?? >> >>This is not true. Test points on static part of program memory, so until someone >>does >> >>test = "something else" or >>test = 0 >> >>it will remain the same. >> >>Note that 'test' is declared as >> >>const char *test=... >> >>not as >> >>const char test[]=... >> >>Sanel >> >> > > > |
October 06, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanel | Hello,
Sanel wrote...
> Yes,
> but there should be silent implicit comparison. For example, GCC with -Wall
> option
> does not say anything and i am thinking that warning for obvious expression
> should not be.
>
> Am i wrong ?
No, because ! should tell the compiler that the result of (c=foo()) is implicitly converted to bool and implicitly compared for equality with false. In my opinion the extra parenthesis also could prevent the warning so that
if( c=foo() ) ...; // gives warning
if( (c=foo()) ) ....; // no warning
On the other hand, as long as it's a warning it doen't matter. In the beginning when I switched from Pascal to C I often made this mistake (same with forgetting parenthesis when calling a function).
- Heinz
|
October 06, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz Saathoff | Hi, am i missing something here. As you can see in my first post, i stated "if(!(c = foo()))" >false. In my opinion the extra parenthesis also could prevent the warning so that so this is compiler specific issue. This has nothing to do with extra parenthesis. >On the other hand, as long as it's a warning it doen't matter You are right, but when you try to port code written for other compilers, this become annoying. What Walter say about this ? Sanel In article <MPG.1bcdf5eed178f13a9896eb@news.digitalmars.com>, Heinz Saathoff says... > >Hello, > >Sanel wrote... >> Yes, >> but there should be silent implicit comparison. For example, GCC with -Wall >> option >> does not say anything and i am thinking that warning for obvious expression >> should not be. >> >> Am i wrong ? > >No, because ! should tell the compiler that the result of (c=foo()) is implicitly converted to bool and implicitly compared for equality with false. In my opinion the extra parenthesis also could prevent the warning so that > > if( c=foo() ) ...; // gives warning > if( (c=foo()) ) ....; // no warning > >On the other hand, as long as it's a warning it doen't matter. In the beginning when I switched from Pascal to C I often made this mistake (same with forgetting parenthesis when calling a function). > > >- Heinz |
October 06, 2004 Re: Unneeded warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sanel | Hello, Sanel wrote... > Hi, > am i missing something here. As you can see in my first post, i stated > "if(!(c = foo()))" I saw that! IMO the ! operator should be enough to suppress the warning. My extra note was that the warning could also be suppressed by the parenthesis around the assignment. > >false. In my opinion the extra parenthesis also could prevent the warning so that > > so this is compiler specific issue. This has nothing to do with extra parenthesis. Right, it's the way the compiler issues warnings. Warnings are not necessary errors, that's why files with only warnings still compile. It's only a hint to the programmer that he might have intended something different. If you ever have used lint you know that you get lot's more of warnings. > >On the other hand, as long as it's a warning it doen't matter > You are right, but when you try to port code written for other compilers, this become annoying. Compilers can behave different here as long as they compile otherwise valid code (and your example is valid). If you want to have a warning clean built you can either suppress this warning with the command line switch -w2 or change the code a bit. > What Walter say about this ? Don't know. I appreciate warnings. - Heinz |
Copyright © 1999-2021 by the D Language Foundation