473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Optimize Code Problem (VS.Net 2003)


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.
Nov 17 '05 #1
6 1516
are you setting the value of 'len' inside your function 'MyFunction' ?

HTH

Ollie Riches

"Silly" <Si***@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...

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.

Nov 17 '05 #2
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" wrote:
are you setting the value of 'len' inside your function 'MyFunction' ?

HTH

Ollie Riches

"Silly" <Si***@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...

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.


Nov 17 '05 #3
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" <Si***@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
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" wrote:
are you setting the value of 'len' inside your function 'MyFunction' ?

HTH

Ollie Riches

"Silly" <Si***@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
>
> 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.


Nov 17 '05 #4
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.
Nov 17 '05 #5
Thanks, that worked a treat!!

"The Crow" wrote:
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.

Nov 17 '05 #6
Silly wrote:
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

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Cengiz Ulku | last post by:
Hi, I have a ComboBox which is filled with 66 items on form_load event: Private Sub Form_Load() Combo1.AddItem "Tekvin" 'item number: 1 Combo1.AddItem "Cikis" ...
3
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any...
0
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an...
8
by: Fuzzy | last post by:
I want to experiment with the JIT compiler's optimization of code, especially regarding inline compilation of small methods. What I have done is set the Build options for 'Optimize Code' to TRUE...
3
by: Gaffar | last post by:
Hello, I am Handling a project in ( ASP.NET with C#.NET) in which a module is slow and inefficient. How to optimize the code. Please give me some suggestions regarding this. If you know any...
2
by: Aaron | last post by:
Hello all, I have 5 warnings only but I want to know how to "optimize" my code so that it is "clean." The warning is this with all 5 the same Warning 1 Variable 'strxxx' is used before it has...
21
by: Jim | last post by:
I am trying to write an HTTP/HTTPS proxy server in VB.Net 2005. But, I don't really even know how the internal workings of a proxy should act. Does anyone have anything on the protocols used in...
2
by: steevehetu18 | last post by:
Hi, I'm doing a algorithm to calcule Earliest Start et Latest Start for a Graph with Nodes and Arcs. (like a PERT diagram) . Unfortunatly, i receive a wierd exception message for a specific...
7
by: Zytan | last post by:
I have the simplified build ("show advanced build configurations" turned off), so that pressing F5 runs in DEBUG mode with the debugger. When an assertion fires, I find that I cannot 'watch' some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.