Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Global variables (and function local statics) with constructors can cause a lot of unexpected problems in many situations. As programs grow in size and complexity these things can get to be very hard to manage.</p> <p>You will be better off if you manage them explicitly - because then you get explicit control over the order of construction/destruction and when these things happen.</p> <p>If you use a vector as suggested above, then as the program exits the vector will be freed. But, you cannot directly control the order in which this happens, so if getNextPass() is called as part of something else which is being cleaned up (so, after main() returns), it will likely crash and you will have to puzzle out why and how to get the ordering correct.</p> <p>Also note that function local static initialization is not thread-safe in general. GCC has a thread-safe initialization mechanism, but other compilers (like VC) do not. Even when supported it isn't free and may require an option to enable.</p> <p>Doing it manually (very similar to the auto-generated code by the compiler):</p> <pre><code>inline char* getNextPass() { static bool initialized; static int chars_num; static int pass_len; static int *cur_pos; if (!initialized) { chars_num = chars_data.charset_len; pass_len = chars_data.pass_len ; cur_pos = new int[pass_len]; for(int aa = 0; aa &lt; pass_len; aa++) cur_pos[aa] = 0 ; initialized = true; } ... } </code></pre> <p>To clarify a bit "with constructors" means an initialization which requires code to execute to do. So, "static int x = 5;" does not, but "static int y = rand();" does.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload