473,881 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 27123
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********@user s.sourceforge.n et

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.F 10CB104
@this.is.invali d 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
2874
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. I am not sure whether this is compiler or language related though. The first problem is, that there are two representations of zero, namely +0 and -0. Does IEEE for floating points allow this? Maybe I didn't activate IEEE floating point numbers?
28
9094
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 Windows does it apply? Does it depend on the encoding (charset)? I have a test page in three encodings: http://www.unics.uni-hannover.de/nhtcapri/temp/zwsp.html http://www.unics.uni-hannover.de/nhtcapri/temp/zwsp.html11...
0
2104
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 data I'm using. SOME of the values in the EXP_NET column are zero, but the sum itself is always non-zero. The query is: TRANSFORM SUM(-)/SUM()*100 AS DATA SELECT TBL.EQUIPMENT AS EQUIPMENT,
25
15647
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
53
8256
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 all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values.
25
4455
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 not a practice I follow, there are no doubt millions of lines of open source libraries and applications that do things like: p = malloc(...);
4
7927
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 precision? I am trying out this method to check for a practical zero in an algorithm I am implementing in C++. ------------------------- #include <iostream> #include <iomanip>
23
4386
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
4
2699
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, etc). It finds in the database records starting with that letter or number. It works fine except when I try to pass in a zero. The PHP
5
3266
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
9927
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11098
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, 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...
0
10717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7953
isladogs
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...
0
7109
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();...
0
5781
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...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.