-4

I'm reading "hacking, The Art of Exploitation" book, and this code sample really confuses me.

It's within the context of Global Variable Scope:

#include <stdio.h>

void function() { // An example function, with its own context
    int var = 5;
    static int static_var = 5; // Static variable initialization
    printf("\t[in function] var = %d\n", var);
    printf("\t[in function] static_var = %d\n", static_var);
    var++; // Add one to var.
    static_var++; // Add one to static_var.
}

int main() { // The main function, with its own context
    int i;
    static int static_var = 1337; // Another static, in a different context
    for(i=0; i < 5; i++) { // Loop 5 times.
        printf("[in main] static_var = %d\n", static_var);
        function(); // Call the function.
    }
}

And here is the output:

reader@hacking:~/booksrc $ gcc static.c
reader@hacking:~/booksrc $ ./a.out
[in main] static_var = 1337
    [in function] var = 5
    [in function] static_var = 5
[in main] static_var = 1337
    [in function] var = 5
    [in function] static_var = 6
[in main] static_var = 1337
    [in function] var = 5
    [in function] static_var = 7
[in main] static_var = 1337
    [in function] var = 5
    [in function] static_var = 8
[in main] static_var = 1337
    [in function] var = 5
    [in function] static_var = 9
reader@hacking:~/booksrc $

The question is that, WHY [in function] var = 5 remains steady? We defined var++; as well as static_var++; within the local function. What is happening?

7
  • 2
    Because that's how non-static local variables work?
    – DCoder
    Jan 12, 2013 at 18:36
  • What behavior do you expect to differ between static and non-static variable? Jan 12, 2013 at 18:38
  • 6
    Why is the title of this question "weird behavior of a static variable" when you are very clearly asking about a non-static (local) variable? Jan 12, 2013 at 18:38
  • PS, I have to ask: This is chapter 1 (or 0) of N in a book about exploitation. What is N? Jan 12, 2013 at 18:44
  • Thanks for your comments .. I changed the title of my question ...
    – Soask
    Jan 12, 2013 at 18:52

3 Answers 3

5

After the

var++;

in function(), that local variable is not used anymore. It goes out of scope and ceases to exist when the function returns. When function() is invoked the next time, a new var is allocated and initialised to 5.

The two static_vars have static storage duration, they exist throughout the lifetime of the programme. Any modification to these persists and they are only initialised once.

So when function() is invoked the next time, the increment done in the previous invocation is still in effect, the initialisation is not executed except in the very first invocation.

0
3

Because var is not static. It's a different instance each time the function gets called.

0
2

This is precisely what distinguishes a static variable from a non-static one. A static variable has static-storage-duration, meaning the variable stays alive during the run of the program. Non-static variables have automatic storage duration; meaning it will disappear at the end of the scope in which it was created (in this case, at the end of function). When the variable is disposed of, the next time the function is invoked, a new instance of that variable will be created.

Not the answer you're looking for? Browse other questions tagged or ask your own question.