• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: C local auto-initialized?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C local auto-initialized?


  • Subject: Re: C local auto-initialized?
  • From: Jack Repenning <email@hidden>
  • Date: Thu, 20 Sep 2007 14:40:11 -0700

On Sep 20, 2007, at 2:02 PM, Fritz Anderson wrote:

Notice that sumY2 is used without being initialized. In both configurations, it contains garbage after [1].

If sumY2 is the first variable declared in the function, the garbage value propagates into the sum. [3b], STRANGENESS == 0

If sumY2 is not the first variable declared, the garbage value does not propagate. [3a], STRANGENESS == 1

It's hard to see how the generated code would "know" to zero out the initial sum, nor why it would do it inconsistently if it did know.

I think you're already demonstrating the evilness of uninitialized variables. Declare victory and get on with your life!


Uninitialized variables aren't necessarily "garbage," they're only "undefined." One of their funner aspects is exactly what you're seeing here: they can be maddeningly consistent, regardless of the fact that the language doesn't officially define their value ... until you look away, and then that ol' garbage sneaks in!

IME, the actual value of such a variable depends on two things:

(1) What was on the stack (or rather, in the memory space where the stack was about to be allocated) just before the call to this routine? That is, the routine called on the line above the line that calls your routine. Those left-overs are what will be in the uninit. If your test program is very simple -- in particular, if the call to this routine is the first time your call stack has been this deep - then the behavior you see when it's consistent might be the result of some broader policy, such as zeroing out memory freshly added to the process address space (brk on stack growth).

(2) Has the optimizer been playing in your back yard? Since the language declares your value to be undefined, the optimizer is perfectly free to do anything it bloody well likes about the initial value. I have seen the optimizer actually fix an uninitialized- variable bug! That, in your case, the optimizer is breaking things is just as legit. You didn't mention whether your disassembly was for the Debug ("working") or Release ("broken") case, but the Release disassembly might even show your uninitialized variables have been optimized entirely into registers, with no in-memory presence at all.

-==-
Jack Repenning
Chief Technology Officer
CollabNet, Inc.
8000 Marina Boulevard, Suite 600
Brisbane, California 94005
office: +1 650.228.2562
mobile: +1 408.835.8090
raindance: +1 877.326.2337, x844.7461
aim: jackrepenning
skype: jrepenning




_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >C local auto-initialized? (From: Fritz Anderson <email@hidden>)

  • Prev by Date: Re: C local auto-initialized?
  • Next by Date: Re: C local auto-initialized?
  • Previous by thread: Re: C local auto-initialized? (persists)
  • Next by thread: Re: C local auto-initialized?
  • Index(es):
    • Date
    • Thread