Thread: Static variable inside member function?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Static variable inside member function?

    Code:
    void Test::init()
    {
        static int times_called = 0;
        ++times_called;
    	
        // This should never happen!
        assert(times_called > 1);
    }
    I only have one single call to the init function in the code, and I still get this:
    Code:
    Assertion failed: (times_called > 1), function init, file ...
    Program received signal:  “SIGABRT”.
    mi_cmd_stack_list_frames: Not enough frames in stack.
    mi_cmd_stack_list_frames: Not enough frames in stack.
    Can't you use static variables like this inside class member functions?

    EDIT: Even without the assert I still get the "not enough frames" error.
    Last edited by Memloop; 11-26-2009 at 10:49 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that the correct assertion is:
    Code:
    assert(times_called > 0);
    Anyway, I am unable to duplicate your error:
    Code:
    #include <cassert>
    
    class Test
    {
    public:
        void init();
    };
    
    int main()
    {
        Test test;
        test.init();
    }
    
    void Test::init()
    {
        static int times_called = 0;
        ++times_called;
    
        // This should never happen!
        assert(times_called > 0);
    }
    Post the smallest and simplest (compilable) program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You're right, changing the assert (or setting times_called to 1 instead) makes the code run. I guess the "out of frames" error was from the debugger for some reason.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I have one question though: Does the static variable inside the function apply to all instances of the class, or will it be initialized every time a new instance is created?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream>
    
    class foo
    {
    public:
    	void foo2()
    	{
    		static int n = 0;
    		std::cout << &n << std::endl;
    	}
    };
    
    int main()
    {
    	foo foo1;
    	foo foo2;
    	foo foo3;
    	foo1.foo2();
    	foo2.foo2();
    	foo3.foo2();
    }
    Output:
    01317138
    01317138
    01317138
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  2. Replies: 6
    Last Post: 12-13-2007, 08:20 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM