Optimize Code Problem (VS.Net 2003)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

byte[] Name = new byte[256];
uint len = (uint)Name.Length;
uint err = MyFunction(devID, out Name[0], out len);

When this code is run in release build with optimize code set to true, len
is evaluated to 0. If it is run with optimize code set to false, len is
evaluated as 256 (what i want).

If I add an extra line of code after declaring len, like:

uint foo = len;

then with optimize code on i get 256.

I am happy to build without optimization (it is a small app), but I would
like to know why this happens and if there is a solution.
 
are you setting the value of 'len' inside your function 'MyFunction' ?

HTH

Ollie Riches
 
Yes.

However, MyFunction does not recieve the value 256 from len, it receives a
value of 0. (MyFunction is part of an external dll)

Ollie Riches said:
are you setting the value of 'len' inside your function 'MyFunction' ?

HTH

Ollie Riches

Silly said:
byte[] Name = new byte[256];
uint len = (uint)Name.Length;
uint err = MyFunction(devID, out Name[0], out len);

When this code is run in release build with optimize code set to true, len
is evaluated to 0. If it is run with optimize code set to false, len is
evaluated as 256 (what i want).

If I add an extra line of code after declaring len, like:

uint foo = len;

then with optimize code on i get 256.

I am happy to build without optimization (it is a small app), but I would
like to know why this happens and if there is a solution.
 
I presume becuase the value of 'len' is not used before being passed to the
method MyFunction then the optimizer removes part of the line 'uint len =
(uint)Name.Length;' because you are not using the value you assign to
variable directly in the method.

HTH

Ollie Riches



Silly said:
Yes.

However, MyFunction does not recieve the value 256 from len, it receives a
value of 0. (MyFunction is part of an external dll)

Ollie Riches said:
are you setting the value of 'len' inside your function 'MyFunction' ?

HTH

Ollie Riches

Silly said:
byte[] Name = new byte[256];
uint len = (uint)Name.Length;
uint err = MyFunction(devID, out Name[0], out len);

When this code is run in release build with optimize code set to true,
len
is evaluated to 0. If it is run with optimize code set to false, len is
evaluated as 256 (what i want).

If I add an extra line of code after declaring len, like:

uint foo = len;

then with optimize code on i get 256.

I am happy to build without optimization (it is a small app), but I
would
like to know why this happens and if there is a solution.
 
i think, because you marked the parameter "len" as out parameter, compiler
would think it must be assigned in the method and leaves assigning
(uint)Name.Length. and logically i think in the method, you cant use this
parameter untill you assign new value. change out with ref. it should be ok.
 
Silly said:
However, MyFunction does not recieve the value 256 from len, it receives a
value of 0. (MyFunction is part of an external dll)

Your declaration of MyFunction has said that it doesn't care about the
initial value of len, by making it an "out" parameter. If you want the
value, it should be a "ref" parameter.

Currently the optimiser is deciding that as you don't care about the
value of len before the call, it won't bother evaluating it. That's
entirely reasonable, IMO...

Jon
 
Back
Top