How To Hardwire A Register To Zero
How to zero-initialize a C string (assortment of wchar_t)?
Are all the following initializations semantically equivalent?
wchar_t a[8] = {L'\0'};
wchar_t b[8] = {'\0'};
wchar_t c[8] = {0};
wchar_t d[8] = {};
If so, why don't we all employ an empty initializer list, {}, when
zero-initializing a C-mode string? I was wondering, considering the volume
C++ Coding Standards (Sutter & Alexandrescu) says at item xix, "Always
initialize variables":
char path[MAX_PATH] = { '\0' };
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #1
nine 25444
On Sat, xi Dec 2004 15:47:16 +0100, Niels Dekker - no answer address
wrote*:
Are all the following initializations semantically equivalent?wchar_t a[8] = {L'\0'};
wchar_t b[8] = {'\0'};
wchar_t c[viii] = {0};
wchar_t d[8] = {};If then, why don't we all use an empty initializer listing, {}, when
zero-initializing a C-style cord? I was wondering, because the book
C++ Coding Standards (Sutter & Alexandrescu) says at detail nineteen, "Always
initialize variables":char path[MAX_PATH] = { '\0' };
you demand to fill the assortment with '\0' because if the array is of dimension
10 let'southward say, the last char is '\0' and you set the first v characters
to let'due south say 'hello'
you'll go :
0 ane 2 3 4 5 6 vii 8 9
h e fifty l o t d r a '\0'
considering the 5-6-7-8 characters were not set and are all the same using unset
memory. Maybe your compiler will clear the memory for you but I'yard not
shure it's a standard beliefs and it's not a good habbit to take
still if you lot do
memset(path, '\0', 10);
//set path value here
you'll get
hullo'\0'
whatever's the lenght of path
which is the wanted result
or even improve !!
requite a endeavour to the STL
#include <string>
std::string path = "hello";
beside std::string is a lot more than secure than char[] because it'southward size
will autoincrease as you append data.
A pocket-size performance hit just a more secure programme...
beside;
cin >> path; //where path is std::cord
is almost impossible to overflow... even so
cin >> path; //where path is char[]
is really easy to overflow.. if yous allocated 10 char and the user input
40 characters... your program may not recover
Promise it helped
========
Eric Boutin
er********@users.sourceforge.internet
Jul 22 '05 #2
Eric Boutin wrote:
Niels Dekker wrote :
Are all the following initializations semantically equivalent?wchar_t a[viii] = {L'\0'};
wchar_t b[8] = {'\0'};
wchar_t c[viii] = {0};
wchar_t d[8] = {};If and then, why don't we all use an empty initializer list, {}, when
zero-initializing a C-fashion cord? I was wondering, because the volume
C++ Coding Standards (Sutter & Alexandrescu) says at particular 19, "Always
initialize variables":char path[MAX_PATH] = { '\0' };
you demand to make full the array with '\0' because if the array is of dimension
ten allow'southward say, the terminal char is '\0' and you set the beginning 5 characters
to permit'southward say 'hi'
you'll get :0 one 2 iii 4 5 6 7 eight nine
h eastward l l o t d r a '\0'because the five-six-7-8 characters were not set and are still using unset
memory. Perhaps your compiler will articulate the retention for you but I'm non
shure it'due south a standard behavior and it's not a good habbit to take
however if you practice
memset(path, '\0', ten);The expression
char yo[three][five][7] = { 0 };
zero-fills everything, relatively optimally. No character contains the
memory's previous contents.Avoid memset() in C++ as a dominion of thumb. In that location are various reasons why.
wchar_t d[8] = {};I don't know nigh that, but typing the 0 won't kill you.
or fifty-fifty better !!
give a try to the STL#include <string>
std::string path = "hullo";
Yep. Acquire to utilise C++ as a loftier-level linguistic communication kickoff, before worrying about
bits.
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3
Niels Dekker - no reply address wrote in news:41BB0874.F10CB104
@this.is.invalid in comp.lang.c++:
Are all the following initializations semantically equivalent?wchar_t a[8] = {Fifty'\0'};
wchar_t b[8] = {'\0'};
wchar_t c[8] = {0};
wchar_t d[8] = {};If so, why don't we all employ an empty initializer list, {}, when
Because we all don't know that using {} works, I do and I employ it.It may exist that using {} doesn't or didn't work with some C and pre-standard
C++ compilers, but AIUI using { 0 } has allways worked, which is probably
why the advice is repeated.
zero-initializing a C-style string? I was wondering, because the book
C++ Coding Standards (Sutter & Alexandrescu) says at item xix, "Always
initialize variables":char path[MAX_PATH] = { '\0' };
Rob.
--
http://www.victim-prime number.dsl.pipex.com/
Jul 22 '05 #4
Phlip wrote:
wchar_t d[8] = {};
I don't know about that, just typing the 0 won't kill yous.I'd rather not use an integer (0) when initializing a character. On the
other paw, using L'\0' is quite verbose. Actually I used to practise:wchar_t eastward[8] = 50"";
Merely now I realize that this L"" literal might unnecessarely take up some
space (withal small) while running my program.An empty initializer listing, {}, seems preferable, but apparently most C++
programmers aren't familiar with this note. Except for Rob
Williscroft, luckily :-)
Eric Boutin wrote: requite a endeavor to the STL#include <string>
std::string path = "hi";
In some cases, especially when you're depending on C-fashion libraries
(e.g., Windows API functions), using to C-fashion strings might exist more
user-friendly.
Phlip wrote: Acquire to apply C++ as a high-level language first, before
worrying nigh bits.
Okay, but I like the bits likewise :-)
Regards,
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #5
Niels Dekker - no respond accost wrote:
Are all the following initializations semantically equivalent?wchar_t a[8] = {L'\0'};
wchar_t b[8] = {'\0'};
wchar_t c[8] = {0};
wchar_t d[8] = {};
Yes.
If so, why don't we all use an empty initializer listing, {}, when
zero-initializing a C-manner string?
Most likely it is a C language inheritance. C language doesn't permit
'{}' initializer. Sometimes C-compatibility (of header files, for
example) might be important. Also non all compilers (and not all
programmers) "know" that '{}' initializer is allowed in C++ and proceed
to enforce C specification for aggregate initializers.
I was wondering, considering the book
C++ Coding Standards (Sutter & Alexandrescu) says at particular 19, "Always
initialize variables":char path[MAX_PATH] = { '\0' };
Perhaps simply to avert confusing a reader, who's using a compiler that
doesn't accept '{}' initializer (MSVC++ vi, for instance).
--
Best regards,
Andrey Tarasevich
Jul 22 '05 #6
Niels Dekker - no answer address wrote:
Phlip wrote:wchar_t d[8] = {};
I don't know nearly that, but typing the 0 won't kill yous.I'd rather not use an integer (0) when initializing a character. On
the other hand, using 50'\0' is quite verbose. Actually I used to exercise:wchar_t eastward[viii] = Fifty"";
Only now I realize that this L"" literal might unnecessarely take up some infinite (yet small) while running my program.
So may any other expression with the same outcome. An optimizer may
replace whatever expression with a more efficient grade, as long every bit both
forms encounter the requirements laid out in the standard. E.g. a CPU
which has an register hardwired to zero (due east.g. Itanium IIRC ) can
only use a store instruction using that annals equally a source.
Regards,
Michiel Salters
Jul 22 '05 #7
>> I used to do:
wchar_t eastward[8] = L"";But now I realize that this L"" literal might unnecessarely take upward
some space (however pocket-sized) while running my programme.
Michiel Salters replied: So may whatever other expression with the same result. An optimizer may
replace whatever expression with a more efficient class, equally long every bit both
forms meet the requirements laid out in the standard.
So is there whatsoever difference (semantically, including their retention usage)
betwixt the following initializations?
wchar_t a[8] = {Fifty'\0'};
wchar_t east[eight] = L"";
Kind regards,
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #8
Niels Dekker - no reply address wrote:
I used to do:wchar_t e[eight] = L"";
But now I realize that this Fifty"" literal might unnecessarely take up some space (however small) while running my program.
Michiel Salters replied:So may any other expression with the same result. An optimizer may
replace any expression with a more efficient form, as long every bit both
forms meet the requirements laid out in the standard.So is there any difference (semantically, including their memory
usage) between the following initializations?wchar_t a[8] = {L'\0'};
wchar_t e[8] = Fifty"";
Notice the word "may" in my respond. That'southward non "must". That ways
the answer here is "Perhaps". I don't know, it /will/ differ between
compilers and I don't know which version will exist more efficient where.
Too, your utilize of "retention usage" here and in earlier posts
covers concepts that are non included when the C++ standard talks
about "semantics".
Regards,
Michiel Salters
Jul 22 '05 #nine
>> And so is there whatsoever divergence (semantically, including their memory
usage) between the post-obit initializations?wchar_t a[8] = {Fifty'\0'};
wchar_t due east[8] = Fifty"";
Michiel Salters replied: Notice the word "may" in my answer. That's not "must". That means
the respond here is "Perhaps". I don't know, it /will/ differ between
compilers and I don't know which version will exist more efficient where.
I don't come across how the initialization to {L'\0'} could reasonably be
implemented less efficiently than the initialization to L"". On the
other manus I can imagine an implementation that reserves retention for each
string literal it encounters, therefore having a less efficient
initialization to 50"". Am I missing the bespeak?Besides, your use of "memory usage" here and in earlier posts
covers concepts that are not included when the C++ standard talks
about "semantics".
By "memory usage" I refer to the fact that string literals accept static
storage elapsing.
Thank you so far,
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #10
This word thread is airtight
Replies have been disabled for this give-and-take.
Source: https://bytes.com/topic/c/answers/136408-how-zero-initialize-c-string-array-wchar_t
Posted by: healeysaings.blogspot.com

0 Response to "How To Hardwire A Register To Zero"
Post a Comment