title: c++ pointer to function and void pointers iteraction resulting in wierd things
body: <p>Im making a little project at home about genetic algorithm. But im trying to make it generic, so i use pointers to function and void pointers. but i think it might be making some problems.</p>
  
  <p>The main goal of this section of the project is to get a pointer to a function, which return a certain struct. The struct containing a void pointer
  and when im trying to view the value of where it points too it isn`t quite right.I suspect that maybe the interaction between these two might be causing me some problems.</p>
  
  <p>details:</p>
  
  <p>struct:</p>
  
  <pre><code>struct  dna_s{
      int         size;
      void        *dna;
  };
  </code></pre>
  
  <p>population is a class contaning all the population for the process. besides, it contains 2 functions as well, init_func and fitter_func which are both pointers to functions.</p>
  
  <p>pointer to function definition:</p>
  
  <pre><code>typedef dna_s   (*init_func_t)();
  typedef int     (*fitter_func_t)(dna_s);
  </code></pre>
  
  <p>population class:</p>
  
  <pre><code>class   population{
      private:
          //  Parameters
          int         population_size;
          node        *pop_nodes;
  
          //  Functions
          init_func_t     init_func;
          fitter_func_t   fitter_func;
      public:
          population(int  pop_size,init_func_t initialization_func){
              //  Insert parameters into vars.
              this-&gt;population_size   = pop_size;
              this-&gt;init_func         = initialization_func;
  
              //  Create new node array.
              this-&gt;pop_nodes         = new node[this-&gt;population_size];
  
              for(int i = 0;i &lt; this-&gt;population_size; i++){
                  dna_s   curr_dna        = this-&gt;init_func();
                  char *s                = static_cast&lt;char*&gt;(curr_dna.dna);
                  cout &lt;&lt; s &lt;&lt; endl;
                  this-&gt;pop_nodes[i].update_dna(curr_dna);
              }
          }
  };
  </code></pre>
  
  <p>You can see that in the constructor im inserting a pointer to function, init_func. this function is generating random words.</p>
  
  <p>init_func:</p>
  
  <pre><code>dna_s   init_func(){
      string alphanum = "0123456789!@#$%^&amp;*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      char        init_s[STRING_SIZE+1]   = {};
      dna_s       dna;
  
      //  Generate String
      for(int i = 0; i &lt; STRING_SIZE; i++){
          init_s[i]   = alphanum[rand() % alphanum.size()];
      }
  
      cout    &lt;&lt; "--&gt;" &lt;&lt; init_s &lt;&lt; endl;
  
      //  Insert into struct.
      dna.size        = STRING_SIZE;
      dna.dna         = static_cast&lt;void*&gt;(&amp;init_s);
  
      //  Return it
      return dna;
  }
  </code></pre>
  
  <p>the main function is not so interesting but it might be connected:</p>
  
  <pre><code>int main(){
      //  Init srand
      srand(time(0));
  
      //  Parameters
      int         population_size     = 10;
      population  pop(population_size, init_func);
  }
  </code></pre>
  
  <p>now for the interesting part, whats the problem?
  in the init_func the cout prints:</p>
  
  <pre><code>--&gt;e%wfF
  </code></pre>
  
  <p>which is all good
  but in the population class the cout prints:</p>
  
  <pre><code>e%Ω²(
  </code></pre>
  
  <p>and the wierd thing is the first 2 characters will always be the same, but the other 3 will always be this string Ω²(.
  example:</p>
  
  <pre><code>--&gt;XaYN7
  XaΩ²(
  --&gt;oBK9Q
  oBΩ²(
  --&gt;lf!KF
  lfΩ²(
  --&gt;RZqMm
  RZΩ²(
  --&gt;oNhMC
  oNΩ²(
  --&gt;EGB6m
  EGΩ²(
  --&gt;osafQ
  osΩ²(
  --&gt;3#NQt
  3#Ω²(
  --&gt;D62l0
  D6Ω²(
  --&gt;tV@mu
  tVΩ²(
  </code></pre>