Solved

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

Posted on 2014-02-28
4
313 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now