Solved

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

Posted on 2014-02-28
4
309 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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.

763 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

9 Experts available now in Live!

Get 1:1 Help Now