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

How to zero-initialize a C string (array 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 use an empty initializer list, {}, when
zero-initializing a C-style string? I was wondering, because the book
C++ Coding Standards (Sutter & Alexandrescu) says at item 19, "Always
initialize variables":

char path[MAX_PATH] = { '\0' };
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #1
9 26939
On Sat, 11 Dec 2004 15:47:16 +0100, Niels Dekker - no reply address
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] = {};

If so, why don't we all use an empty initializer list, {}, when
zero-initializing a C-style string? I was wondering, because the book
C++ Coding Standards (Sutter & Alexandrescu) says at item 19, "Always
initialize variables":

char path[MAX_PATH] = { '\0' };


you need to fill the array with '\0' because if the array is of dimension
10 let's say, the last char is '\0' and you set the first 5 characters
to let's say 'hello'
you'll get :

0 1 2 3 4 5 6 7 8 9
h e l l o t d r a '\0'

because the 5-6-7-8 characters were not set and are still using unset
memory. Maybe your compiler will clear the memory for you but I'm not
shure it's a standard behavior and it's not a good habbit to take
however if you do
memset(path, '\0', 10);
//set path value here
you'll get

hello'\0'
whatever's the lenght of path
which is the wanted result

or even better !!
give a try to the STL

#include <string>
std::string path = "hello";
beside std::string is a lot more secure than char[] because it's size
will autoincrease as you append data.

A small performance hit but a more secure program...

beside;
cin >> path; //where path is std::string

is almost impossible to overflow... however
cin >> path; //where path is char[]

is really easy to overflow.. if you allocated 10 char and the user input
40 characters... your program may not recover


Hope it helped
========
Eric Boutin
er********@users.sourceforge.net

Jul 22 '05 #2
Eric Boutin wrote:
Niels Dekker 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] = {};

If so, why don't we all use an empty initializer list, {}, when
zero-initializing a C-style string? I was wondering, because the book
C++ Coding Standards (Sutter & Alexandrescu) says at item 19, "Always
initialize variables":

char path[MAX_PATH] = { '\0' };


you need to fill the array with '\0' because if the array is of dimension
10 let's say, the last char is '\0' and you set the first 5 characters
to let's say 'hello'
you'll get :

0 1 2 3 4 5 6 7 8 9
h e l l o t d r a '\0'

because the 5-6-7-8 characters were not set and are still using unset
memory. Maybe your compiler will clear the memory for you but I'm not
shure it's a standard behavior and it's not a good habbit to take
however if you do
memset(path, '\0', 10);


The expression

char yo[3][5][7] = { 0 };

zero-fills everything, relatively optimally. No character contains the
memory's previous contents.

Avoid memset() in C++ as a rule of thumb. There are various reasons why.
wchar_t d[8] = {};


I don't know about that, but typing the 0 won't kill you.
or even better !!
give a try to the STL

#include <string>
std::string path = "hello";


Yep. Learn to use C++ as a high-level language first, 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] = {L'\0'};
wchar_t b[8] = {'\0'};
wchar_t c[8] = {0};
wchar_t d[8] = {};

If so, why don't we all use an empty initializer list, {}, when
Because we all don't know that using {} works, I do and I use it.

It may be 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 19, "Always
initialize variables":

char path[MAX_PATH] = { '\0' };


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
Phlip wrote:
wchar_t d[8] = {};

I don't know about that, but typing the 0 won't kill you.


I'd rather not use an integer (0) when initializing a character. On the
other hand, using L'\0' is quite verbose. Actually I used to do:

wchar_t e[8] = L"";

But now I realize that this L"" literal might unnecessarely take up some
space (however small) while running my program.

An empty initializer list, {}, seems preferable, but apparently most C++
programmers aren't familiar with this notation. Except for Rob
Williscroft, luckily :-)
Eric Boutin wrote: give a try to the STL

#include <string>
std::string path = "hello";
In some cases, especially when you're depending on C-style libraries
(e.g., Windows API functions), using to C-style strings might be more
convenient.
Phlip wrote: Learn to use C++ as a high-level language first, before
worrying about bits.


Okay, but I like the bits too :-)
Regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #5
Niels Dekker - no reply address 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 list, {}, when
zero-initializing a C-style string?
Most likely it is a C language inheritance. C language doesn't allow
'{}' initializer. Sometimes C-compatibility (of header files, for
example) might be important. Also not all compilers (and not all
programmers) "know" that '{}' initializer is allowed in C++ and continue
to enforce C specification for aggregate initializers.
I was wondering, because the book
C++ Coding Standards (Sutter & Alexandrescu) says at item 19, "Always
initialize variables":

char path[MAX_PATH] = { '\0' };


Maybe just to avoid confusing a reader, who's using a compiler that
doesn't accept '{}' initializer (MSVC++ 6, for example).

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6

Niels Dekker - no reply address wrote:
Phlip wrote:
wchar_t d[8] = {};
I don't know about that, but typing the 0 won't kill you.


I'd rather not use an integer (0) when initializing a character. On

the other hand, using L'\0' is quite verbose. Actually I used to do:

wchar_t e[8] = L"";

But now I realize that this L"" literal might unnecessarely take up some space (however small) while running my program.


So may any other expression with the same result. An optimizer may
replace any expression with a more efficient form, as long as both
forms meet the requirements laid out in the standard. E.g. a CPU
which has an register hardwired to zero (e.g. Itanium IIRC ) can
simply use a store instruction using that register as a source.
Regards,
Michiel Salters

Jul 22 '05 #7
>> I used to do:

wchar_t e[8] = L"";

But now I realize that this L"" 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 as 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] = 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[8] = L"";

But now I realize that this L"" 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 as 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] = L"";


Notice the word "may" in my reply. That's not "must". That means
the answer here is "Perhaps". I don't know, it /will/ differ between
compilers and I don't know which version will be more efficient where.

Besides, your use of "memory usage" here and in earlier posts
covers concepts that are not included when the C++ standard talks
about "semantics".

Regards,
Michiel Salters

Jul 22 '05 #9
>> So is there any difference (semantically, including their memory
usage) between the following initializations?

wchar_t a[8] = {L'\0'};
wchar_t e[8] = L"";

Michiel Salters replied: Notice the word "may" in my reply. That's not "must". That means
the answer here is "Perhaps". I don't know, it /will/ differ between
compilers and I don't know which version will be more efficient where.
I don't see how the initialization to {L'\0'} could reasonably be
implemented less efficiently than the initialization to L"". On the
other hand I can imagine an implementation that reserves memory for each
string literal it encounters, therefore having a less efficient
initialization to L"". Am I missing the point?

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 have static
storage duration.
Thanks so far,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #10

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

Similar topics

17
by: matthias_k | last post by:
Hi, I am currently implementing a solver for linear optimization problems using the revised simplex method and I stumbled accross some strange behavior regarding the treatment of the number 0....
28
by: Andreas Prilop | last post by:
Jukka reports on http://www.cs.tut.fi/~jkorpela/chars/spaces.html that Internet Explorer 6 fails on the "zero width space" U+200B ​ Is this observation still valid? For which versions of MS...
0
by: Gary Carson | last post by:
Can anyone tell why the query below would throw a divide-by-zero error? The only reason I can see for the error happening would be if SUM() came out to be zero, but this never happens with the...
25
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...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
4
by: H.S. | last post by:
Hello, I am trying out a few methods with which to test of a given number is practically zero. as an example, does the following test correctly if a given number is zero within machine...
23
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...
4
by: lawrence k | last post by:
I've got a function that starts off like this: function alphabeticalListOfAlbumsByLetter($whichLetter="a") { } I pass in the letters (a, b, c, d, etc) and the numbers (0, 1, 2, 3, 4,...
5
by: DBC User | last post by:
How would you develop a zero footprint application, is the smart client application a zero footprint application? Thanks.
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.