473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

array elements

Hi,

is there any guarantee that elements of an un initialized array
by default contains the element 0 ?
Nov 24 '07 #1
16 2169
so**********@gmail.com wrote:
Hi,

is there any guarantee that elements of an un initialized array
by default contains the element 0 ?

If there was, it wouldn't be uninitialised!

--
Ian Collins.
Nov 24 '07 #2
<so**********@gmail.comwrote in message
is there any guarantee that elements of an un initialized array
by default contains the element 0 ?
The implementation sometimes guarantees it. For instance Microsoft's Visual
C used to initialise all memory to 0xC0 in debug, I think to zero in
release - the idea that if an integer with 0xC0C0.... is used as an index it
will generate a large negative vlaue and almost certainly a crash, whilst 0
will probably keep the show on the road.

However it is incorrect to rely on such details.

There are two exceptions.

int array[10000] = {0};

will initialise all elements of array to zero. This is a concession to save
typing.

in global space

/* global */
int array[1000];

void foo(void)
{

}

array is actually initialised to zero, because it is a global. However it is
poor form to rely on this.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Nov 24 '07 #3
In article
<39**********************************@e23g2000prf. googlegroups.com>,
so**********@gmail.com <so**********@gmail.comwrote on Saturday 24
Nov 2007 9:52 am:
Hi,

is there any guarantee that elements of an un initialized array
by default contains the element 0 ?
Yes, if it is a static array, the compiler initialises the elements to
zero if no explicit initialisation is provided.

For the case of auto arrays, if you initialise at least one member, the
trailing members are initialised to zero. Otherwise the array has
indeterminate valued elements.

/* file.c */

int arr[10]; /* This is initialised to zero by the compiler/loader */

/* file1.c */

fx()
{
int arr[10] = { 0 }; /* Remaining members are set to zero */
/* ... */
}

Nov 24 '07 #4
Malcolm McLean said:
<so**********@gmail.comwrote in message
>is there any guarantee that elements of an un initialized array
by default contains the element 0 ?
The implementation sometimes guarantees it.
<snip>
However it is incorrect to rely on such details.
With you so far...

There are two exceptions.

int array[10000] = {0};

will initialise all elements of array to zero. This is a concession to
save typing.
<grinWell, I suppose you could put it like that. In fact, you could argue
that the standard library is a concession to save typing.
in global space

/* global */
int array[1000];

void foo(void)
{

}

array is actually initialised to zero, because it is a global. However it
is poor form to rely on this.
Here I must disagree. I would argue that it's poor form to use the file
scope object in the first place, but if you're going to do so, I don't see
why it's poor form to rely on the guarantee that ISO gives in its default
static initialiser rule.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 24 '07 #5
On Fri, 23 Nov 2007 20:22:31 -0800 (PST), so**********@gmail.com
wrote:
>Hi,

is there any guarantee that elements of an un initialized array
by default contains the element 0 ?
For a static array or one defined at file scope, if the elements are
integer they contain the VALUE 0. If they are floating point, they
contain 0.0. If they are pointers, they contain NULL. If the
elements are structures, apply the above recursively to the members.
If the elements are unions, ONLY the first member is initialized. They
will all compare equal to zero but need not be represented by all bits
0.

For an automatic array, the guarantee is just the opposite. The
values are indeterminate and any attempt to evaluate an indeterminate
value invokes undefined behavior.
Remove del for email
Nov 25 '07 #6
On Nov 24, 7:51 pm, Barry Schwarz <schwa...@doezl.netwrote:
On Fri, 23 Nov 2007 20:22:31 -0800 (PST), sophia.ag...@gmail.com
wrote:
They will all compare equal to zero but need not be represented by all
bits 0.

can you be bit more precise on the above statement.
Do you mean signed zero ?, so that a sign bit 1 is used to represent -
ve values isn't it?
Nov 25 '07 #7
In article
<54**********************************@i12g2000prf. googlegroups.com>,
so**********@gmail.com <so**********@gmail.comwrote on Sunday 25 Nov
2007 12:37 pm:
On Nov 24, 7:51 pm, Barry Schwarz <schwa...@doezl.netwrote:
>On Fri, 23 Nov 2007 20:22:31 -0800 (PST), sophia.ag...@gmail.com
wrote:

They will all compare equal to zero but need not be represented by all
bits 0.

can you be bit more precise on the above statement.
I suggest that you read the entirety of the following section of the
comp.lang.c FAQ:

<http://c-faq.com/null/index.html>

Also this subject has been discussed innumerable times in this group. A
quick search on Google Groups will turn up a lot links to excellent
previous discussions.
Do you mean signed zero ?, so that a sign bit 1 is used to represent -
ve values isn't it?
No, only for certain architectures.

Nov 25 '07 #8
Barry Schwarz <sc******@doezl.netwrites:
On Fri, 23 Nov 2007 20:22:31 -0800 (PST), so**********@gmail.com
wrote:
>>
is there any guarantee that elements of an un initialized array
by default contains the element 0 ?
<snip>
For an automatic array, the guarantee is just the opposite. The
values are indeterminate and any attempt to evaluate an indeterminate
value invokes undefined behavior.
Nit: except for unsigned char where the values can only be
"unspecified". Unspecified values are always valid for the given
type, so UB does not inevitable follow. Any program that makes use
of this nit is fit only for language lawyering.

--
Ben.
Nov 25 '07 #9

"Ian Collins" <ia******@hotmail.comwrote in message
news:5q*************@mid.individual.net...
so**********@gmail.com wrote:
>Hi,

is there any guarantee that elements of an un initialized array
by default contains the element 0 ?

If there was, it wouldn't be uninitialised!

--
Ian Collins.
I'm certain you mean were instead of was. Third person subjunctive present
tense.

Tja.

--
wade ward

wa**@zaxfuuq.net
435 -838-7760

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 26 '07 #10
In article <11*************@sp12lax.superfeed.net>,
Wade Ward <wa**@zaxfuuq.netwrote:
>I'm certain you mean were instead of was. Third person subjunctive present
tense.
The subjunctive is in decline, at least in British English. Not many
people will miss it.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 26 '07 #11
On Sat, 24 Nov 2007 23:07:22 -0800 (PST), so**********@gmail.com
wrote:
>On Nov 24, 7:51 pm, Barry Schwarz <schwa...@doezl.netwrote:
>On Fri, 23 Nov 2007 20:22:31 -0800 (PST), sophia.ag...@gmail.com
wrote:

They will all compare equal to zero but need not be represented by all
bits 0.

can you be bit more precise on the above statement.
Do you mean signed zero ?, so that a sign bit 1 is used to represent -
ve values isn't it?
Please configure your newsgroup software to distinguish between what
your are quoting and what you are adding. And then quote enough of
the message you are replying to so the context is clear. Not everyone
is going to remember what "they" stands for.

For a scalar object x of type T which has been initialized with or
assigned a value, you can print the individual bytes that make up the
object with a function like

void hexprint(void *v, size_t n) {
size_t i;
unsigned char *p = v;
for (i = 0; i < n; i++)
printf("%x ", p[i]);
putc('\n');
}

You would call this function with a statement like
hexprint(&x, sizeof x);

If we assume the system uses 8-bit bytes, then for any of the integer
types with a value of 0 the output will be a sequence of 0x00. Using
the terminology in question, an integer value of 0 is represented by
all bits zero. However, this need not be true for floating point and
pointer types. (On my system, one of the many valid representations
of 0.0f is 0x40 0x00 0x00 0x00.) In those cases where it is not true,
the compiler is responsible for ensuring that the code generated for
the expression
x == 0
still evaluates to 1. How it does this is implementation dependent.

The point I was trying to make is that the default initialization for
a static object is performed more in the manner of an assignment
rather than the technique used by calloc.
Remove del for email
Nov 26 '07 #12
Richard Tobin wrote:
In article <11*************@sp12lax.superfeed.net>,
Wade Ward <wa**@zaxfuuq.netwrote:
>I'm certain you mean were instead of was. Third person subjunctive present
tense.

The subjunctive is in decline, at least in British English. Not many
people will miss it.
Ah, would that they would.
Nov 26 '07 #13
On Sun, 25 Nov 2007 17:13:53 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
>Barry Schwarz <sc******@doezl.netwrites:
>On Fri, 23 Nov 2007 20:22:31 -0800 (PST), so**********@gmail.com
wrote:
>>>
is there any guarantee that elements of an un initialized array
by default contains the element 0 ?
<snip>
>For an automatic array, the guarantee is just the opposite. The
values are indeterminate and any attempt to evaluate an indeterminate
value invokes undefined behavior.

Nit: except for unsigned char where the values can only be
"unspecified". Unspecified values are always valid for the given
type, so UB does not inevitable follow. Any program that makes use
of this nit is fit only for language lawyering.
This makes sense but do you have a reference? 6.2.4-5, 6.2.4-6,
6.7.8-10, and J.2 (bullet 10) don't have any exceptions for unsigned
characters when asserting that accessing an indeterminate value
results in undefined behavior.
Remove del for email
Nov 27 '07 #14
On Mon, 26 Nov 2007 19:45:00 -0800, Barry Schwarz wrote:
On Sun, 25 Nov 2007 17:13:53 +0000, Ben Bacarisse <be********@bsb.me.uk>
wrote:
>>Barry Schwarz <sc******@doezl.netwrites:
>>For an automatic array, the guarantee is just the opposite. The
values are indeterminate and any attempt to evaluate an indeterminate
value invokes undefined behavior.

Nit: except for unsigned char where the values can only be
"unspecified". Unspecified values are always valid for the given type,
so UB does not inevitable follow. Any program that makes use of this
nit is fit only for language lawyering.

This makes sense but do you have a reference? 6.2.4-5, 6.2.4-6,
6.7.8-10, and J.2 (bullet 10) don't have any exceptions for unsigned
characters when asserting that accessing an indeterminate value results
in undefined behavior.
J.2 is not normative, and no longer correct. The only possibility for
undefined behaviour for use of an indeterminate value is given by
6.2.6.1p5 (trap representations), and unsigned char isn't allowed to have
trap representations.

This program is also strictly conforming in C99:

#include <limits.h>
int main(void) {
if (INT_MIN == -0x8000
&& INT_MAX == 0x7FFF
&& sizeof(int) * CHAR_BIT == 16)
{
int i;
return !(i | 1);
}
else
return 0;
}

The indeterminate value of i is only read after it is determined that
trap representations are not possible, so 6.2.6.1p5 doesn't apply, so the
behaviour is defined.
Nov 27 '07 #15
Barry Schwarz <sc******@doezl.netwrites:
On Sun, 25 Nov 2007 17:13:53 +0000, Ben Bacarisse
<be********@bsb.me.ukwrote:
<snip>
>>Nit: except for unsigned char where the values can only be
"unspecified". Unspecified values are always valid for the given
type, so UB does not inevitable follow. Any program that makes use
of this nit is fit only for language lawyering.

This makes sense but do you have a reference?
To add to Harald van Dijk's answer (I am just tidying up marked
messages!), my reasoning follows from 3.17.2 which defines an
indeterminate value as "either an unspecified value or a trap
representation". 6.2.6.2 para. 1 tells us that unsigned char has no
padding bits, so trap representations are not possible. Thus
uninitialised unsigned char variables can only hold, at worst, an
"unspecified value".

Section 3.17.3 defines this as a "valid value of the relevant type
where this International Standard imposes no requirements on which
value is chosen in any instance". Accessing a valid value can't be
the sole cause of undefined behaviour.

--
Ben.
Dec 5 '07 #16
On Nov 24, 1:32 pm, santosh <santosh....@gmail.comwrote:
In article
<39483261-f07e-445d-8a57-093c9909b...@e23g2000prf.googlegroups.com>,
sophia.ag...@gmail.com <sophia.ag...@gmail.comwrote on Saturday 24
Nov 2007 9:52 am:
Hi,
is there any guarantee that elements of an un initialized array
by default contains the element 0 ?

Yes, if it is a static array, the compiler initialises the elements to
zero if no explicit initialisation is provided.

For the case of auto arrays, if you initialise at least one member, the
trailing members are initialised to zero. Otherwise the array has
indeterminate valued elements.

I tried with the bellow mentioned code to see the array initialization
working .

#include<stdio.h>
int main(void)
{
int ar[10]= {-1};
int i = 0 ;
for (i = 0 ;i <10 ;i++)
{
printf("\n value of ar[%d] = %d \n",i,ar[i]);
}
return 0;
}

Output of the program

value of ar[0] = -1

value of ar[1] = 0

value of ar[2] = 0

value of ar[3] = 0

value of ar[4] = 0

But I would like to know what the intention behind this kind of
behavior is? Meaning ar[0] = -1 why the other members are
initialized with 0 why not -1 ? What is the idea behind this
behavior?

>
/* file.c */

int arr[10]; /* This is initialised to zero by the compiler/loader */

/* file1.c */

fx()
{
int arr[10] = { 0 }; /* Remaining members are set to zero */
/* ... */
Dec 10 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
5
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
2
by: Salman Khilji | last post by:
After reading all the FAQs, I cannot solve the following problem: I have a pointer of type double. I am supposed to 1) Allocate memory for it assuming that the pointer will be pointing to a...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: Richard Harter | last post by:
On Fri, 27 Jun 2008 09:28:56 -0700 (PDT), pereges <Broli00@gmail.comwrote: There are some obvious questions that should be asked, e.g., is the contents of your array already sorted as your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.