Please help me with some pointer work -- thanks!

A

almurph

Folks,

Hope you can help me here. I know this is the C# forum but I have
translated some C into C# and I'm wondering did I get it right? So, I
would appreciate any corrections/comments/suggestions that you may be
able to make (or even a way of doing this equivalently without
pointers). I hate working with pointers ;-(

Anyway below is the C original code and my attempt on it below that.
It should be noted that weight is an array of type double.

Thanks again for any corrections/comments.
Al


**** C Language Code As Follows *****

int A, B;
int *Wb, *Wa;
double Sum=0.0;

for ( Wb = Weight + B, Wa = Weight + A ; Wb <= Wa ; )
{
Sum += *Wb++;
}


return Sum;


**** END C CODE ****


Here is my C#.NET attempt:


**** BEGIN MY C# ATTEMPT ****

int B, A;
double sum = 0.0;

unsafe
{

fixed (double* start = &Weight)
fixed (double* end = &Weight[A])
{
for (double* Wb = start; Wb <= end; Wb++)
{
Sum += *Wb++;
}
}
}

return Sum;


**** END C# ATTEMPT *****
 
A

almurph

   Hope you can help me here. I know this is the C# forum but I have
translated some C into C# and I'm wondering did I get it right? So, I
would appreciate any corrections/comments/suggestions that you may be
able to make (or even a way of doing this equivalently without
pointers). I hate working with pointers ;-(
   Anyway below is the C original code and my attempt on it below that.
It should be noted that weight is an array of type double.

Without more specifics, it's hard to comment.  But, probably the reason 
the C code is using pointers is as a performance optimization (and most  
likely a pointless one).  In C#, you should just do the normal array-based  
approach:

     double sum = 0;

     for (int iweight = iweightB; iweight <= iweightA; iweight++)
     {
         sum += weight[iweight];
     }

Forget all the "fixed" stuff, until such a time as you've proven that not 
using it is causing a performance problem that has to be corrected.

Pete

Hi Pete - Thanks for the reply but I'm not so sure this is correct.
weight is an array double and the LOC:
int iweight = iweightB
where I'm assuming that iWeightB = Weight;

would tend to lose that granularity or am i missing something?
 
A

almurph

Without more specifics, it's hard to comment.  But, probably the reason  
the C code is using pointers is as a performance optimization (and most 
likely a pointless one).  In C#, you should just do the normal array-based  
approach:
     double sum = 0;
     for (int iweight = iweightB; iweight <= iweightA; iweight++)
     {
         sum += weight[iweight];
     }
Forget all the "fixed" stuff, until such a time as you've proven that not  
using it is causing a performance problem that has to be corrected.

Hi Pete - Thanks for the reply but I'm not so sure this is correct.
weight is an array double and the LOC:
int iweight = iweightB
where I'm assuming that iWeightB  = Weight;

would tend to lose that granularity or am i missing something?- Hide quoted text -

- Show quoted text -


Hi Pete, how about something like:

double Wt_Value = 0.0;
for (int iweight = iweightB; iweight <= iweightA; iweight++)
{
Wt_Value = Wt;
Sum = Sum + Wt_Value;
Wt = Wt + 1;
}

return Sum;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top