2012-04-22 15 views
9

クラス内に2つの関数があり、ParseBits()関数の呼び出しでエラーが発生しました。 "int num_elements = ParseBits(bits, buffer);" "バッファ"引数のため "public int ParseBits(string bits, int* buffer) 「:ポインタと固定サイズのバッファは、安全でないコンテキストでのみ使用することができます

機能1:

public float AssignFitness(string bits, int target_value) 
     { 

    //holds decimal values of gene sequence 
    int[] buffer = new int[(VM_Placement.AlgorithmParameters.chromo_length/VM_Placement.AlgorithmParameters.gene_length)]; 

    int num_elements = ParseBits(bits, buffer); 

    // ok, we have a buffer filled with valid values of: operator - number - operator - number.. 
    // now we calculate what this represents. 
    float result = 0.0f; 

    for (int i=0; i < num_elements-1; i+=2) 
    { 
     switch (buffer[i]) 
     { 
     case 10: 

      result += buffer[i+1]; 
      break; 

     case 11: 

      result -= buffer[i+1]; 
      break; 

     case 12: 

      result *= buffer[i+1]; 
      break; 

     case 13: 

      result /= buffer[i+1]; 
      break; 

     }//end switch 

    } 

    // Now we calculate the fitness. First check to see if a solution has been found 
    // and assign an arbitarily high fitness score if this is so. 

    if (result == (float)target_value) 

     return 999.0f; 

    else 

     return 1/(float)fabs((double)(target_value - result)); 
    // return result; 
} 

機能2:私は強調表示された行に、この機能のために2つのエラーを取得しています

public int ParseBits(string bits, int* buffer) 
     { 

      //counter for buffer position 
      int cBuff = 0; 

      // step through bits a gene at a time until end and store decimal values 
      // of valid operators and numbers. Don't forget we are looking for operator - 
      // number - operator - number and so on... We ignore the unused genes 1111 
      // and 1110 

      //flag to determine if we are looking for an operator or a number 
      bool bOperator = true; 

      //storage for decimal value of currently tested gene 
      int this_gene = 0; 

      for (int i = 0; i < VM_Placement.AlgorithmParameters.chromo_length; i += VM_Placement.AlgorithmParameters.gene_length) 
      { 
       //convert the current gene to decimal 
       this_gene = BinToDec(bits.Substring(i, VM_Placement.AlgorithmParameters.gene_length)); 

       //find a gene which represents an operator 
       if (bOperator) 
       { 
        if ((this_gene < 10) || (this_gene > 13)) 

         continue; 

        else 
        { 
         bOperator = false; 
         buffer[cBuff++] = this_gene; 
         continue; 
        } 
       } 

       //find a gene which represents a number 
       else 
       { 
        if (this_gene > 9) 

         continue; 

        else 
        { 
         bOperator = true; 
         buffer[cBuff++] = this_gene; 
         continue; 
        } 
       } 

      }//next gene 

      // now we have to run through buffer to see if a possible divide by zero 
      // is included and delete it. (ie a '/' followed by a '0'). We take an easy 
      // way out here and just change the '/' to a '+'. This will not effect the 
      // evolution of the solution 
      for (int i = 0; i < cBuff; i++) 
      { 
       if ((buffer[i] == 13) && (buffer[i + 1] == 0)) 

        buffer[i] = 10; 
      } 

      return cBuff; 
     } 

s:

+0

あなたの質問は何ですか?なぜあなたはエラーを取得しますか?あなたはそれらを避けるために何ができますか?マネージコードや別のものでポインタを使用できないのはなぜですか? (私たちが推測しなければならないよりも、あなたが助けてくれるものを正確に知っていることがわかっていると、正しいかもしれないし、そうでないかもしれません) –

+0

私はそれらを避けるために何ができるでしょう...私はParseBits ()関数が不正な方法で呼び出され、エラーがもう1つ発生しています "引数2: 'int []'から 'int *'に変換できません" – user1277070

+0

なぜ最初にポインタを使用していますか? – harold

答えて

5

おそらく、私はそれを逃しましたが、あなたが実際にint*を使用する必要が何をやっているように見えません。なぜ単にそれをint配列を渡すにParseBits機能シグネチャを変更しない:

public int ParseBits(string bits, int[] buffer)

と完全unsafe{ ... }ブロックを削除します。

19

unsafeブロックに生ポインタを使用して関数を囲む必要があります。

unsafe 
{ 
//your code 
} 
+0

とその関数を呼び出す方法は? "安全でないコンテキストでのみ使用できるポインタと固定サイズのバッファ"エラーが発生していますが、 "VM_Placement.Program.ParseBits(string、int *)の最適オーバーロードされたメソッド一致があります無効な引数 "を返します。 – user1277070

+0

ポインタが必要なので、あなたの関数の呼び出しサイトを 'unsafe'にするといいでしょう。 –

+0

Nopesはうまくいかなかった...私が渡しているような議論のためだと思う...引数2: 'int []'から 'int *'に変換できない " – user1277070

1

私は同じ問題を抱えていましたが、他の回答では解決できませんでした。私は別のエラーを得続けた。

安全でないコードを使用する機能は、単に「安全でない」キーワードで宣言する必要があります。たとえば

static unsafe void myFunction(int* myInt, float* myFloat) 
{ 
    // Function definition 
} 

個人的に、私はラッパークラスを作成する際にこれを行うにしようとしていました。

using System.Runtime.InteropServices; 

namespace myNamespace 
{ 
    public class myClass 
    { 
     [DllImport("myLib.so", EntryPoint = "myFunction")] 
     public static extern unsafe void myFunction(float* var1, float* var2); 
    } 
} 

は、MSDN「安全でないコードチュートリアル」の偉大な多くの情報をTheresの:興味のある方のために
が、それはこのようなものを見てしまった
https://msdn.microsoft.com/en-us/library/aa288474(v=vs.71).aspx

それはおそらく読む価値があるが、Iそれが非常に有用であることが分かった

関連する問題