Thread: where are static values stored?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But back to the point in question, I don't think the variable is question is a variable, I think it
    just tells the compiler that that function returns an integer, thats all, nothing more. [...] it would only exist temporilary on the stack when the function had been called and
    was returning it's return value, so it is really a dynamic variable.
    I don't want to claim that the compiler wouldn't optimize my snippet in the crude manner that you implied; however my snippet is not "in question." I was demonstrating my answer: That statics are stored in the same place as globals with access restrictions. My snippet was more of a test to see if you could globally poke at the value of something static. Had you not been able to, the assertion should have failed, (like it would for a pointer to a local automatic variable).

    What you later stated is a possibility, but static variables cannot be destroyed at any point in run time (as dynamic ones can). As I stated, statics could be copied around from the function stack like you said. But the lifetime of the static variable is well defined, unlike a dynamic one: as long as from the point of initialization to the termination of your program. There's no need to explicitly manage static memory, and although implementors may opt to do that behind the scenes, I don't think that is the case for many. Personally, I wouldn't do it that way.

    OP: c will live just as I described but it may point to other places in memory that make c invalid.
    Last edited by whiteflags; 03-02-2008 at 11:28 PM.

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by citizen View Post
    I don't want to claim that the compiler wouldn't optimize my snippet in the crude manner that you implied; however my snippet is not "in question." I was demonstrating my answer: That statics are stored in the same place as globals with access restrictions. My snippet was more of a test to see if you could globally poke at the value of something static. Had you not been able to, the assertion should have failed, (like it would for a pointer to a local automatic variable).

    What you later stated is a possibility, but static variables cannot be destroyed at any point in run time (as dynamic ones can). As I stated, statics could be copied around from the function stack like you said. But the lifetime of the static variable is well defined, unlike a dynamic one: as long as from the point of initialization to the termination of your program. There's no need to explicitly manage static memory, and although implementors may opt to do that behind the scenes, I don't think that is the case for many. Personally, I wouldn't do it that way.

    OP: c will live just as I described but it may point to other places in memory that make c invalid.
    Well my point is basically that the question is a false one, he is not declaring a static
    variable in this case, merely informing the compiler that the function returns an integer.

    It's a fairly academic question to me anyway, my use of passing variable back and forth
    between functions is somewhat 'limited' shall we say.

    And in answer to the OP question, when function is called a second time it merely
    retrives the return value of the stack as it did in the first call.

    I mean how could you access this static variable any way?

    Code:
    int somevariable;
    
    somevariable=do_something;
    That is, I am fairly sure going to return an error similar to:-
    "Oi!! you cannot do that, incompatible types, somevariable is an integer and do_something is a pointer to a function.

    You *might* get away with:-

    Code:
    int somevariable;
    
    somevariable=(int)do_something;
    But I can't see it being of any use.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by esbo View Post
    "Oi!! you cannot do that, incompatible types, somevariable is an integer and do_something is a pointer to a function.
    This is not a way to call a function in C. If you do not know this bit of info about C - why you still trying to answer any question here?

    You just confusing people with such a crap.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    This is not a way to call a function in C. If you do not know this bit of info about C - why you still trying to answer any question here?

    You just confusing people with such a crap.
    I never implied it was a way to call a function hence the rest of your post is based up on
    you initial false assumption and thus the 'crap' would appear to be a product of your own
    mind.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by esbo View Post
    Well my point is basically that the question is a false one, he is not declaring a static
    variable in this case, merely informing the compiler that the function returns an integer.
    Quote Originally Posted by esbo View Post
    I never implied it was a way to call a function hence the rest of your post is based up on
    you initial false assumption and thus the 'crap' would appear to be a product of your own
    mind.
    If we ask you another time, will we get another answer?

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by tabstop View Post
    If we ask you another time, will we get another answer?
    Well you will get a reply, whether you get and answer depends to a large extent upon
    whether your 'question' is comprehensible which unfortunately it is not on this occasion.
    ( obviously I mean comprehensibe to someone other than yourself).

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well my point is basically that the question is a false one, he is not declaring a static
    variable in this case, merely informing the compiler that the function returns an integer.

    It's a fairly academic question to me anyway, my use of passing variable back and forth
    between functions is somewhat 'limited' shall we say.
    But the pointer is a static variable, and he doesn't seem to be using functions in any sort of awkward way (admittedly I'm losing track of what you really are saying).

    Just because he is not taking advantage of the fact that the value is alive for the whole program in this particular snippet, doesn't mean that he wouldn't take advantage in a real project. Static variables are like globals, but because they have scope, it can be easier to locate where errors happen. Some statics aren't even constant: some counters could be static (as in, maybe, reference counters in C++).

    I mean how could you access this static variable any way?
    That being the point: you use a static variable while it is in scope; it sits there otherwise. Anything that might happen, such as passing the value of a static variable to another function, is pretty seamless because you are passing by value. It's more to the point that a static variable has it's value preserved.

    The rest of your statements are irrelevant. No one is using the functions we've written in the manner you describe.
    Last edited by whiteflags; 03-03-2008 at 01:56 AM. Reason: further comments!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  2. static object never inits
    By krappa in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2008, 12:04 PM
  3. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  4. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  5. program to unpack packed data
    By vsk in forum C Programming
    Replies: 5
    Last Post: 11-14-2002, 09:17 PM