Declare variables at top of function or in separate scopes?

后端 未结 9 1120
温柔的废话
温柔的废话 2020-12-01 01:47

Which is preferred, method 1 or method 2?

Method 1:

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (         


        
9条回答
  •  一整个雨季
    2020-12-01 01:54

    Define the variables in the narrowest scope where they are relevant. There's no reason to use Method 2 above in my opinion.

    Stack space is only likely to be used when the variables are in scope. As @paxdiablo points out, your locals may wind up in registers rather than on the stack, if the compiler can find the space for them.

提交回复
热议问题