473,811 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this type of char array initization legal?

the code below will compile in visual c++ 2003, but im not sure its
valid.

unsigned char myString[200] = "";

after this line executes, all the bytes within myString are indeed set
to '0's' but is this really valid c++ or c? where can I find out how
this is implemented?

Im concerned because I had a 3rd party library wrapper which was
crashing, and I was able to alleviate the crash by changing the
initialization method from the above to ...

unsigned char myString[200];

memset( myString, 0, sizeof( myString ) );

any guidance is greatly appreciated!
-Velik
Oct 24 '08
24 2200
blargg wrote:
In article
<79************ *************** *******@y29g200 0hsf.googlegrou ps.com>, James
Kanze <ja*********@gm ail.comwrote:
>On Oct 24, 10:46=A0pm, Pete Becker <p...@versatile coding.com>
wrote:
>>On 2008-10-24 16:18:56 -0400, Victor Bazarov
<v.Abaza...@c omAcast.netsaid :
8.5.1/7:
<<If there are fewer initializers in the list than there are members in
the aggregate, then each member not
explicitly initialized shall be value-initialized (8.5).>>
Well, yes, that's the requirement for aggregate
initializatio n. But does aggregate initialization apply here?
Aggregate initialization is indicated by "a brace-enclosed,
comma-separated list ...", which isn't present here. I don't
see anything in 8.5.2 [dcl.init.string] that says that
aggregate initialization is used, although the "optionally
enclosed in braces" hints that it may be.
Well, I rather think it was intended, although you do have a
point. Probably a defect in the standard. (Maybe I should
raise an issue. I'd love to say that it was just editorial, and
just push it off on you, but I think it's a little too much for
that.)

If it's not required, then the following would result in a[2] and a[3]
having const indeterminate values, a rather odd situation:

char const a [4] = "X";
Not sure about the "odd" comment. The compiler that is created
following the rule that it's allowed to forgo the initialisation of the
array elements beyond the terminating null char, would probably *warn*
about the unintialised const and force using the curly brackets form:

char const a [4] = { 'X' };

which ensures that the tail is value-initialised.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 25 '08 #21
On Oct 24, 11:46*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-10-24 17:17:10 -0400, "Default User" <defaultuse...@ yahoo.comsaid:
Pete Becker wrote:
On 2008-10-24 16:29:45 -0400, "Default User"
<defaultuse...@ yahoo.comsaid:
>C89's wording was similar to the C++ standard, but had
further examples to show that initialization with a string
literal is identical to a brace-enclosed list of the
characters, which would amount to the same thing.
So C99 made it crystal clear, presumably because C90 wasn't. <g>
It was, I think, clear enough. It just wasn't all put
together nicely. They had examples to show that:
* *char str[] = "XYZ";
Was equivalent to:
* *char str[] = {'X', 'Y', 'Z', '\0'};
So if a string literal is equivalent to a brace-enclosed
list of characters, then the "fewer initializer" rule would
come into play to finish out an array larger than the string
literal with '\0'.
Examples in standards are not normative. That is, they do not
impose requirements. They illustrate what the words say. If
the words don't say it, examples don't make it true.
In this case, the words do say what the example shows. But it
has nothing to do with the case at hand, something like:
char str[ 20 ] = "abc" ;
Is this guaranteed to initialize all 20 bytes, or just the first
four?

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 25 '08 #22
On 2008-10-24 19:21:26 -0400, "Default User" <de***********@ yahoo.comsaid:
>
Searching back in comp.std.c, I find that apparently this issue was
submitted as a Defect Report / Technical Corrigendum that "clarifies
that a string literal is equivalent to a brace-enclosed
list of characters."

So the issue has come up before there, and that report is probably why
C99 explicitly said so.
Sigh. That's what I said several messages back. C99 changed the wording
because the C90 words weren't clear.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 25 '08 #23
Pete Becker wrote:
On 2008-10-24 19:21:26 -0400, "Default User"
<de***********@ yahoo.comsaid:

Searching back in comp.std.c, I find that apparently this issue was
submitted as a Defect Report / Technical Corrigendum that "clarifies
that a string literal is equivalent to a brace-enclosed
list of characters."

So the issue has come up before there, and that report is probably
why C99 explicitly said so.

Sigh. That's what I said several messages back. C99 changed the
wording because the C90 words weren't clear.
You mentioned nothing about this defect report. My research was to add
to the previous discussion, not to refute anything.


Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Oct 25 '08 #24
On Oct 24, 10:47*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-10-24 17:11:10 -0400, James Kanze <james.ka...@gm ail.comsaid:
On Oct 24, 10:46*pm, Pete Becker <p...@versatile coding.com>
wrote:
On 2008-10-24 16:18:56 -0400, Victor Bazarov
<v.Abaza...@com Acast.netsaid:
8.5.1/7:
<<If there are fewer initializers in the list than there are members in
the aggregate, then each member not
explicitly initialized shall be value-initialized (8.5).>>
Well, yes, that's the requirement for aggregate
initialization. But does aggregate initialization apply
here? Aggregate initialization is indicated by "a
brace-enclosed, comma-separated list ...", which isn't
present here. I don't see anything in 8.5.2
[dcl.init.string] that says that aggregate initialization
is used, although the "optionally enclosed in braces" hints
that it may be.
Well, I rather think it was intended, although you do have a
point. *Probably a defect in the standard. *(Maybe I should
raise an issue. *I'd love to say that it was just editorial,
and just push it off on you, but I think it's a little too
much for that.)
Me, too. <g>
Done. I've raised the issue with the committee.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 26 '08 #25

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

Similar topics

3
2256
by: Jonathan Turkanis | last post by:
Can the syntax for function types be used within a function type declaration as a synonym for the corresponding pointer-to-function type? For instance, is int(char(long)) a valid type equivalent to
30
5076
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () { srand(time(0)); int array_length; int count;
21
2521
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A String Literal";//the compiler isuues no error/warning here // but ideally it should...as p is a non-const
7
4363
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either (1) or (2) will assign dword value to not dword aligned address. The question is - will this code be fatal on some systems??
4
1676
by: Andrew K | last post by:
Hello, I have run into a problem with VC7 that worked in VC6. These two sections should be exactly the same... class test { public: virtual void blah(void)=0;}; void func(test) {} and
51
23751
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc 10018-clc.c: In function `main': 10018-clc.c:22: warning: array subscript has type `char' I don't like warnings ... or casts.
33
3669
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of serializing the object array). I can think of three ways to do this:
3
1830
by: somenath | last post by:
Hi All, I got some questions regarding the type of expression . For example 1)char *const *(*ptr)(); Here the type of ptr is it is constant pointer to a function which accept unspecified number of argument and returns char * 2) char *const *(ptr1)();
0
9730
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
10651
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
10392
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
10403
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6893
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
5555
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
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
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
3868
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.