Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to convert some c++ code into c# and I've come across a for loop that I don't understand:

C
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
Posted

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:

C++
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).
 
Share this answer
 
v2
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
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900