Like Tree1Likes
  • 1 Post By kmdv

Difference between static variable and global variable in C++?

This is a discussion on Difference between static variable and global variable in C++? within the C++ Programming forums, part of the General Programming Boards category; Difference between static variable and global variable in C++?...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    30

    Difference between static variable and global variable in C++?

    Difference between static variable and global variable in C++?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    242
    a global variable isn't defined inside a class, whereas a static variable is. a global variable dies with its file, whereas a static variable dies when no instance of the class in which it is defined exists.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    That's a pretty poor description.
    Regarding what they are: the two different types of variables have different visibility scopes. A static variable will only be visible (that is, accessible) inside the function it is defined. Alternatively, if it resides in a class, it acts like a global. Globals exists on a global level; that is, that can be accessed anywhere.
    Re lifetime: both types of variables dies first when the program ends.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    Well thanks for your replies.
    I would like discuss this further..
    So static variables in class,are being shared among the different objects for that class.
    Static variables in general are being shared among different functions in a program.
    Global variables are being shared among different functions and/or files.
    Are above statements correct ??

    Also, if some variable is required to accessed form different functions in a program, which one is generally preferred, global or static ?

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Static variables in general are being shared among different functions in a program.
    static LOCAL variables are just global variables, but can be accessed only from the function in which they are declared. And there is one important thing: they are initialized not on the program startup, but with the first call of the function. This means - static local variables kill multithreading.

    Personally, I never make static (variable) fields, unless they are marked as 'const'. But still, class marked as 'const' might not be thread-safe - it may contain mutable fields for example.

    Also, if some variable is required to accessed form different functions in a program, which one is generally preferred, global or static ?
    Rethink your design / pass this variable as parameter, etc.
    But if you really want to do this, make it a static field, so it can be hidden, and you will know what class is using it.
    Last edited by kmdv; 09-27-2010 at 11:33 AM.
    antred likes this.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,365
    Quote Originally Posted by OCcounty View Post
    So static variables in class,are being shared among the different objects for that class.
    yes, and they can be visible to the entire program, depending on their access level (public, protected, private)

    Static variables in general are being shared among different functions in a program.
    a static variable in a function is visible only to that function, but retains its value between function calls - it is not a stack variable.

    a static variable NOT in a function is visible (by name) only to the translation unit (source file) in which it is defined. it is declared in a similar way to a global variable, but it is local to the translation unit.

    Global variables are being shared among different functions and/or files.
    a global variable is visible to the entire program, regardess of which translation unit is trying to access it.

    Also, if some variable is required to accessed form different functions in a program, which one is generally preferred, global or static ?
    I'd say it's better practice to have a class with static variables, even if that's the only purpose for the class. global variables are generally frowned upon, although as with any rule, there are exceptions.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    I fail to see how they are any safer than globals with regards to multi-threading. They are equally dangerous in that light.
    Wrapping statics inside a class merely to make them global defeats the entire purpose.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Aisthesis View Post
    a global variable isn't defined inside a class, whereas a static variable is. a global variable dies with its file, whereas a static variable dies when no instance of the class in which it is defined exists.
    That's not right. The only difference between a global variable and a static member variable is scope. The name of a global is accessible from everywhere -- the static member is only accessible through its class scope, and thus is subject to protection rules.

    In all other respects they behave identically. It certainly does not matter when/whether a member of the class is constructed/destructed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Memory which is allocated on the stack or the heap can be thought of as dynamic because its allocation depends on the flow of program control.

    Memory which is allocated once when the program is loaded and then simply exists throughout the lifetime of the program is static because its allocation doesn't depend on any program control structures, the memory gets allocated when the program is loaded. Static memory is essentially allocated by the program loader and not the program itself.

    That's the idea anyway, what the keyword static actually does is tell the language how to scope the static data. For example, a class can have a static variable as a member, but that member would not be bound to any particular object of that class, it exists statically within the program. However, the static member is linguistically restricted to the scope in which it was defined (including public/protected/private access). The same thing happens when you declare a variable within a function as static.

    It's all about scope.




    edit:
    Sorry, some people prefer to call heap memory dynamic and stack memory automatic but when I say dynamic here I basically mean "non-static" and IMO I think it helps make clear what static memory is.
    Last edited by BMJ; 09-27-2010 at 04:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Replies: 2
    Last Post: 10-02-2004, 10:12 AM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21