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 24 2198 Do******@gmail. com wrote:
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
It is valid. Allowed explicitly by the Standard, subclause 8.5.2.
The only reason it could be crashing is if the compiler wastn't
providing proper initialisation for the array. What you could do is
revert this to what it was and put an assertion to see if it's indeed
the problem:
unsigned char myString[200] = "";
#ifndef NDEBUG
for (int iii = 0; iii < 200; ++iii)
ASSERT(myString[iii] == 0);
#endif
Now, if you are saying that you verified that all elements of the array
are indeed set to 0 after the initialisation, then it's OK, and the
crash is due to some other problem which you just *hid* by introducing
your 'memset' "fix", most likely.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On 2008-10-24 12:22:09 -0400, Do******@gmail. com said:
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?
They're set to zero if myString has static storage duration, and that's
also true with no initializer. If it has automatic storage duration,
myString[0] is set to 0.
>
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!
Apparently the third-party library requires an array with all elements
set to zero. That requirement should be in the documentation for the
library.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book) Do******@gmail. com wrote:
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!
Practical advice: change it to
unsigned char myString[ 200 ] = {} ;
What to you mean by "alleviate" the crash?
PS: the language lawyering behind the first initialization is a
bit more complicated than I have time to explain; consider that
the array elements are of type unsigned char (not char) and that
there was a C standard clarification in this area which C++
hasn't picked up (though your C++ compiler might de-facto
implement it).
<http://groups.google.c om/group/comp.lang.c++/msg/d5ac14fbe55de25 b>
--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
On 2008-10-24 12:49:25 -0400, Victor Bazarov <v.********@com Acast.netsaid: Do******@gmail. com wrote:
>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 initializati on method from the above to ...
unsigned char myString[200];
memset( myString, 0, sizeof( myString ) );
any guidance is greatly appreciated!
It is valid. Allowed explicitly by the Standard, subclause 8.5.2.
The only reason it could be crashing is if the compiler wastn't
providing proper initialisation for the array. What you could do is
revert this to what it was and put an assertion to see if it's indeed
the problem:
unsigned char myString[200] = "";
#ifndef NDEBUG
for (int iii = 0; iii < 200; ++iii)
ASSERT(myString[iii] == 0);
#endif
Hmm, is this required if the char array has automatic storage duration?
I have always assumed that it wasn't, that only the characters
corresponding to characters in the initializer would be initialized,
but it doesn't seem completely clear from a quick glance at the
standard.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
On Oct 24, 12:22*pm, DomoC...@gmail. com wrote:
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?
Sounds like you see the results of a program in debug mode. The string
literal "" is actually {'\0'} so everything after myString[0] has an
undeterminate value. Nobody cares whether some compiler initializes or
not the rest of the array with some magic value, there is no
requirement that says it must.
>
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
An array has a special initializer that looks like so:
unsigned char myString[200] = {'a'};
which initializes all elements with 'a' in this case.
Note the element type of the array (unsigned char) and the type in the
array init list.
Now having said that, the above is an array, not a terminated sequence
of unsigned characters.
Another reason to prefer modern containers:
#include <vector>
// all elements initialized: guarenteed
std::vector< unsigned char vuc(200);
// all 'a'
std::vector< unsigned char vuca(200, 'a');
On 2008-10-24 13:35:25 -0400, Salt_Peter <pj*****@yahoo. comsaid:
On Oct 24, 12:22Â*pm, DomoC...@gmail. com wrote:
>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 ho
w
>this is implemented?
Sounds like you see the results of a program in debug mode.
In which case, the debug mode needs further thought. Initializing to
all zeros is a bad idea when debugging, because the result looks like
good data.
The string
literal "" is actually {'\0'} so everything after myString[0] has an
undeterminate value. Nobody cares whether some compiler initializes or
not the rest of the array with some magic value, there is no
requirement that says it must.
>> Im concerned because I had a 3rd party library wrapper which was crashing, and I was able to alleviate the crash by changing the initializati on method from the above to ...
unsigned char myString[200];
memset( myString, 0, sizeof( myString ) );
any guidance is greatly appreciated! -Velik
An array has a special initializer that looks like so:
unsigned char myString[200] = {'a'};
which initializes all elements with 'a' in this case.
This is just straight aggregate initialization, and it initializes all
values after the end of the initializer to 0, not to the value of the
initializer.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
Pete Becker wrote:
On 2008-10-24 12:49:25 -0400, Victor Bazarov <v.********@com Acast.net>
said:
>Do******@gmail. com wrote:
>>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 initializatio n method from the above to ...
unsigned char myString[200];
memset( myString, 0, sizeof( myString ) );
any guidance is greatly appreciated!
It is valid. Allowed explicitly by the Standard, subclause 8.5.2.
The only reason it could be crashing is if the compiler wastn't providing proper initialisation for the array. What you could do is revert this to what it was and put an assertion to see if it's indeed the problem:
unsigned char myString[200] = ""; #ifndef NDEBUG for (int iii = 0; iii < 200; ++iii) ASSERT(myString[iii] == 0); #endif
Hmm, is this required if the char array has automatic storage duration?
I have always assumed that it wasn't, that only the characters
corresponding to characters in the initializer would be initialized, but
it doesn't seem completely clear from a quick glance at the standard.
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).>>
To me it's pretty clear. Each character from the literal initialises
its respective element of the array. The terminating null character
does initialise the corresponding element of the array *too*. If there
are fewer characters in the literal than elements in the array (which is
an aggregate), the rest of the array elements are zero-initialised.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Pete Becker wrote:
On 2008-10-24 12:49:25 -0400, Victor Bazarov
<v.********@com Acast.netsaid:
The only reason it could be crashing is if the compiler wastn't
providing proper initialisation for the array. What you could do
is revert this to what it was and put an assertion to see if it's
indeed the problem:
unsigned char myString[200] = "";
#ifndef NDEBUG
for (int iii = 0; iii < 200; ++iii)
ASSERT(myString[iii] == 0);
#endif
Hmm, is this required if the char array has automatic storage
duration? I have always assumed that it wasn't, that only the
characters corresponding to characters in the initializer would be
initialized, but it doesn't seem completely clear from a quick glance
at the standard.
Interesting. I also didn't find anything explicit regarding string
literals, just the usual "fewer initializer" rule:
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).
The C standard does contain such wording (assuming that it didn't
change from the draft standard):
[#21] If there are fewer initializers in a brace-enclosed
list than there are elements or members of an aggregate, or
fewer characters in a string literal used to initialize an
array of known size than there are elements in the array,
the remainder of the aggregate shall be initialized
implicitly the same as objects that have static storage
duration.
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.
Brian
On 2008-10-24 16:18:56 -0400, Victor Bazarov <v.********@com Acast.netsaid:
Pete Becker wrote:
>On 2008-10-24 12:49:25 -0400, Victor Bazarov <v.********@com Acast.netsaid:
>>Do******@gmail. com wrote: 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 initializati on method from the above to ...
unsigned char myString[200];
memset( myString, 0, sizeof( myString ) );
any guidance is greatly appreciated!
It is valid. Allowed explicitly by the Standard, subclause 8.5.2.
The only reason it could be crashing is if the compiler wastn't providing proper initialisation for the array. What you could do is revert this to what it was and put an assertion to see if it's indeed the problem:
unsigned char myString[200] = ""; #ifndef NDEBUG for (int iii = 0; iii < 200; ++iii) ASSERT(myString[iii] == 0); #endif
Hmm, is this required if the char array has automatic storage duration? I have always assumed that it wasn't, that only the characters correspondin g to characters in the initializer would be initialized, but it doesn't seem completely clear from a quick glance at the standard.
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.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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;
|
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
|
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??
|
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
| |
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.
|
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:
|
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)();
|
by: fuzhen |
last post by:
what's this?
|
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...
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |