CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,828

    A static global variable in a DLL

    Am I doing something stupid here?? The following code is in a DLL:-

    Code:
    // In a header file while building the DLL (i.e. assume that
    // LIBTEMPORAL_API handles import or export, as appropriate)
    
    namespace Temporal {
    
    	typedef int64_t superclock_t;
    
    	static superclock_t _superclock_ticks_per_second = 282240000;
    
    	LIBTEMPORAL_API void set_superclock_ticks_per_second (superclock_t sc);
    
    }
    
    
    // In one of the DLL's source files
    void
    Temporal::set_superclock_ticks_per_second (Temporal::superclock_t sc)
    {
    	_superclock_ticks_per_second = sc; // <--- Never changes the value of _superclock_ticks_per_second  !!!!
    }
    It's almost as if _superclock_ticks_per_second is being treated as const (or are static globals in a DLL automatically const ??
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,800

    Re: A static global variable in a DLL

    Why is _superclock_ticks_per_second static? What is trying to be achieved?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,828

    Re: A static global variable in a DLL

    I don't know - it must've been a limitation in some older version of MSVC. Or maybe it was originally a class member and needed to have the same value for all instances of the class

    I'm just surprised that it isn't declared as const and yet it doesn't seem to be changeable

    [Edit...] I guess this might be significant... I tried the following search in Google:-

    +"C++" +"global variables" +"static"
    and the first entry says this:-

    A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope. In C, the
    preprocessor directive #define was used to create a variable with a constant value. This still works in C++, but problems could arise.
    So if I'm reading that correctly, if the header file needs to get #included by some other library (rather than libtemporal in this case) the word static tells the compiler that the other library won't neeed access to _superclock_ticks_per_second - but the downside is that _superclock_ticks_per_second will effectively be const
    Last edited by John E; October 4th, 2023 at 05:21 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,800

    Re: A static global variable in a DLL

    static at a file level says that the variable can't be used outside of the file in which it resides ie. it can't be used by another compilation unit. I'm suspicious that if you have multiple files all using the same header file then you have multiple instances of the variable - each specific to its own file. So each file is changing it's version only.

    If you want one copy of _superclock_ticks_per_second then consider a singleton.
    Last edited by 2kaud; October 4th, 2023 at 06:38 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured