2012-04-22 19 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:

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; 
     } 

我收到2个错误此功能上突出显示的行s:

Error 1: The best overloaded method match for 'VM_Placement.Program.ParseBits(string, int*)' has some invalid arguments 

Error 2: Pointers and fixed size buffers may only be used in an unsafe context 
+0

那么你的问题是什么?为什么你会得到错误?你可以做些什么来避免它们?为什么你不能在托管代码或其他东西中使用指针? (确切地知道你对什么有帮助,比起我们必须猜测,帮助你更容易) – 2012-04-22 16:35:08

+0

我该怎么做才能避免它们......我想我是在ParseBits中传递参数()函数以不正确的方式出现,因此我又收到一个错误“参数2:无法从'int []'转换为'int *'” – user1277070 2012-04-22 16:58:55

+0

为什么在第一个地方使用指针? – harold 2012-04-22 17:34:55

回答

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 2012-04-22 16:35:03

+0

我会说你也会让你的函数的调用地址为'unsafe',因为它也需要指针。 – 2012-04-22 16:41:04

+0

Nopes没有解决......我想这是因为我正在通过的那种争论......在调用ParseBits()“参数2:无法从'int []'转换为'int *'”时,我得到了一个更多的参数传递错误[ – user1277070 2012-04-22 16:57:58

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); 
    } 
} 

Theres很多伟大的信息在MSDN“不安全代码教程”:
https://msdn.microsoft.com/en-us/library/aa288474(v=vs.71).aspx

这可能是值得一读,我发现它非常有用。

相关问题