Static variables

This is a discussion on Static variables within the C Programming forums, part of the General Programming Boards category; Hello, Static variables are stored in initialised rw memory in the process memory area. Can anyone tell me what does ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    9

    Static variables

    Hello,
    Static variables are stored in initialised rw memory in the process memory area. Can anyone tell me what does the OS does internally in order to save its contents, if the variable is defined and used inside a function and called twice?
    suresh

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Here is a good page that answers all your questions:

    http://www.purdue.edu/PUCC/Short-Cou...s/p_00200.html

    Cheers,
    Z.
    Beware the fury of a patient man.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Can anyone tell me what does the OS does internally in order to save its contents, if the variable is defined and used inside a function and called twice?
    What do you mean by this??
    Saravanan.T.S.
    Beginner.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can anyone tell me what does the OS does internally in order to save its
    >contents, if the variable is defined and used inside a function and called twice?
    Local variables exist on the stack. They're pushed onto the stack when the function is entered and popped off the stack when the function exits. static variables are stored in the data segment of your executable. Since they aren't stored on the stack, and thus not created and destroyed at runtime, the memory exists throughout the program's execution. That's why static variables save their contents between function calls.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    Perfect, but in which part of data segment does it exits, does it exists in initialised read/write memory as even if you dont initialize a static variable , it gets zero value?

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Yes it is stored in intialized data area and those variables are intialized to zero implicitly.
    Saravanan.T.S.
    Beginner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 02:16 AM
  2. Static variables
    By ashughs in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2006, 09:21 AM
  3. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 06:35 AM
  4. Visual C++ 6/.net Debug static variables
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 03:45 AM
  5. Replies: 5
    Last Post: 11-19-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21