public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32984]  New: add checking for array new & delete
@ 2007-08-04 12:21 dcb314 at hotmail dot com
  2007-08-04 13:01 ` [Bug c++/32984] " gdr at cs dot tamu dot edu
                   ` (9 more replies)
  0 siblings, 10 replies; 12+ messages in thread
From: dcb314 at hotmail dot com @ 2007-08-04 12:21 UTC (permalink / raw)
  To: gcc-bugs

Given the following C++ code

class K
{
public:
        void f();
        void g();

private:
        int * a;
        double * b;
        float * c;
        unsigned int * d;
};

void K :: f()
{
        a = new int;
        b = new double [ 10];
        delete c;
        delete [] d;
}

void K :: g()
{
        delete [] a;    // error
        delete b;               // error
        c = new float [ 20];    // error
        d = new unsigned int;   // error
}

Recent snapshot g++ 4.3 20070803 can't find anything
wrong with the code.

Suggest enhance compiler to track use of plain new/delete
and array new/delete and warn where possible to detect 
an error.

Manual memory allocation and de-allocation is enough
of a pain in C++ without total silence from the compiler.


-- 
           Summary: add checking for array new & delete
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dcb314 at hotmail dot com
  GCC host triplet: x86_64-suse-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* Re: [Bug c++/32984]  New: add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
  2007-08-04 13:01 ` [Bug c++/32984] " gdr at cs dot tamu dot edu
@ 2007-08-04 13:01 ` Gabriel Dos Reis
  2007-08-04 19:52 ` [Bug c++/32984] " dcb314 at hotmail dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: Gabriel Dos Reis @ 2007-08-04 13:01 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs

"dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| Given the following C++ code
| 
| class K
| {
| public:
|         void f();
|         void g();
| 
| private:
|         int * a;
|         double * b;
|         float * c;
|         unsigned int * d;
| };
| 
| void K :: f()
| {
|         a = new int;
|         b = new double [ 10];
|         delete c;
|         delete [] d;
| }
| 
| void K :: g()
| {
|         delete [] a;    // error
|         delete b;               // error
|         c = new float [ 20];    // error
|         d = new unsigned int;   // error
| }
| 
| Recent snapshot g++ 4.3 20070803 can't find anything
| wrong with the code.

Special, dedicated tools exist for that task.  I would suggest
you use one of them.  

The above should not be the business of the *compiler*.

-- Gaby


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
@ 2007-08-04 13:01 ` gdr at cs dot tamu dot edu
  2007-08-04 13:01 ` [Bug c++/32984] New: " Gabriel Dos Reis
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: gdr at cs dot tamu dot edu @ 2007-08-04 13:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from gdr at cs dot tamu dot edu  2007-08-04 13:01 -------
Subject: Re:   New: add checking for array new & delete

"dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| Given the following C++ code
| 
| class K
| {
| public:
|         void f();
|         void g();
| 
| private:
|         int * a;
|         double * b;
|         float * c;
|         unsigned int * d;
| };
| 
| void K :: f()
| {
|         a = new int;
|         b = new double [ 10];
|         delete c;
|         delete [] d;
| }
| 
| void K :: g()
| {
|         delete [] a;    // error
|         delete b;               // error
|         c = new float [ 20];    // error
|         d = new unsigned int;   // error
| }
| 
| Recent snapshot g++ 4.3 20070803 can't find anything
| wrong with the code.

Special, dedicated tools exist for that task.  I would suggest
you use one of them.  

The above should not be the business of the *compiler*.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
  2007-08-04 13:01 ` [Bug c++/32984] " gdr at cs dot tamu dot edu
  2007-08-04 13:01 ` [Bug c++/32984] New: " Gabriel Dos Reis
@ 2007-08-04 19:52 ` dcb314 at hotmail dot com
  2007-08-04 22:06   ` Gabriel Dos Reis
  2007-08-04 22:06 ` gdr at cs dot tamu dot edu
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 12+ messages in thread
From: dcb314 at hotmail dot com @ 2007-08-04 19:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from dcb314 at hotmail dot com  2007-08-04 19:52 -------
(In reply to comment #1)
> Special, dedicated tools exist for that task.  

Would you be willing to name three of them ?

> The above should not be the business of the *compiler*.

Why not ?

The compiler can generate a whole bunch of warnings
already.

Are you suggesting these existing warnings are none
of the business of the compiler ?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
                   ` (2 preceding siblings ...)
  2007-08-04 19:52 ` [Bug c++/32984] " dcb314 at hotmail dot com
@ 2007-08-04 22:06 ` gdr at cs dot tamu dot edu
  2007-08-04 22:10 ` gdr at cs dot tamu dot edu
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: gdr at cs dot tamu dot edu @ 2007-08-04 22:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from gdr at cs dot tamu dot edu  2007-08-04 22:06 -------
Subject: Re:  add checking for array new & delete

"dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| The compiler can generate a whole bunch of warnings
| already.

Which fall in different mindset that the one you would like.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* Re: [Bug c++/32984] add checking for array new & delete
  2007-08-04 19:52 ` [Bug c++/32984] " dcb314 at hotmail dot com
@ 2007-08-04 22:06   ` Gabriel Dos Reis
  0 siblings, 0 replies; 12+ messages in thread
From: Gabriel Dos Reis @ 2007-08-04 22:06 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs

"dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| The compiler can generate a whole bunch of warnings
| already.

Which fall in different mindset that the one you would like.

-- Gaby


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
                   ` (3 preceding siblings ...)
  2007-08-04 22:06 ` gdr at cs dot tamu dot edu
@ 2007-08-04 22:10 ` gdr at cs dot tamu dot edu
  2007-08-05  0:31 ` sebor at roguewave dot com
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: gdr at cs dot tamu dot edu @ 2007-08-04 22:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from gdr at cs dot tamu dot edu  2007-08-04 22:09 -------
Subject: Re:  add checking for array new & delete

"dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| (In reply to comment #1)
| > Special, dedicated tools exist for that task.  
| 
| Would you be willing to name three of them ?

web searchers are your friends.  look for "valgrind" and friends.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
                   ` (4 preceding siblings ...)
  2007-08-04 22:10 ` gdr at cs dot tamu dot edu
@ 2007-08-05  0:31 ` sebor at roguewave dot com
  2007-08-05 19:37 ` pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: sebor at roguewave dot com @ 2007-08-05  0:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from sebor at roguewave dot com  2007-08-05 00:31 -------
There are third party tools that track these types of problems. Some of them
have started to make their way into compilers. For example, the HP static
analysis tool called Code Adviser is integrated into the HP aCC compiler on
IPF (see www.hp.com/go/cadvise).


-- 

sebor at roguewave dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sebor at roguewave dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
                   ` (5 preceding siblings ...)
  2007-08-05  0:31 ` sebor at roguewave dot com
@ 2007-08-05 19:37 ` pinskia at gcc dot gnu dot org
  2007-08-06 16:06 ` dcb314 at hotmail dot com
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-08-05 19:37 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pinskia at gcc dot gnu dot org  2007-08-05 19:37 -------
This is not the business of a compiler to detect all and every programming
mismatch.   It can detect some but not all.  This is one which you need huge
dataflow analysis and whole program to detect this problem.

This is not going to be inside GCC for a long time and even then it will not
give good results.

So closing as won't fix.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |WONTFIX


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
                   ` (6 preceding siblings ...)
  2007-08-05 19:37 ` pinskia at gcc dot gnu dot org
@ 2007-08-06 16:06 ` dcb314 at hotmail dot com
  2007-08-09  2:54 ` fang at csl dot cornell dot edu
  2007-08-09 16:03 ` dcb314 at hotmail dot com
  9 siblings, 0 replies; 12+ messages in thread
From: dcb314 at hotmail dot com @ 2007-08-06 16:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from dcb314 at hotmail dot com  2007-08-06 16:06 -------
(In reply to comment #6)
> This is one which you need huge dataflow analysis 

Doubtful. Yes/No/Don't know flag on each pointer data
member of a class would be some of it.

> and whole program to detect this problem.

I'd be happy with a one file solution to this problem, as I hope
my original problem report made clear.

In summary, my customer doesn't have HP kit, valgrind is run time 
and not compile time, the compiler isn't going to be fixed anytime soon
so I feel a Perl script may be invented.

> So closing as won't fix.

Fair enough. Point taken. I'll try a Perl script.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
                   ` (7 preceding siblings ...)
  2007-08-06 16:06 ` dcb314 at hotmail dot com
@ 2007-08-09  2:54 ` fang at csl dot cornell dot edu
  2007-08-09 16:03 ` dcb314 at hotmail dot com
  9 siblings, 0 replies; 12+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-08-09  2:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from fang at csl dot cornell dot edu  2007-08-09 02:54 -------
Please forgive a wee bit more noise on this matter: (Yes, I know this is
resolved invalid)

If you really insist on using a pointer instead of a valarray or vector, I
suggest taking a technique from the STL and using an auto_array class template
that delete []'s an array-allocated pointer automatically upon destruction
(exception-safe too).  Overload for member operator [] to do pointer
arithmetic, for convenience.  

Taking the paradigm further, you can devise things like boost::shared_array
(TR1?), or some sort of policy-driven pointer-class that performs The
Appropriate Action (TM) upon destruction.  If you're extremist, like me, and
want to find/substitute *all* bare occurrences of operators new and delete
(outside of certified pointer/array/vector classes), 'cpp | grep' for them, no
g++ required.  :)  I can't remember the last time I botched any operation
mismatch/typo/bug/error/unbalance.  

Of course, if you didn't actually make such an error, and the example was just
a demonstration of your point, then I'm probably just preaching to the choir. 
[returns to lurking]


-- 

fang at csl dot cornell dot edu changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fang at csl dot cornell dot
                   |                            |edu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

* [Bug c++/32984] add checking for array new & delete
  2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
                   ` (8 preceding siblings ...)
  2007-08-09  2:54 ` fang at csl dot cornell dot edu
@ 2007-08-09 16:03 ` dcb314 at hotmail dot com
  9 siblings, 0 replies; 12+ messages in thread
From: dcb314 at hotmail dot com @ 2007-08-09 16:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from dcb314 at hotmail dot com  2007-08-09 16:03 -------
(In reply to comment #8)
> I suggest taking a technique from the STL and using an auto_array class 

In practice, I find both STL and Boost are rarely used.
Such advanced tools are fine for experienced C++ programmers.

For ex-Fortran programmers, new to C++, making their first steps in
C++ manual memory management, the news that the C++ compiler will silently
eat certainly wrong code is worrying.

Hence my bug report. Some newbie C++ programmers need more hand-holding
than traditional C++ compilers provide.

And as C++ becomes more and more mainstream, this will be more and more
true. 

The bug report remains at resolved wontfix.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32984


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

end of thread, other threads:[~2007-08-09 16:03 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-04 12:21 [Bug c++/32984] New: add checking for array new & delete dcb314 at hotmail dot com
2007-08-04 13:01 ` [Bug c++/32984] " gdr at cs dot tamu dot edu
2007-08-04 13:01 ` [Bug c++/32984] New: " Gabriel Dos Reis
2007-08-04 19:52 ` [Bug c++/32984] " dcb314 at hotmail dot com
2007-08-04 22:06   ` Gabriel Dos Reis
2007-08-04 22:06 ` gdr at cs dot tamu dot edu
2007-08-04 22:10 ` gdr at cs dot tamu dot edu
2007-08-05  0:31 ` sebor at roguewave dot com
2007-08-05 19:37 ` pinskia at gcc dot gnu dot org
2007-08-06 16:06 ` dcb314 at hotmail dot com
2007-08-09  2:54 ` fang at csl dot cornell dot edu
2007-08-09 16:03 ` dcb314 at hotmail dot com

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