指针和固定大小的缓冲区只能在不安全的上下文中使用 [英] Pointers and fixed size buffers may only be used in an unsafe context

查看:514
本文介绍了指针和固定大小的缓冲区只能在不安全的上下文中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中有2个函数,但由于传递"public int ParseBits(string bits, int* buffer)"而导致的缓冲"争论,导致对ParseBits()函数的调用出现错误,即"int num_elements = ParseBits(bits, buffer);":

I have 2 functions within a class and getting error on the call for ParseBits() function i.e. "int num_elements = ParseBits(bits, buffer);" because of the "buffer" arguement I am passing "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个错误:

I am getting 2 errors for this functions on the highlighted lines:

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

推荐答案

也许我错过了,但是您似乎并没有做任何实际上需要使用int*的操作.为什么不简单地将其传递给一个int数组,然后将ParseBits函数签名更改为:

Perhaps I've missed it, but you don't appear to be doing anything that actually requires the use of a int*. Why not simply pass it an int array and change the ParseBits function signature to:

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

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

并完全删除unsafe{ ... }块.

这篇关于指针和固定大小的缓冲区只能在不安全的上下文中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆