?
Solved

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

Posted on 2014-02-28
4
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 2000 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 2000 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

Independent Software Vendors: 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

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…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

771 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