Solved

repeated initialisation of static variable doesn't overwrite previous value?

Posted on 2014-02-28
4
323 Views
Last Modified: 2014-02-28
hey guys, i saw this piece of code.

// main.m
#import <Foundation/Foundation.h>

int countByTwo() {
    static int currentCount = 0;
    currentCount += 2;
    return currentCount;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"%d", countByTwo());    // 2
        NSLog(@"%d", countByTwo());    // 4
        NSLog(@"%d", countByTwo());    // 6
    }
    return 0;
}

Open in new window


when the countByTwo function is called repeatedly, it always hits the line

   static int currentCount = 0;

however from the main method 's comments, it seems that the currentCount variable isn't set to 0 again even though it hits the initialisation line multiple time.

Question: if we have a line "static int currentCount = 0;", when it is run multiple times LVVM will check if currentCount already has a value other than 0 and if it does it will skip the setting of the value to 0 is that correct?

thanks in advance guys!
0
Comment
Question by:developingprogrammer
  • 2
  • 2
4 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 39896650
All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified.
0
 

Author Comment

by:developingprogrammer
ID: 39896655
whao thanks for the super speedy reply ozo!

sorry for being dense here - but so it means to say that the initialisation line will be skipped each time the countByTwo function is called - because it has already been initialised before programme startup - is that correct?

thanks once again ozo!
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39896658
From the ISO/IEC C Standard:
An object whose identifier is declared with external or internal linkage, or with the
storage-class specifier static has static storage duration. Its lifetime is the entire
execution of the program and its stored value is initialized only once, prior to program
startup.
0
 

Author Comment

by:developingprogrammer
ID: 39896666
whao perfect! thanks ozo! i do have some problems finding help for some words and i am trying to learn how i can self help instead of asking silly questions. do you think you can help me take a look at this other question ozo - relating to self help on objective c? thanks! = )

http://www.experts-exchange.com/Programming/Languages/C/Q_28377602.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question