CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 31

Threaded View

  1. #1
    Join Date
    Jun 2009
    Posts
    65

    Red face Reinitializing variables & their speed sacrifice

    My C++ teacher states that one should not initialize a variable at declaration time (yes i know who made her a teacher!?). Although I'm past the C# point, & VCL, & GDI in C/C++, I find this amusing & irritating at the same time.

    so for the foll. code:

    Code:
    //It's C Code, so cant initialize 'a' inside loop.
    int main()
    {
    	int a=0,b=0;
    	scanf(...,&b)
    	for(a=0;a<10;a++)
    		...	
    return 1;
    }
    She says:
    1. Why the hell use int main() when void main() works fine. Let's just not get started on her ignorance to good coding practices & her ignorance to OS's error handling. I just skip it thinking she's ignorant.

    2. Why initialize b. When b is inputted. ***?
    3. Why initialize a, when you reinitialize it in loop. *** x2?

    OK, as we all know it's ALWAYS a good practice to ZeroMemory() or atleast initialize a variable to 0 or NULL. Her only case may appear a bit(0.001&#37 effective, if this practice poses a speed loss?

    Only if assigning a=0 inside loop or declaring b=0 at initialization poses speed loss, then only her arguments might be only a bit(0.001%) effective, otherwise I'll declare war.

    Can anyone tell if these practices do really pose a speed issue?

    Thanks,
    Nisheeth
    Last edited by nbaztec; March 9th, 2010 at 01:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured