why automatic variables are assigned with garbage values

This is a discussion on why automatic variables are assigned with garbage values within the C Programming forums, part of the General Programming Boards category; Hi all I have one doubt why automatic variables are assigned with garbage values? I mean static variables are assigned ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    why automatic variables are assigned with garbage values

    Hi all

    I have one doubt
    why automatic variables are assigned with garbage values?

    I mean static variables are assigned with zero(during load time), whereas automatic variables are visible during execution, when the function is called, when the block is entered, so why these variables are not assigned with zero like static varibles

    Regards
    Srinivas

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Why in theory? Because the standard says that they are undefined.

    Why in practice? Because automatic variables are given space on the stack, and the stack could have anything on it, from previous function calls, et cetera.

    Why did the standard make them not defined? Efficiency, I expect. Otherwise the compiler would have to generate code to initialise them to zero every time the function is entered. That's a bit of a waste of time if later in the function you just set them to another value.

    And of course, if you want them initialised, all you need do is set them to 0 explicitly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Automatic variables in external structure definition
    By Boxknife in forum C Programming
    Replies: 2
    Last Post: 06-23-2008, 12:56 PM
  2. Static vs. Automatic Variables
    By Trekkie in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:55 PM
  3. automatic variables
    By FOOTOO in forum C Programming
    Replies: 5
    Last Post: 03-08-2005, 05:30 PM
  4. Automatic vs. Static Duration
    By JMB in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 07:16 PM
  5. Garbage values
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-03-2002, 05:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21