473,396 Members | 1,968 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.

Initialization without loop

Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)
Nov 13 '05 #1
17 2646
"caijunfu" <ca******@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...
Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)


I don't believe there's any way to do it that's guaranteed to be O(1). You
could do something like this

int array[100] = { 0 };

That would initialize array with 100 zeros (because the initializer list has
fewer elements than the array, the last 99 elements are given the same value
they would get if they were static storage--i.e., 0). With your C
implementation, the initialization process might end up being of the order
O(1), for all I know. But again, there's no guarantee in the C standard.

Regards,

Russell Hanneken
rh*******@pobox.com
Nov 13 '05 #2
caijunfu wrote:
Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)

In many implementations, memset() would choose the fastest way of
initialization. Then, it should be better than O(n), up to some size,
perhaps as big as a cache line.
calloc() would be expected to implement the best general algorithm for the
task you have described, if the developers of your library have taken care
to do so. You could test various alternate libraries for your platform. I
don't know whether you mean the speed of the entire process, or just the
speed of the initialization part.
--
Tim Prince
Nov 13 '05 #3
"Tim Prince" <ti***********@xxxxsbcglobal.net> wrote in message
news:XB********************@newssvr14.news.prodigy .com...
caijunfu wrote:
Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?

(usually, we do initialisation by using for loop. that is of O(n).)

In many implementations, memset() would choose the fastest way of
initialization. Then, it should be better than O(n), up to some size,
perhaps as big as a cache line.
calloc() would be expected to implement the best general algorithm for the
task you have described, if the developers of your library have taken care
to do so.


memset could give every byte in the array a value of (unsigned char) 0.
That's fine if you have an array of unsigned chars, but if you have an array
of, say, ints, setting every byte to 0 isn't guaranteed to give every
element the value 0.

calloc sets every bit to 0, but again, that may not give every element a
value of 0. The value 0 may or may not be represented by all-bits-zero.
:-\

Regards,

Russell Hanneken
rh*******@pobox.com
Nov 13 '05 #4
"Russell Hanneken" <rh*******@pobox.com> writes:
memset could give every byte in the array a value of (unsigned char) 0.
That's fine if you have an array of unsigned chars, but if you have an array
of, say, ints, setting every byte to 0 isn't guaranteed to give every
element the value 0.


Actually, all-bits-zero is guaranteed to be a representation of the value
zero for all integer types. See Defect Report #263.

Martin
Nov 13 '05 #5
"Martin Dickopp" <ex*************@zero-based.org> wrote in message
news:bk*************@news.t-online.com...
"Russell Hanneken" <rh*******@pobox.com> writes:
memset could give every byte in the array a value of (unsigned char) 0.
That's fine if you have an array of unsigned chars, but if you have an
array of, say, ints, setting every byte to 0 isn't guaranteed to give
every element the value 0.


Actually, all-bits-zero is guaranteed to be a representation of the value
zero for all integer types. See Defect Report #263.


Can defect reports guarantee anything? I thought they just described
perceived problems with the standard.

Anyway, even if all-bits-zero is guaranteed to be a representation of the
value zero for all integer types, does that mean we can use memset to zero
an array of ints? As I understand it, if we tell memset to write zeros,
we're telling it to write some valid unsigned char representation of 0 to
each byte. According to what you're saying, all-bits-zero must be *a* valid
representation of unsigned char 0, but it doesn't have to be the only one.
How do we know memset will choose the all-bits-zero representation?

Regards,

Russell Hanneken
rh*******@pobox.com
Nov 13 '05 #6
"Russell Hanneken" <rh*******@pobox.com> writes:
"Martin Dickopp" <ex*************@zero-based.org> wrote in message
news:bk*************@news.t-online.com...
"Russell Hanneken" <rh*******@pobox.com> writes:
memset could give every byte in the array a value of (unsigned char) 0.
That's fine if you have an array of unsigned chars, but if you have an
array of, say, ints, setting every byte to 0 isn't guaranteed to give
every element the value 0.


Actually, all-bits-zero is guaranteed to be a representation of the value
zero for all integer types. See Defect Report #263.


Can defect reports guarantee anything? I thought they just described
perceived problems with the standard.


The committee's response to a defect report can clarify and/or
correct problems with the standard.
--
"The fact that there is a holy war doesn't mean that one of the sides
doesn't suck - usually both do..."
--Alexander Viro
Nov 13 '05 #7
Ben Pfaff wrote:

"Russell Hanneken" <rh*******@pobox.com> writes:
"Martin Dickopp" <ex*************@zero-based.org> wrote in message
news:bk*************@news.t-online.com...
Actually, all-bits-zero is guaranteed to be a representation of the value
zero for all integer types. See Defect Report #263.


Can defect reports guarantee anything? I thought they just described
perceived problems with the standard.


The committee's response to a defect report can clarify and/or
correct problems with the standard.

Right, but that's not Martin said. He said that the defect report
guarantees that all-bits is ok now. In fact, while the recommendation is
to modify the Standard, the response from the committee could be,
"tough, live with it."


Brian Rodenborn
Nov 13 '05 #8
Default User <fi********@company.com> spoke thus:
Right, but that's not Martin said. He said that the defect report
guarantees that all-bits is ok now. In fact, while the recommendation is
to modify the Standard, the response from the committee could be,
"tough, live with it."


So what's the official status of all these defect reports? Is a response
still to be determined?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #9
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Default User <fi********@company.com> spoke thus:
Right, but that's not Martin said. He said that the defect report
guarantees that all-bits is ok now. In fact, while the recommendation is
to modify the Standard, the response from the committee could be,
"tough, live with it."


So what's the official status of all these defect reports? Is a response
still to be determined?


Take a look at http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/summary.htm .

Irrwahn
--
Close your eyes and press escape three times.
Nov 13 '05 #10
Irrwahn Grausewitz wrote:
So what's the official status of all these defect reports? Is a response
still to be determined?


Take a look at http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/summary.htm .

Hmmm, what does "closed" mean? That the suggested inclusion was
approved? That it was rejected? For the one in question (263), there's
no Technical Corrigendum publication information.

Brian Rodenborn
Nov 13 '05 #11
Default User <fi********@company.com> writes:
Ben Pfaff wrote:

"Russell Hanneken" <rh*******@pobox.com> writes:
"Martin Dickopp" <ex*************@zero-based.org> wrote in message
news:bk*************@news.t-online.com... Actually, all-bits-zero is guaranteed to be a representation of the value
> zero for all integer types. See Defect Report #263.

Can defect reports guarantee anything? I thought they just described
perceived problems with the standard.


The committee's response to a defect report can clarify and/or
correct problems with the standard.


Right, but that's not Martin said. He said that the defect report
guarantees that all-bits is ok now. In fact, while the recommendation is
to modify the Standard, the response from the committee could be,
"tough, live with it."


It is my understanding (i.e., I was told in comp.std.c once) that the
Committee's response to Defect Reports is in fact normative.

In this case, I was extremely surprised when first learned about DR#263,
and I still find the situation that the DR created quite unsatisfactory.
From reading the standard, I would have thought that padding bits in
integer types would for example allow an efficient implementation on
hardware where the machine word has an odd parity bit. But with the
requirement that all-bits-zero must be a valid representation of zero,
such a machine couldn't just use its hardware word to store integer types.
Given this requirement, I do in fact wonder why the Committee allowed
padding bits and trap representations of integer types in the first
place.

Martin
Nov 13 '05 #12
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
So what's the official status of all these defect reports? Is a response
still to be determined?


See <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/summary.htm>, which lists
the status of each Defect Report.

Martin
Nov 13 '05 #13
In article <bk*************@news.t-online.com>,
Martin Dickopp <ex*************@zero-based.org> wrote:
Default User <fi********@company.com> writes:
Ben Pfaff wrote:

"Russell Hanneken" <rh*******@pobox.com> writes:

> "Martin Dickopp" <ex*************@zero-based.org> wrote in message
> news:bk*************@news.t-online.com...
> > Actually, all-bits-zero is guaranteed to be a representation of the
> > value
> > zero for all integer types. See Defect Report #263.
>
> Can defect reports guarantee anything? I thought they just described
> perceived problems with the standard.

The committee's response to a defect report can clarify and/or
correct problems with the standard.


Right, but that's not Martin said. He said that the defect report
guarantees that all-bits is ok now. In fact, while the recommendation is
to modify the Standard, the response from the committee could be,
"tough, live with it."


It is my understanding (i.e., I was told in comp.std.c once) that the
Committee's response to Defect Reports is in fact normative.

In this case, I was extremely surprised when first learned about DR#263,
and I still find the situation that the DR created quite unsatisfactory.
From reading the standard, I would have thought that padding bits in
integer types would for example allow an efficient implementation on
hardware where the machine word has an odd parity bit. But with the
requirement that all-bits-zero must be a valid representation of zero,
such a machine couldn't just use its hardware word to store integer types.


Such a machine would have to be able to explictely read parity bits by
using a char*. I think that would be highly unusual.
Given this requirement, I do in fact wonder why the Committee allowed
padding bits and trap representations of integer types in the first
place.


There are actually real machines, built and sold today, that have
padding bits in integer types. There is one DSP with 32 bit int and 40
bit long; the 40 bit long is stored in 64 bits including 24 padding
bits. (This machine supports 40 bit integer arithmetic which is slightly
slower than 32 bit, but significantly faster than full 64 bit
arithmetic, so it is quite useful for the intended market).
Nov 13 '05 #14
Default User <fi********@company.com> wrote:
Irrwahn Grausewitz wrote:
>So what's the official status of all these defect reports? Is a response
>still to be determined?


Take a look at http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/summary.htm .

Hmmm, what does "closed" mean? That the suggested inclusion was
approved? That it was rejected? For the one in question (263), there's
no Technical Corrigendum publication information.

Hmmm, I can only assume they made a cut&paste mistake, because otherwise
it would mean that DR263 ends with an IMHO nonsensical repetition of
it's last paragraph. But this is only guessing on my side...

DR263>
DR263> [...]
DR263>
DR263> Suggested Technical Corrigendum
DR263>
DR263> Append to 6.2.6.2#5:
DR263>
DR263> For any integer type, the object representation where all the
DR263> bits are zero shall be a representation of the value zero in
DR263> that type.
DR263>
DR263> ----------------------------------------------------------------
DR263>
DR263> Suggested Technical Corrigendum
^^^^^^^^^
I can only /suppose/ this was intented to read 'Proposed'.
Maybe one of the officials can point it out???

DR263> Append to 6.2.6.2#5:
DR263>
DR263> For any integer type, the object representation where all the
DR263> bits are zero shall be a representation of the value zero in
DR263> that type.
DR263>

Regards

Irrwahn
--
Close your eyes and press escape three times.
Nov 13 '05 #15
Martin Dickopp wrote:
It is my understanding (i.e., I was told in comp.std.c once) that the
Committee's response to Defect Reports is in fact normative.

I see. So the response is the suggested inclusion into a TC of the
changes, and since it's closed it's effectively approved?

Brian Rodenborn
Nov 13 '05 #16
Russell Hanneken <rh*******@pobox.com> wrote:
"Martin Dickopp" <ex*************@zero-based.org> wrote in message
news:bk*************@news.t-online.com...
"Russell Hanneken" <rh*******@pobox.com> writes:
> memset could give every byte in the array a value of (unsigned char) 0.
> That's fine if you have an array of unsigned chars, but if you have an
> array of, say, ints, setting every byte to 0 isn't guaranteed to give
> every element the value 0.


Actually, all-bits-zero is guaranteed to be a representation of the value
zero for all integer types. See Defect Report #263.


Can defect reports guarantee anything? I thought they just described
perceived problems with the standard.

Anyway, even if all-bits-zero is guaranteed to be a representation of the
value zero for all integer types, does that mean we can use memset to zero
an array of ints? As I understand it, if we tell memset to write zeros,
we're telling it to write some valid unsigned char representation of 0 to
each byte. According to what you're saying, all-bits-zero must be *a* valid
representation of unsigned char 0, but it doesn't have to be the only one.
How do we know memset will choose the all-bits-zero representation?


unsigned char must be a pure binary representation with no padding bits,
and since it's unsigned no signing issues come into play.

- Kevin.

Nov 13 '05 #17
Martin Dickopp <ex*************@zero-based.org> spoke thus:
See <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/summary.htm>, which lists
the status of each Defect Report.


Does "closed" equate to "approved" or what? (sorry if I'm being dense...)

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #18

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

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
4
by: Brian Folke Seaberg | last post by:
I have been examining the documentation for a class library written in C++. It seems that most of the classes in that library use two stage initialization. Objects are not ready for use after...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
10
by: charlie.xia.fdu | last post by:
Hi C++ users, for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {} example from: http://www.tech-faq.com/iterations.shtml Is not valid in my eclipse cdt. Is there multiple initialization in C++?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.