public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* attribute initialized
@ 2005-07-11 18:07 Sylvester Diehl
  2005-07-11 19:06 ` Joe Buck
  0 siblings, 1 reply; 6+ messages in thread
From: Sylvester Diehl @ 2005-07-11 18:07 UTC (permalink / raw)
  To: gcc; +Cc: Sylvester Diehl

Hello

why doesn't gcc (-Wall -Wuninitalized -O) detect 
an uninialized variable passed by reference
decleared as <type> const *<name> ?

Do we need an attribute like (("initialized")) in the
function prototype to give gcc an hint to force
checking of uninitalized parameters ?

example
file foo.c:
--------- cut here
/*
gcc -Wall -Wuninitialized -O foo.c

*/
#include <stdio.h>
int foo(int const *p)
{
	static int sum = 0;

	sum += *p;
	return sum;
}

int main(int argc, char **argv)
{
	int k;

	return printf("%d\n", foo(&k));
}

--------- cut here  end of file foo.c

p.s. i know i could pass the variable by value
     to get a warning of an uninitalized variable.

-- 
Sylvester Diehl
marco Systemanalyse und Entwicklung GmbH    Tel   +49 8131 5161 42
Hans-Böckler-Str. 2, D 85221 Dachau         Fax   +49 8131 5161 66
http://www.marco.de/                        Email diehl@marco.de

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attribute initialized
  2005-07-11 18:07 attribute initialized Sylvester Diehl
@ 2005-07-11 19:06 ` Joe Buck
  2005-07-12  9:34   ` Dave Korn
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Buck @ 2005-07-11 19:06 UTC (permalink / raw)
  To: Sylvester Diehl; +Cc: gcc

On Mon, Jul 11, 2005 at 08:07:20PM +0200, Sylvester Diehl wrote:
> why doesn't gcc (-Wall -Wuninitalized -O) detect 
> an uninialized variable passed by reference
> decleared as <type> const *<name> ?

There are no uninitialized variables in your program.  For the
kind of access checking you seem to be asking for, you'll need
something like valgrind or mudflap.

> int foo(int const *p)
> {
> 	static int sum = 0;
> 
> 	sum += *p;
> 	return sum;
> }

it happens that the memory that p points to is unitialized, but
that is not what -Wuninitialized does.  Similarly, in

> int main(int argc, char **argv)
> {
> 	int k;
> 
> 	return printf("%d\n", foo(&k));
> }

there are no uninitialized variables, as the address of k is
perfectly well defined.

> p.s. i know i could pass the variable by value
>      to get a warning of an uninitalized variable.

Yes, because then there will *be* an unitialized variable.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: attribute initialized
  2005-07-11 19:06 ` Joe Buck
@ 2005-07-12  9:34   ` Dave Korn
  2005-07-12  9:56     ` Falk Hueffner
  2005-07-13 14:09     ` Sylvester Diehl
  0 siblings, 2 replies; 6+ messages in thread
From: Dave Korn @ 2005-07-12  9:34 UTC (permalink / raw)
  To: 'Joe Buck', 'Sylvester Diehl'; +Cc: gcc

----Original Message----
>From: Joe Buck
>Sent: 11 July 2005 20:07

> On Mon, Jul 11, 2005 at 08:07:20PM +0200, Sylvester Diehl wrote:
>> why doesn't gcc (-Wall -Wuninitalized -O) detect
>> an uninialized variable passed by reference
>> decleared as <type> const *<name> ?
> 
> There are no uninitialized variables in your program.  For the
> kind of access checking you seem to be asking for, you'll need
> something like valgrind or mudflap.
> 
>> int foo(int const *p)
>> {
>> 	static int sum = 0;
>> 
>> 	sum += *p;
>> 	return sum;
>> }
> 
> it happens that the memory that p points to is unitialized, but
> that is not what -Wuninitialized does.  Similarly, in
> 
>> int main(int argc, char **argv)
>> {
>> 	int k;
>> 
>> 	return printf("%d\n", foo(&k));
>> }
> 
> there are no uninitialized variables, as the address of k is
> perfectly well defined.

  Indeed so, but I think Sylvester's point is that given that foo takes a
const pointer, the compiler could theoretically know that foo cannot
legitimately make any use of (dereference) the pointer value being passed
and could perhaps issue a warning.

  Myself, I was surprised that the inliner didn't catch on to what was going
on and complain.  I would have expected that, but it didn't even at O3.

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: attribute initialized
  2005-07-12  9:56     ` Falk Hueffner
@ 2005-07-12  9:56       ` Dave Korn
  0 siblings, 0 replies; 6+ messages in thread
From: Dave Korn @ 2005-07-12  9:56 UTC (permalink / raw)
  To: falk; +Cc: 'Joe Buck', 'Sylvester Diehl', gcc

----Original Message----
>From: falk@debian.org
>Sent: 12 July 2005 10:56

> "Dave Korn" writes:
> 

>>   Myself, I was surprised that the inliner didn't catch on to what
>> was going on and complain.  I would have expected that, but it
>> didn't even at O3.
> 
> It does for me with mainline.


  Ah, there ya go.  I tested 3.4.4.


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attribute initialized
  2005-07-12  9:34   ` Dave Korn
@ 2005-07-12  9:56     ` Falk Hueffner
  2005-07-12  9:56       ` Dave Korn
  2005-07-13 14:09     ` Sylvester Diehl
  1 sibling, 1 reply; 6+ messages in thread
From: Falk Hueffner @ 2005-07-12  9:56 UTC (permalink / raw)
  To: Dave Korn; +Cc: 'Joe Buck', 'Sylvester Diehl', gcc

"Dave Korn" <dave.korn@artimi.com> writes:

>>From: Joe Buck
>> 
>> there are no uninitialized variables, as the address of k is
>> perfectly well defined.
>
>   Indeed so, but I think Sylvester's point is that given that foo
> takes a const pointer, the compiler could theoretically know that
> foo cannot legitimately make any use of (dereference) the pointer
> value being passed and could perhaps issue a warning.

However, it can store the pointer somewhere and dereference it later,
when it points to something initialized. But a low-priority warning
might be useful, one would have to experiment with how many false
positives this gives.

>   Myself, I was surprised that the inliner didn't catch on to what
> was going on and complain.  I would have expected that, but it
> didn't even at O3.

It does for me with mainline.

-- 
	Falk

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attribute initialized
  2005-07-12  9:34   ` Dave Korn
  2005-07-12  9:56     ` Falk Hueffner
@ 2005-07-13 14:09     ` Sylvester Diehl
  1 sibling, 0 replies; 6+ messages in thread
From: Sylvester Diehl @ 2005-07-13 14:09 UTC (permalink / raw)
  To: Dave Korn; +Cc: Joe.Buck, gcc

> ----Original Message----
> >From: Joe Buck
> >Sent: 11 July 2005 20:07
> 
> > On Mon, Jul 11, 2005 at 08:07:20PM +0200, Sylvester Diehl wrote:
> >> why doesn't gcc (-Wall -Wuninitalized -O) detect
> >> an uninialized variable passed by reference
> >> decleared as <type> const *<name> ?
> > 
> > There are no uninitialized variables in your program.  For the
> > kind of access checking you seem to be asking for, you'll need
> > something like valgrind or mudflap.
> > 
> >> int foo(int const *p)
> >> {
> >> 	static int sum = 0;
> >> 
> >> 	sum += *p;
> >> 	return sum;
> >> }
> > 
> > it happens that the memory that p points to is unitialized, but
> > that is not what -Wuninitialized does.  Similarly, in
> > 
> >> int main(int argc, char **argv)
> >> {
> >> 	int k;
> >> 
> >> 	return printf("%d\n", foo(&k));
> >> }
> > 
> > there are no uninitialized variables, as the address of k is
> > perfectly well defined.
> 
>   Indeed so, but I think Sylvester's point is that given that foo takes a
> const pointer, the compiler could theoretically know that foo cannot
> legitimately make any use of (dereference) the pointer value being passed
> and could perhaps issue a warning.

My inital point was a function which gets parameters as pointers
and this pointers have to point to something initalized.
Passing the values gives me the expected warning.
However it's more efficent to refer big data structures
to a function with a pointer.

Now i was looking for a posibility to clearify this the compiler.

I didn't find an adequate attribute.
The use of const was a trial to issue a warning.
I favored an attribute.

cheers,
Sylvester

> 
>   Myself, I was surprised that the inliner didn't catch on to what was going
> on and complain.  I would have expected that, but it didn't even at O3.
> 
>     cheers,
>       DaveK

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-07-13 14:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-11 18:07 attribute initialized Sylvester Diehl
2005-07-11 19:06 ` Joe Buck
2005-07-12  9:34   ` Dave Korn
2005-07-12  9:56     ` Falk Hueffner
2005-07-12  9:56       ` Dave Korn
2005-07-13 14:09     ` Sylvester Diehl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).