Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
C does have scoped variables, where a variable only exists within a pair of braces. When I was writing C code that got cross-compiled to 68000 assembler, I could see that the compiler just assigned space on the stack for that type of variable by moving the stack pointer down, and once the scope ended, the stack pointer was just adjusted back up.

That's quite typical for a C compiler, and C works that way on all common platforms (x86, ARM, AVR, ...).

C also has the static keyword, which says the variable is to be stored in the lexical scope, meaning it's created at the beginning of the run (perhaps on the stack, but not anywhere that's going to get re-used), and only initialized once, if that initialization happens during declaration.

No, static variables inside a function are NOT on the stack. They work like normal static variables, except that the name of the variable is known only inside the function scope. If static local variables were on the stack, their content would be lost on return from the function.

Example (on x86-64 Linux):

/tmp>cat addr.c #include <stdio.h> static int global; void func(int param) { int local; static int static_local; printf( "global: %16p\n" "static local: %16p\n" "local: %16p\n" "param: %16p\n", (void *)&global, (void *)&static_local, (void *)&local, (void *)&param ); } int main(int argc, char ** argv) { func(0); return 0; } /tmp>gcc -o addr -Wall -pedantic addr.c /tmp>./addr global: 0x601048 static local: 0x60104c local: 0x7ffc40f001cc param: 0x7ffc40f001bc /tmp>

The printf output is quite obvious: the global and the static local variable follow each other without any gap, in the data area. The local variable is far away, on the stack, as is the function parameter.


Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^6: What technical benfits perl offers over python + few more questions. by afoken
in thread What technical benfits perl offers over python + few more questions. by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2022-05-20 16:36 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (75 votes). Check out past polls.