473,691 Members | 3,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to avoid using arrays for strings???

(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.program mer.win32
since there's Windows material in here as well as questions related to
standard
C++. Not sure how that'd go over at just comp.lang.c++. If one of
these groups is
too inappropriate, just take it off from where you send your replies.)

Hi.

I'm writing a program for the Windows OS in C++. But it seems the
Windows
functions all accept string _arrays_ of type "TCHAR" (actually,
_pointers_ to
arrays), which can be toggled between the C/C++ primitive types
wchar_t/char,
the former of which is used for Unicode encoding. Will the C++ STL
classes
std::string/std::wstring work in this case? How does it clash with the
Unicode
encodings, if at all? I'd be mad with whoever comes up with the
standards if
it had a problem since UNICODE is used by all sorts of modern
operating
systems, not just Windows!

But with C++, it is said that arrays are "evil". Is it possible to use
the C++ STL
functions for all internal string manipulations _even while I want
Unicode support_
and then only convert to array when I need to send it to the Windows
functions?
If not, should I go and just use arrays up front, or at least write up
some custom
container that will bury the "evil" arrays of TCHAR, keeping them out
of the way
and packaged?
Nov 15 '07 #1
16 2537
On 16 Nov., 00:30, mike3 <mike4...@yahoo .comwrote:
(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.program mer.win32
since there's Windows material in here as well as questions related to
standard
C++. Not sure how that'd go over at just comp.lang.c++. If one of
these groups is
too inappropriate, just take it off from where you send your replies.)

Hi.

I'm writing a program for the Windows OS in C++. But it seems the
Windows
functions all accept string _arrays_ of type "TCHAR" (actually,
_pointers_ to
arrays), which can be toggled between the C/C++ primitive types
wchar_t/char,
the former of which is used for Unicode encoding. Will the C++ STL
classes
std::string/std::wstring work in this case? How does it clash with the
Unicode
encodings, if at all? I'd be mad with whoever comes up with the
standards if
it had a problem since UNICODE is used by all sorts of modern
operating
systems, not just Windows!

But with C++, it is said that arrays are "evil". Is it possible to use
the C++ STL
functions for all internal string manipulations _even while I want
Unicode support_
and then only convert to array when I need to send it to the Windows
functions?
If not, should I go and just use arrays up front, or at least write up
some custom
container that will bury the "evil" arrays of TCHAR, keeping them out
of the way
and packaged?
For all TCHARs passed as constant data, you can use std::string or
std::vector. If the
called function is modifying whats pointed to, you must currently use
std::vector.
There is no need to use a raw array.

/Peter
Nov 15 '07 #2
[Posted in comp.os.ms-windows.program mer.win32]

mike3 said:

<snip>
>
But with C++, it is said that arrays are "evil".
Don't believe all you read. It isn't arrays that are evil. What is evil is
using arrays if you don't understand how they work.

Since arrays are just about the simplest aggregate data structure
imaginable, there is little difficulty in understanding how they work.

If you want to use arrays (and know how), use arrays. Don't be put off by
misplaced zealotry.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 16 '07 #3
On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
[Posted in comp.os.ms-windows.program mer.win32]

mike3 said:

<snip>
But with C++, it is said that arrays are "evil".

Don't believe all you read. It isn't arrays that are evil. What is evil is
using arrays if you don't understand how they work.

Since arrays are just about the simplest aggregate data structure
imaginable, there is little difficulty in understanding how they work.

If you want to use arrays (and know how), use arrays. Don't be put off by
misplaced zealotry.
However, the concerns weren't just based on the claims the
arrays are "evil", but also on this post that was given
when discussing an earlier version of the program which had
a bug in it, and I got this in one of the responses:

http://groups.google.com/group/comp....6278b77597d648

"The main design problem I saw, with just a cursory look at the code,
was
a mix of very high level (abstract operations) and low level
(pointers,
casting), an abstraction gap, indicating one or more missing
intermediate levels.
Try to encapsulate low-level operations in some not-very-high-level
classes. For example, such encapsulation classes, or functions, do
all
pointer stuff, translate from error codes to exceptions, etc. Just
getting that bug-inducing low level stuff /out of the way/, packaged.
Else-thread I have already mentioned another aspect of that high
level
low level clash, that it would be a good idea to use std::vector
instead
of raw arrays and pointers. "

See, he said about the use of std::vector instead of raw
arrays/pointers.
<snip>

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 16 '07 #4
mike3 said:
On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>[Posted in comp.os.ms-windows.program mer.win32]

mike3 said:

<snip>
But with C++, it is said that arrays are "evil".

Don't believe all you read. It isn't arrays that are evil. What is evil
is using arrays if you don't understand how they work.

Since arrays are just about the simplest aggregate data structure
imaginable, there is little difficulty in understanding how they work.

If you want to use arrays (and know how), use arrays. Don't be put off
by misplaced zealotry.

However, the concerns weren't just based on the claims the
arrays are "evil", but also on this post that was given
when discussing an earlier version of the program which had
a bug in it, and I got this in one of the responses:

http://groups.google.com/group/comp....6278b77597d648
<snip>
>
See, he said about the use of std::vector instead of raw
arrays/pointers.
Yes, he did. Like I said, don't believe all you read. The std::vector stuff
is very useful, sure, but making people afraid of arrays (as the "arrays
are evil" faction seem to be trying to do) is a backward step.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 16 '07 #5
On Nov 16, 7:15 am, mike3 <mike4...@yahoo .comwrote:
On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
But with C++, it is said that arrays are "evil".
Not evil, just broken.
Don't believe all you read. It isn't arrays that are evil.
What is evil is using arrays if you don't understand how
they work.
Having worked on C compilers in the past, I think I understand
how they work. Or rather, how they don't work.
Since arrays are just about the simplest aggregate data
structure imaginable, there is little difficulty in
understanding how they work.
If you want to use arrays (and know how), use arrays. Don't
be put off by misplaced zealotry.
It's not misplaced zealotry to encourage people to avoid
mis-features in the language.
However, the concerns weren't just based on the claims the
arrays are "evil",
The "concern" is simple. In C, arrays are simply broken. In
C++, we have other alternatives, which should be used if
possible. (There are still a few cases where C style arrays are
necessary---when you need static initialization, for example,
since neither std::vector nor std::string are PODS.)

--
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
Nov 16 '07 #6
On 2007-11-16 08:31:37 -0500, James Kanze <ja*********@gm ail.comsaid:
>
The "concern" is simple. In C, arrays are simply broken. In
C++, we have other alternatives, which should be used if
possible. (There are still a few cases where C style arrays are
necessary---when you need static initialization, for example,
since neither std::vector nor std::string are PODS.)
For completeness: std::array<Ty(n ow in TR1 and coming in C++0x) can
be statically initialized.

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

Nov 16 '07 #7
James Kanze said:
On Nov 16, 7:15 am, mike3 <mike4...@yahoo .comwrote:
>On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
But with C++, it is said that arrays are "evil".

Not evil, just broken.
Don't believe all you read. It isn't arrays that are evil.
What is evil is using arrays if you don't understand how
they work.

Having worked on C compilers in the past, I think I understand
how they work. Or rather, how they don't work.
I've tried 'em myself, and they work just fine. I was going to make the
rather poor joke "Have you tried switching them on?", except that their
whole advantage is that they don't need to be switched on. They just work,
right out of the box. Yes, they're sharp tools that need careful handling,
I agree. But we don't say "don't use this tool" just because it's sharp
and needs careful handling. Okay, we might say that to children, perhaps.
But to grown-ups we can just say "look out, it's sharp", and expect them
to be bright enough to take appropriate precautions (especially if we
spell out those appropriate precautions for the tool under discussion).
Since arrays are just about the simplest aggregate data
structure imaginable, there is little difficulty in
understanding how they work.
If you want to use arrays (and know how), use arrays. Don't
be put off by misplaced zealotry.

It's not misplaced zealotry to encourage people to avoid
mis-features in the language.
It's misplaced debating to beg the question. :-)
>However, the concerns weren't just based on the claims the
arrays are "evil",

The "concern" is simple. In C, arrays are simply broken.
Well, I disagree. In C, the arrays work perfectly. Perhaps it's only in C++
that they're broken? Except that I find that very hard to believe. (Surely
if C compiler writers are clever enough to get arrays to work, C++
compiler writers are clever enough too? After all, C++ is so much more
complex than C.)
In
C++, we have other alternatives, which should be used if
possible.
Well, in C++ you have other alternatives, which *can* be used if preferred
and which, perhaps very deeply under the hood, use arrays in any case. If
arrays are truly broken, you should not use anything that uses them, so
wouldn't you need a certificate from your compiler vendor to assure you
that none of his fancy STL stuff relies on broken technology?

Not that it would do any good. I'll bet you that if you take a look at the
code a typical processor is actually executing, you'll find array
operations going on all over the place. So you're doomed, because the
hardware itself uses arrays. If arrays are broken, computers are broken,
so you can't trust *anything*. Whoops.
(There are still a few cases where C style arrays are
necessary---when you need static initialization, for example,
since neither std::vector nor std::string are PODS.)
What you're arguing is that there are times when C++ programs must rely on
brokenness. If that is true, then you are arguing that C++ programs are
inherently unreliable. I can't and don't agree with this very negative
view of C++.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 16 '07 #8
On Nov 16, 2:44 pm, Pete Becker <p...@versatile coding.comwrote :
On 2007-11-16 08:31:37 -0500, James Kanze <james.ka...@gm ail.comsaid:
The "concern" is simple. In C, arrays are simply broken. In
C++, we have other alternatives, which should be used if
possible. (There are still a few cases where C style arrays are
necessary---when you need static initialization, for example,
since neither std::vector nor std::string are PODS.)
For completeness: std::array<Ty(n ow in TR1 and coming in
C++0x) can be statically initialized.
That's what I'd heard. I've not verified in detail, but from
what little I've seen, it should eliminate all uses of C style
arrays entirely.

--
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
Nov 16 '07 #9
James Kanze said:
On Nov 16, 2:44 pm, Pete Becker <p...@versatile coding.comwrote :
<snip>
>For completeness: std::array<Ty(n ow in TR1 and coming in
C++0x) can be statically initialized.

That's what I'd heard. I've not verified in detail, but from
what little I've seen, it should eliminate all uses of C style
arrays entirely.
No, it will simply mean that it's possible for C++ users to choose whether
or not they wish to use array syntax.

Please bear in mind that this thread is cross-posted, and I'm reading it in
comp.os.ms-windows.program mer.win32, where arrays are very much a part of
ordinary programming practice, not some kind of anathema to be shunned at
all costs.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 16 '07 #10

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

Similar topics

4
3687
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array of the following kind: $people = array(); // maps age and e-mail address to names $people = array(21, "paul@foo.bar"); $people = array(22, "linda@bar.foo"); $people = array(19, "max@foobar.foobar");
5
12621
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
10
18296
by: spoc | last post by:
I have been using memcpy to copy one class to another of the same type. There are reasons why I had to do this bug am getting some odd crashes and maybe I'm doing something dodgy copying classes like this? The classes contain no dynamicaly allocated data, just standard types and arrays. Here is an example of what I'm doing, I know in isolation it may seem odd. I am presuming this will copy all data ok but not really sure of the workings and...
2
6453
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as "*?<>/\|\\" If one of the chars are found, I'd like to replace it with a "-" and then commit the change to the actual file system. I know that a scripting language may be better suited for this, but I'm experimenting with C and I'm trying to...
2
7183
by: Steve | last post by:
I want an initializer for an array of pointers to arrays of strings. So I can do something like this: const char *t1 = { "a", "b", "c", NULL }; const char *t2 = { "p", "q", NULL }; const char *t3 = { "w", "x", "y", "z", NULL }; const char **test = { t1, t2, t3, NULL }; I was wondering whether the is a more elegant way of writing such an
7
7562
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it. Also, I was wondering could there be issues in not making copies when converting??? I was thinking, when "pointing" to the same data with both managed and unmanaged strings/arrays couldn't there be issues such as the garbage collector trying to...
2
15990
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my script. Now I want to delete a line from a file. Line being the strings I got/saved to/from array of arrays.
4
2176
by: bei | last post by:
Hi, I am trying to write several arrays into one file, with one arrays in one column. Each array (column) is seperated by space. ie. a= b= c= 1 5 9 2 6 10 3 7 11 4 8 12
13
6240
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things get me, which brings me to... I'm trying to create a program that gets a string from standard input and then manipulates it a little bit. It has to be a char array and not use string from the library. Here are my prototypes:
0
8531
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,...
0
9077
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...
1
8786
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
8793
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 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...
0
4322
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
4550
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2965
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
2225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1952
bsmnconsultancy
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...

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.