Re: In which scope should variables be declared?



In article <1127506658.746576.56720@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
elzacho <elzacho@xxxxxxxxx> wrote:
>I would like to (and commonly do) define my variables in the most
>specific scope I can. For example...
>
>int foo(int a, int b, int c)
>{
> /* don't declare temp here if we can help it */
> ...
>
> for (i = 0; i < max; i++)
> {
> /* declare here */
> int temp;
> ...
> /* use temp */
If you expect temp to have its value survive from one interation
of this loop to the next, DON'T declare it inside the for loop.
Logically, the variable is destroyed and re-created each iteration
of the loop.

> }
>}
>
>But my concern is, does the declaration take up cpu time?

Maybe.

>One can
>imagine that temp could be a huge array or max could be a large number.

Does adding a million take more time than adding one?

> In this case, if the declaration maps to any cpu time when compiled,
>this could lead to a significant performance drop.

Standard C does not guarantee that a program has performance to drop.
If you can't measure it, don't worry about it. If you haven't got
time to measure it, you don't have time to worry about whether it
takes CPU time.

>So, is there a difference in the machine code between declaring all of
>your variables at outer most scope or inner most scope?

Possibly. If you declare the variables at the innermost scope,
those in different blocks may overlap each other, making the offsets
different. That's a difference in the machine code. I didn't say
it was a difference in the execution time.

>My impressions
>on C are that it makes does not impose how this is handled, rather that
>this is a compiler issue, but I am not sure how much the standards
>dictate this. Thought I would pass it by some experts.

C rarely dictates HOW anything is handled. It's just supposed to work
as indicated.

Gordon L. Burditt
.



Relevant Pages

  • Re: Local variables - quick question
    ... You can't use a variable which isn't in scope within ... possiblity to declare class argument int i - only signal for me is compiler ... > being alive by the garbage collector. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Count total no. of characters,words & sentences in a text file
    ... int main ... Use meaningful names and declare them in the scope where they are used. ... Whitespace is free, use it. ...
    (comp.lang.c)
  • Re: Local variables - quick question
    ... You can't use a variable which isn't in scope within ... You could declare an instance variable i, ... and after that I would like to use int i - new declaration. ... language is defined to prevent local variables from hiding each other. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Looking for a way to convert dec to bin
    ... I prefer to see at a glance what is going to be used. ... this in dot net and declare all variables with the minimum possible scope. ... int i = 5; ...
    (microsoft.public.vb.general.discussion)
  • Re: Really tough ADO Stored Procedure Question. Please Help!!!
    ... @lScenarioID_CopyFrom int, ... DECLARE @ErrMSG varchar--This is the max msg size ... ROLLBACK TRANSACTION ... SELECT @lRowCountHolder = MIN ...
    (microsoft.public.sqlserver.odbc)