这个for循环的C#等效项是什么 [英] What is the c# equivalent of this for loop

查看:74
本文介绍了这个for循环的C#等效项是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些c ++代码转换为c#,但遇到了一个我不理解的for循环:

  const   unsigned   int  * source =数据;
 for ( int  y =  0 ; y <高度; y ++,源+ =宽度)
{
    未签名  int  * scanline =( unsigned   int  *)FreeImage_GetScanLine(bitmap,Height-y- 1 );
    memcpy(scanline,source, sizeof (source [ 0 ])*宽度);
} 


我的具体问题是source+=Width的作用是什么?每次循环增加,它会增加宽度的来源吗?

数据是一个数组,我的猜测是这段代码正在增加数组的大小,并使用GetScanLine函数的数据填充(通过memcpy)

解决方案

<将blockquote> source声明为const unsigned int的指针,然后在其上使用指针算术.要了解其工作原理,请看下面的代码片段:

 未签名  int  data [ 1024 ];
 for ( int  i =  0 ; i <  1024 ; i ++)data [i] = i;
 const   unsigned   int  *源=数据;
printf(" ,*源);
源++;
printf("  ;, * source);
来源+ =  10 ;
printf(" ,*源); 



产生的输出是:

*source is 0
*source is 1
*source is 11



这是因为:


  1. 在第一个printf语句之前,将data的起始地址分配给source,然后它指向数组的第一个元素data[0]
  2. ,之后,该语句source++将指针加1,这意味着:增加指针,使其指向它指向的类型的下一项(即,指针增加4,因为这是无符号int的大小).然后第二个printf最终显示data[1]
  3. 的值,语句source += 10将指针增加10个无符号int占用的内存量,因此它指向data[11]



请注意,只要您不使用不安全代码.
但是,不建议使用不安全代码,将 C ++ 代码转换为 C#代码时,最好的选择是从头开始重写那些使用指针来避免它.
如果您的目标是在 .NET 下重用您的 C ++ 代码,请考虑使用 C ++/CLR ,这是传统的最佳选择代码(它允许在程序集中混合托管非托管代码,因此您可以使用它将旧的本机项目与新的 .NET 用任何支持它的语言编写的项目).


cjb110写道:

我的具体问题+ =宽度是做什么的?每次循环增加,它会增加宽度吗?



是的

cjb110写道:

数据是一个数组,我的猜测是这段代码正在增加数组的大小,并通过数据填充(通过memcpy)从GetScanLine函数




嗨cjb110
看来您是从上方读取或扫描图片(我可以从第二个参数中看到它)
每次扫描时,您都在扫描该图片的全水平线,该数据的宽度等于Width,并且您在可变源中收集(+ =)这些数据
source + = Width表示source = source + width;
khalid sabtan
沙特阿拉伯


I''m trying to convert some c++ code into c# and I''ve come across a for loop that I don''t understand:

const unsigned int* source = Data;
for( int y=0; y < Height; y++, source += Width )
{
    unsigned int* scanline = (unsigned int*)FreeImage_GetScanLine(bitmap, Height - y - 1 );
    memcpy(scanline, source, sizeof(source[0]) * Width);
}


my specific question is what does the source+=Width do? does it increase source by width each time the loop increments?

Data is an array, and my guess is that this code is increasing the array size and filling it (via memcpy) with the data from the GetScanLine function

解决方案

source is declared as pointer to const unsigned int, then pointer arithmetic is used on it. To understand how it works let''s have a look to the code snippet below:

unsigned int data[1024];
for(int i = 0; i < 1024; i++) data[i] = i;
const unsigned int *source = data;
printf("*source is %d\n", *source);
source++;
printf("*source is %d\n";, *source);
source += 10;
printf("*source is %d\n", *source);



the output produced is:

*source is 0
*source is 1
*source is 11



this because:


  1. before the first printf statement the starting address of data is assigned to source, then it points to the first element of the array, data[0]
  2. after that, the statement source++ increments the pointer by one, and this means: increase the pointer so that it points to the next item of the type it points to (i.e. the pointer is increased by 4 because this is the size of an unsigned int). Then the second printf shows the value of data[1]
  3. finally, the statement source += 10 increase the pointer by the amount of memory occupied by 10 unsigned int, thus it points to data[11]



Note that there is not an equivalent to pointers and pointer arithmetic in C# as long as you don''t use unsafe code.
However the use of unsafe code is not recommended, and the best option while translating C++ code to C# code is to rewrite from scratch those parts that use pointers to avoid it.
If your target is to reuse your C++ code under .NET, think about using C++/CLR which is the best choice for legacy code (it allow to mix managed and unmanaged code inside an assembly, so you can use it to interface old native projects to new .NET projects written in any language that supports it).


cjb110 wrote:

my specific question is what does the source+=Width do? does it increase source by width each time the loop increments?



Yes

cjb110 wrote:

Data is an array, and my guess is that this code is increasing the array size and filling it (via memcpy) with the data from the GetScanLine function



Yes


Hi cjb110
it seems you are readding or scanning a picture from the upward(i could see it from your 2nd parameter)
each time you scan you are scanning full horizontal line of that picture which has data equal Width and you are collecting ( += )those data in variable source
source +=Width means source=source+width;
khalid sabtan
saudia arabia


这篇关于这个for循环的C#等效项是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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