Thread: Storage Class

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    Storage Class

    I"m confused about his program that i wrote just to see what would happen:
    Code:
    #include <iostream.h>
    
    int count = 0;
    void func();
    
    
    void main()
    	{
    	for(int j=0; j<10; j++)
    		func();
    	cout << "I have been excecuted " << count << " times.";
    	}
    void func()
    	{
    	count++;
    	}
    i wanted to see if 'j' would be reset to 0, and i expected it would have. But it wasn't. I thought automatic variables only keep their values and place in memory when the function which they are defined is called, and when excecution leaves the function the varibales are destroyed and their place in memory is no longer used. Why then would ''j' retain it's value. Doesn it has something to do with the loop that it's in?

    #include <Jesus.h>
    It will save your life

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I don't see what you mean, j is local to the scope of the for loop, it remains that way until you leave the loop, even if you call functions inside the loop.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    I hope i am right in drawing this conclusion but it seems to me that you assume that if you call func all the variables that are local to main will be destroyed. This assumption is wrong, it is not till you hit the return statement (or the final bracket in the case of void functions) that all local variables are destroyed. in you example you have main call func. func reaches it end and the memory allocated for count is set free, main however never reaches an end till the entire program is done. This results in J being kept whereas count is not because func is ended but main is not.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    cool cool cool

    thanks for clarifying that DirX. glad you replied
    #include <Jesus.h>
    It will save your life

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    glad to have been of service

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    in you example you have main call func. func reaches it end and the memory allocated for count is set free, main however never reaches an end till the entire program is done. This results in J being kept whereas count is not because func is ended but main is not.
    Sorry thats wrong. count is global, it will not be destroyed until after main exits. j on the other hand is local to the loop and cannot be accessed outside the loop being as it has been destroyed when you exited the loop. Remember your scoping rules.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Replies: 3
    Last Post: 07-08-2008, 10:01 AM
  3. Replies: 5
    Last Post: 07-17-2006, 03:13 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM