public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* free (static_array)
@ 2008-07-01  6:39 Sajish V
  2008-07-01  7:14 ` Denys Vlasenko
  0 siblings, 1 reply; 8+ messages in thread
From: Sajish V @ 2008-07-01  6:39 UTC (permalink / raw)
  To: gcc

Hi All, 
Can you please let me know why GCC does not crib when we try to free a static array?
main ()
{ 
     char array[100];
     free (array);
}
The above code compiles without any hitch. 
Thanks,
Sajish.



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

* Re: free (static_array)
  2008-07-01  6:39 free (static_array) Sajish V
@ 2008-07-01  7:14 ` Denys Vlasenko
  0 siblings, 0 replies; 8+ messages in thread
From: Denys Vlasenko @ 2008-07-01  7:14 UTC (permalink / raw)
  To: gcc; +Cc: Sajish V

On Tuesday 01 July 2008 08:38, Sajish V wrote:
> Hi All, 
> Can you please let me know why GCC does not crib when we try to free a static array?
> main ()
> { 
>      char array[100];
>      free (array);
> }
> The above code compiles without any hitch. 
> Thanks,
> Sajish.

# ./a.out
*** glibc detected *** ./a.out: free(): invalid pointer: 0xffa8b530 ***
======= Backtrace: =========
/lib/libc.so.6[0xf7ecb7a7]
/lib/libc.so.6(__libc_free+0x79)[0xf7ecc0ad]
./a.out[0x804834e]
/lib/libc.so.6(__libc_start_main+0x138)[0xf7e8da74]
./a.out[0x80482b5]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:06 1315925                            /.local/tmp/a.out
08049000-0804a000 rwxp 00000000 08:06 1315925                            /.local/tmp/a.out
0804a000-0806b000 rwxp 0804a000 00:00 0                                  [heap]
f7e6f000-f7e76000 r-xp 00000000 08:05 46193                              /app/gcc-3.3.3/lib/libgcc_s.so.1
f7e76000-f7e77000 rwxp 00006000 08:05 46193                              /app/gcc-3.3.3/lib/libgcc_s.so.1
f7e77000-f7e78000 rwxp f7e77000 00:00 0
f7e78000-f7f60000 r-xp 00000000 08:0

--
vda

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

* Re: free (static_array)
@ 2008-07-08 13:59 Sajish V
  0 siblings, 0 replies; 8+ messages in thread
From: Sajish V @ 2008-07-08 13:59 UTC (permalink / raw)
  To: Joseph S. Myers, Michael Meissner; +Cc: Denys Vlasenko, gcc

Thanks everyone!
I will file a feature request for this via bugzilla.

Regards,
Sajish.



----- Original Message ----
From: Joseph S. Myers <joseph@codesourcery.com>
To: Michael Meissner <gnu@the-meissners.org>
Cc: Denys Vlasenko <vda.linux@googlemail.com>; Sajish V <emailsaju@yahoo.com>; gcc@gcc.gnu.org
Sent: Tuesday, July 1, 2008 5:07:05 PM
Subject: Re: free (static_array)

On Tue, 1 Jul 2008, Michael Meissner wrote:

> On Tue, Jul 01, 2008 at 11:50:58AM +0200, Denys Vlasenko wrote:
> > On Tuesday 01 July 2008 09:24, Sajish V wrote:
> > > Thanks for the reply, Denys.
> > > My question was, why doesn't gcc catch it during compilation? It is clear
> > > that we are trying to free a variable from stack. Shouldn't gcc flag an
> > > error for this during compilation?
> 
> It could.  Nobody has written the code to do so, and as they say patches are
> welcome (assuming you have done the paperwork to legally contribute to GCC).
> Have you filed a feature request via bugzilla so that it can be tracked?

An error is inappropriate; undefined behavior only occurs on execution of 
the call to free, not on compilation of the program.  A warning would be 
fine (as would converting the call to free into an abort).

-- 
Joseph S. Myers
joseph@codesourcery.com



      

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

* Re: free (static_array)
  2008-07-01 11:56     ` Joseph S. Myers
@ 2008-07-01 17:35       ` Michael Meissner
  0 siblings, 0 replies; 8+ messages in thread
From: Michael Meissner @ 2008-07-01 17:35 UTC (permalink / raw)
  To: Joseph S. Myers; +Cc: Michael Meissner, Denys Vlasenko, Sajish V, gcc

On Tue, Jul 01, 2008 at 11:37:05AM +0000, Joseph S. Myers wrote:
> On Tue, 1 Jul 2008, Michael Meissner wrote:
> 
> > On Tue, Jul 01, 2008 at 11:50:58AM +0200, Denys Vlasenko wrote:
> > > On Tuesday 01 July 2008 09:24, Sajish V wrote:
> > > > Thanks for the reply, Denys.
> > > > My question was, why doesn't gcc catch it during compilation? It is clear
> > > > that we are trying to free a variable from stack. Shouldn't gcc flag an
> > > > error for this during compilation?
> > 
> > It could.  Nobody has written the code to do so, and as they say patches are
> > welcome (assuming you have done the paperwork to legally contribute to GCC).
> > Have you filed a feature request via bugzilla so that it can be tracked?
> 
> An error is inappropriate; undefined behavior only occurs on execution of 
> the call to free, not on compilation of the program.  A warning would be 
> fine (as would converting the call to free into an abort).

True enough from a standards point of view.  Obviously having a warning would
allow some invalid code to be detected at compilation time.

-- 
Michael Meissner
email: gnu@the-meissners.org
http://www.the-meissners.org

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

* Re: free (static_array)
  2008-07-01 11:37   ` Michael Meissner
@ 2008-07-01 11:56     ` Joseph S. Myers
  2008-07-01 17:35       ` Michael Meissner
  0 siblings, 1 reply; 8+ messages in thread
From: Joseph S. Myers @ 2008-07-01 11:56 UTC (permalink / raw)
  To: Michael Meissner; +Cc: Denys Vlasenko, Sajish V, gcc

On Tue, 1 Jul 2008, Michael Meissner wrote:

> On Tue, Jul 01, 2008 at 11:50:58AM +0200, Denys Vlasenko wrote:
> > On Tuesday 01 July 2008 09:24, Sajish V wrote:
> > > Thanks for the reply, Denys.
> > > My question was, why doesn't gcc catch it during compilation? It is clear
> > > that we are trying to free a variable from stack. Shouldn't gcc flag an
> > > error for this during compilation?
> 
> It could.  Nobody has written the code to do so, and as they say patches are
> welcome (assuming you have done the paperwork to legally contribute to GCC).
> Have you filed a feature request via bugzilla so that it can be tracked?

An error is inappropriate; undefined behavior only occurs on execution of 
the call to free, not on compilation of the program.  A warning would be 
fine (as would converting the call to free into an abort).

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: free (static_array)
  2008-07-01 10:51 ` Denys Vlasenko
@ 2008-07-01 11:37   ` Michael Meissner
  2008-07-01 11:56     ` Joseph S. Myers
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Meissner @ 2008-07-01 11:37 UTC (permalink / raw)
  To: Denys Vlasenko; +Cc: Sajish V, gcc

On Tue, Jul 01, 2008 at 11:50:58AM +0200, Denys Vlasenko wrote:
> On Tuesday 01 July 2008 09:24, Sajish V wrote:
> > Thanks for the reply, Denys.
> > My question was, why doesn't gcc catch it during compilation? It is clear
> > that we are trying to free a variable from stack. Shouldn't gcc flag an
> > error for this during compilation?

It could.  Nobody has written the code to do so, and as they say patches are
welcome (assuming you have done the paperwork to legally contribute to GCC).
Have you filed a feature request via bugzilla so that it can be tracked?

> How gcc can know that free() is not supposed to be used like that?

Unless you use the -ffreestanding or -fno-builtin switches, the compiler is
free to know about any function mentioned in the ISO C standard (or C++,
etc. depending on your language).

-- 
Michael Meissner
email: gnu@the-meissners.org
http://www.the-meissners.org

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

* Re: free (static_array)
  2008-07-01  7:24 Sajish V
@ 2008-07-01 10:51 ` Denys Vlasenko
  2008-07-01 11:37   ` Michael Meissner
  0 siblings, 1 reply; 8+ messages in thread
From: Denys Vlasenko @ 2008-07-01 10:51 UTC (permalink / raw)
  To: Sajish V; +Cc: gcc

On Tuesday 01 July 2008 09:24, Sajish V wrote:
> Thanks for the reply, Denys.
> My question was, why doesn't gcc catch it during compilation? It is clear that we are trying to free a variable from stack. Shouldn't gcc flag an error for this during compilation?

How gcc can know that free() is not supposed to be used like that?
--
vda

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

* Re: free (static_array)
@ 2008-07-01  7:24 Sajish V
  2008-07-01 10:51 ` Denys Vlasenko
  0 siblings, 1 reply; 8+ messages in thread
From: Sajish V @ 2008-07-01  7:24 UTC (permalink / raw)
  To: Denys Vlasenko, gcc

Thanks for the reply, Denys.
My question was, why doesn't gcc catch it during compilation? It is clear that we are trying to free a variable from stack. Shouldn't gcc flag an error for this during compilation?
Regards,
Sajish.



----- Original Message ----
From: Denys Vlasenko <vda.linux@googlemail.com>
To: gcc@gcc.gnu.org
Cc: Sajish V <emailsaju@yahoo.com>
Sent: Tuesday, July 1, 2008 2:44:06 PM
Subject: Re: free (static_array)

On Tuesday 01 July 2008 08:38, Sajish V wrote:
> Hi All, 
> Can you please let me know why GCC does not crib when we try to free a static array?
> main ()
> { 
>      char array[100];
>      free (array);
> }
> The above code compiles without any hitch. 
> Thanks,
> Sajish.

# ./a.out
*** glibc detected *** ./a.out: free(): invalid pointer: 0xffa8b530 ***
======= Backtrace: =========
/lib/libc.so.6[0xf7ecb7a7]
/lib/libc.so.6(__libc_free+0x79)[0xf7ecc0ad]
./a.out[0x804834e]
/lib/libc.so.6(__libc_start_main+0x138)[0xf7e8da74]
./a.out[0x80482b5]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:06 1315925                            /.local/tmp/a.out
08049000-0804a000 rwxp 00000000 08:06 1315925                            /.local/tmp/a.out
0804a000-0806b000 rwxp 0804a000 00:00 0                                  [heap]
f7e6f000-f7e76000 r-xp 00000000 08:05 46193                              /app/gcc-3.3.3/lib/libgcc_s.so.1
f7e76000-f7e77000 rwxp 00006000 08:05 46193                              /app/gcc-3.3.3/lib/libgcc_s.so.1
f7e77000-f7e78000 rwxp f7e77000 00:00 0
f7e78000-f7f60000 r-xp 00000000 08:0

--
vda



      

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

end of thread, other threads:[~2008-07-08  9:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-01  6:39 free (static_array) Sajish V
2008-07-01  7:14 ` Denys Vlasenko
2008-07-01  7:24 Sajish V
2008-07-01 10:51 ` Denys Vlasenko
2008-07-01 11:37   ` Michael Meissner
2008-07-01 11:56     ` Joseph S. Myers
2008-07-01 17:35       ` Michael Meissner
2008-07-08 13:59 Sajish V

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).