public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/9156: static variables in member functions should not retain value across class instances
@ 2003-01-03 12:16 stashelp
  0 siblings, 0 replies; 2+ messages in thread
From: stashelp @ 2003-01-03 12:16 UTC (permalink / raw)
  To: gcc-gnats


>Number:         9156
>Category:       c++
>Synopsis:       static variables in member functions should not retain value across class instances
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jan 03 04:16:02 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Will Styles
>Release:        3.2-1mdk
>Organization:
>Environment:
Mandrake Linux 9.0
>Description:
#include <stdio.h>
#include <string.h>

class CLASS
{
public:
        int FUNCTION (void)
        {
                static int val = 0;
                return val++;
        }
};

int main (void)
{
        CLASS *class1 = new CLASS;
        printf ("%i\n", class1->FUNCTION ());
        delete class1;

        CLASS *class2 = new CLASS;
        printf ("%i\n", class2->FUNCTION ());
        delete class2;

        CLASS *class3 = new CLASS;
        printf ("%i\n", class3->FUNCTION ());
        delete class3;

        return 0;
}

the above program imho should print:
0
0
0

but instead prints:
0
1
2

, despite the fact that a different instance of CLASS::FUNCTION is being used each time, so it should return 0 _every_ single time

>How-To-Repeat:

>Fix:
don't implement static variables in member functions like static variables in ordinary functions (or static member functions) as hidden globals, but rather as hidden class members (disclaimer: i don't actually know internals of gcc)
>Release-Note:
>Audit-Trail:
>Unformatted:


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

* Re: c++/9156: static variables in member functions should not retain value across class instances
@ 2003-01-03 14:06 ehrhardt
  0 siblings, 0 replies; 2+ messages in thread
From: ehrhardt @ 2003-01-03 14:06 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, nobody, stashelp

Synopsis: static variables in member functions should not retain value across class instances

State-Changed-From-To: open->closed
State-Changed-By: cae
State-Changed-When: Fri Jan  3 06:06:42 2003
State-Changed-Why:
    Not a bug. The variable is declared static and 3.7.1 states that
    the storage of static variables shall last for the duration
    of the program.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9156


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

end of thread, other threads:[~2003-01-03 14:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-03 12:16 c++/9156: static variables in member functions should not retain value across class instances stashelp
2003-01-03 14:06 ehrhardt

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