Is the following fully legal and fully portable for all the unsigned
types? The aim of the function is to take an array by reference and set
each element's value to zero.
#include <...
template<class UnsignedNumeric Type, std::size_t const i>
void SetAllElementsT oZero( UnsignedNumeric Type (&array)[i] )
{
memset( array, 0, i * sizeof(Unsigned NumericType) );
//or:
memset( array, 0, sizeof (array) );
}
I writing a function at the moment that's manipulating an array which is
passed to it by reference. It needs to set a certain amount of the
elements to zero. It will only ever be given the unsigned types, e.g.:
unsigned
unsigned char
unsigned short
unsigned long...
Is the code fully portable and well defined? Is there a guarantee in the
Standard that the bit pattern in memory for all the aforementioned types
will be all zeros, ie. 0000 0000?
-Tomás
Mar 5 '06
15 2139 The 2nd methos sets the pointer to "all bits zero" in memory, which may or may not represent "all bits zero" in memory.
Typo:
The 2nd method sets the pointer to "all bits zero" in memory, which may
or may not represent a "null pointer".
Andrew Koenig wrote: If we talk of straight 0, doesn't the fact that only three representations are accepted (two's complement, one's complement, signed magnitude) serve as the guarantee? Signed magnitude and one's complement have a way to represent -0, but that's not what the OP asked.
Well, I can't find any place in the standard that prohibits sign-magnitude notation in which 0 represents negative and 1 represents positive. In such a notation, all bits 0 means -0, which is presumably distinguishable from 0.
However, this is of no relevance, since the OP didn't ask about signed, but
rather only about unsigned types.
"Tomás" <NU**@NULL.NULL > skrev i meddelandet
news:y2******** **********@news .indigo.ie... 2) int* p; memset(p,0,size of(int*) ); Actually come to think of it, if we've no guarantee that an unsigned numeric type stores its zero value as "all bits zero", then we've not guarantee that when we pass a zero literal (ie. 0) to memset, that it will make the memory all bits zero...
First, the 0 isn't unsigned, but a signed int. :-)
Also, it is converted to an unsigned char before it is stored. That
presumably takes care of any pad bits, as an unsigned char cannot have
any.
Bo Persson
Andrew Koenig wrote: If we talk of straight 0, doesn't the fact that only three representations are accepted (two's complement, one's complement, signed magnitude) serve as the guarantee? Signed magnitude and one's complement have a way to represent -0, but that's not what the OP asked.
Well, I can't find any place in the standard that prohibits sign-magnitude notation in which 0 represents negative and 1 represents positive.
How about :
"The range of nonnegative values of a signed integer type is a subrange of
the corresponding unsigned integer type, and the value representation of
each corresponding signed/unsigned type shall be the same."
I don't see why this shouldn't include the zero value. So an unsigned 0 must
have the same value representation as a signed 0.
are there implementations where null pointers aren't zeros?
which?
Diego Martins posted: are there implementations where null pointers aren't zeros? which?
I don't know any off hand. What I do know is:
A) The Standard permits that a pointer not be all bits zero.
And from that, I'd speculate that they allowed this because there is in
fact a system where pointers aren't all bits zeros.
-Tomás This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steven T. Hatton |
last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe
it passed the moderator's veto - and I did not expect or desire anything
different. But the question remains:
ISO/IEC 14882:2003(E) §8.5 says:
To zero-initialize an object of type T means:
5
-- if T is a scalar type (3.9), the object is set to the value of 0 (zero)
converted to T;
|
by: zahni31 |
last post by:
I had trouble to get the hmon work in 8.x FP 8 and FP 9 .
The server runnig Windows 2003 SP1 on a member server.
The db2 services are running under an domain admin account.
Ok, no problem so far (HMON working) .
Now i had made a special local group a the member server (contains both
local and domain admin accounts and some local accounts without local
|
by: Mantorok Redgormor |
last post by:
Finally, control is returned to the host environment. If the value of
status is zero or EXIT_SUCCESS, an implementation-defined form of the
status successful termination is returned.
beyond this paragraph from the standard, I can't determine if this
macro
will always be zero. It would surely be convenient if it is but it
never
states this directly. the "zero or" part, leads me to believe that the
macro
|
by: Christopher Benson-Manica |
last post by:
How does one determine whether a double is equal to -0.0000...? Will
fabs() from math.h return 0.000... on -0.0000...?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
|
by: Lyle Fairfield |
last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp
"If the value of the variant argument is Null, the Nz function returns
the number zero or a zero-length string (always returns a zero-length
string when used in a query expression)"
****
How many records are there in FirstTable in which Product Is Null.
SELECT COUNT(*) AS CountofNullProdcut
| |
by: hireagenius |
last post by:
Hi heros,
I have a text field that stores numbers like 06-1234. I created a combo box
to look up values on a form based on the number. However, I can't get it
formatted correctly. It is formatted @@-@@@@.
The results are 0-61234 in the combo box. Can you assist me?
Thank you
Charlie
--
Message posted via http://www.accessmonster.com
|
by: Zytan |
last post by:
I want to make a zero element array. I know that Nothing is not the
same as a zero element array, since I can't get the length of, or
iterate through, an array = Nothing. I could make a zero element
array like this, but it seems like overkill:
Dim emptyArray as Byte() = System.Text.Encoding.Default.GetBytes("")
Is there a better way?
Zytan
|
by: Hallvard B Furuseth |
last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is
negative zero. So for signed types, x == 0 does not guarantee
(x & foo) == 0. Is that right? (Not that I expect to ever
encounter a non-two's-complement machine. Just wondering.)
--
Hallvard
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |