[net.lang.c] Array Initialization

rjs (10/28/82)

As Kernighan and Ritchie's book says: "Regrettably, there is no way to specify
repetition of an initializer, nor to initialize an element in the middle of
an array without supplying all the intervening values as well."  Not only
is this regrettable, it's been the case for far too long.  These facilities
really should be added to the language, especially since we are told not
to rely on default initialization to zero.

	Robert Snyder
	floyd!rjs

davy@pur-ee.UUCP (07/03/83)

#N:ecn-ec:15500016:000:390
ecn-ec!davy    Jul  2 17:07:00 1983


	On our system, arrays of integers are initialized to zero 
	automatically.  I.e., "int n[100]" produces an array of 100
	integers all equal to 0.  

	Is it ok to assume all UNIX C compilers do this, or do some of
	them not initialize the stuff?  Basically, I need to know if I
	can skip initializing them.

--Dave Curry
decvax!pur-ee!davy

P.S. - We're running 4.1 BSD on a VAX-11/780.

ptw@vaxine.UUCP (P. Tucker Withington) (07/07/83)

K&R p. 198 says statics and externals are guaranteed to be "uninitialized" to
zero; automatic and register are guaranteed to be "uninitialized" to garbage.

Since this is supposed to be the "standard"...


				       --Tucker (ptw@vaxine.UUCP)

mostek@ccvaxa.UUCP (07/08/83)

#N:ccvaxa:8800007:000:528
ccvaxa!mostek    Jul  6 17:04:00 1983

 According to "The C Programming Language" by Kernigan and Ritchie,
 static and externals are ALWAYS initialized to zero, whereas automatic
 and register variables are garbage. So if you're array is external or
 static, you don't need to initialize it (and hope noone ever changes
 the declaration).

 If a C compiler doesn't follow this, there is a bug somewhere,
 in the C compiler or in the manual.

 If you have the execution time and text space to spare (one usually does),
 a safe rule is to always explicitly initialize.

ss@rabbit.UUCP (07/08/83)

	K&R p. 198 says statics and externals are guaranteed to be
	"uninitialized" to zero; automatic and register are guaranteed
	to be "uninitialized" to garbage.

	Since this is supposed to be the "standard"...


					       --Tucker (ptw@vaxine.UUCP)


I do wish people would at least READ the book before quoting from it. K&R
p. 198 says

"Static and external variables which are not initialized are guaranteed to
start off as 0; automatic and register variables which are not initialized
are guaranteed to start off as garbage."

	Yes, that is supposed to be the "standard"...

						-- Sharad Singhal
						   ..!rabbit!ss

lee@ut-ngp.UUCP (07/08/83)

All globals and statics in C are initialized to zero. Globals are
identifiers declared outside of any function without a storage-class
specifier. This is part of the C definition and all C compilers should
guarantee this. Auto identifiers are typically not initialized (you
should definitely not expect them to be) because they are allocated on
the stack and disappear every time you leave a function and are created
every time you enter the function. This is also why most C compilers
don't allow you to initialize autos. If you allow auto initialization,
then your C compiler must generate code that assigns the correct value
to the auto upon every entry to the function. It's usually cheaper to
explicitly do the assignment yourself.

ptw@vaxine.UUCP (P. Tucker Withington) (07/11/83)

		K&R p. 198 says statics and externals are guaranteed to be
		"uninitialized" to zero; automatic and register are guaranteed
		to be "uninitialized" to garbage.

		Since this is supposed to be the "standard"...

						--Tucker (ptw@vaxine.UUCP)


	I do wish people would at least READ the book before quoting from it.
	K&R p. 198 says

	"Static and external variables which are not initialized are
	guaranteed to start off as 0; automatic and register variables which
	are not initialized are guaranteed to start off as garbage."

	Yes, that is supposed to be the "standard"...

					-- Sharad Singhal
					..!rabbit!ss

Excuse me, but my lack of quotation marks should have been sufficient to
indicate that I was paraphrasing.  The page number should have been sufficient
to provide the exact reference, and far be it from me to infringe on someone
else's copyright.  Nonetheless, I do admit to this blasphemous act.  People
such as I should be drawn and quartered for desecrating the "bible".  People
such as you... Let's just say your remarks were "Charmin".

				       --Tucker (ptw@vaxine.UUCP)

Off to net.flame for me!

steffen@ihu1f.UUCP (07/12/83)

I have heard that the Whitesmith C compilers do not initialize static
variables to zero.  This and many other differences make Whitesmith C a
definite varient of Bell C.  I think that use of these compilers will die
out so I have made no attempt to be compatible with Whitesmith C in the
portable code I write.
-- 


				Joe Steffen
				ihu1f!steffen
				IH 2C-331 x5381