By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

How to avoid using arrays for strings???

P: n/a
(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.programmer.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
Share this Question
Share on Google+
16 Replies


P: n/a
On 16 Nov., 00:30, mike3 <mike4...@yahoo.comwrote:
(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.programmer.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

P: n/a
[Posted in comp.os.ms-windows.programmer.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

P: n/a
On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.invalidwrote:
[Posted in comp.os.ms-windows.programmer.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

P: n/a
mike3 said:
On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>[Posted in comp.os.ms-windows.programmer.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

P: n/a
On Nov 16, 7:15 am, mike3 <mike4...@yahoo.comwrote:
On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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 objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 16 '07 #6

P: n/a
On 2007-11-16 08:31:37 -0500, James Kanze <ja*********@gmail.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(now 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

P: n/a
James Kanze said:
On Nov 16, 7:15 am, mike3 <mike4...@yahoo.comwrote:
>On Nov 15, 9:26 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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

P: n/a
On Nov 16, 2:44 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-11-16 08:31:37 -0500, James Kanze <james.ka...@gmail.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(now 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 objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 16 '07 #9

P: n/a
James Kanze said:
On Nov 16, 2:44 pm, Pete Becker <p...@versatilecoding.comwrote:
<snip>
>For completeness: std::array<Ty(now 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.programmer.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

P: n/a
mike3 said:

<snip>
Would you, if you were the one making this
program, frown on seeing arrays of "TCHAR" in there?
No, I would not fight the API. That way madness lies. So I'd go for those
arrays of TCHAR (not that I'm a huge Unicode fan, actually).

Nor would I *necessarily* wrap it, although this can sometimes be
beneficial. Start off by programming the API "naturally", and then, when
you find yourself doing the same stuff over and over, maybe that's the
time to wrap it up and perhaps abstract it a little (but don't go
overboard - you want to finish some time, right?).

--
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 #11

P: n/a
On Nov 16, 2:17 am, "Alf P. Steinbach" <al...@start.nowrote:
That's because Windows is based on UCS-2 encoding, and so every C and
C++ compiler for Windows must (in practice) have 16-bit wchar_t.
Actually Windows XP and later use UTF-16 encoding form, including the
surrogate pairs.
Nov 16 '07 #12

P: n/a
On Nov 16, 1:02 pm, Richard Heathfield <r...@see.sig.invalidwrote:
mike3 said:

<snip>
Would you, if you were the one making this
program, frown on seeing arrays of "TCHAR" in there?

No, I would not fight the API. That way madness lies. So I'd go for those
arrays of TCHAR (not that I'm a huge Unicode fan, actually).

Nor would I *necessarily* wrap it, although this can sometimes be
beneficial. Start off by programming the API "naturally", and then, when
you find yourself doing the same stuff over and over, maybe that's the
time to wrap it up and perhaps abstract it a little (but don't go
overboard - you want to finish some time, right?).
So then in this case the arrays may not be such an "evil"
as in other cases, then, provided one handles them safely?
Nov 16 '07 #13

P: n/a
mike3 said:

<snip>
So then in this case the arrays may not be such an "evil"
as in other cases, then, provided one handles them safely?
Whoever told you "arrays are evil" is wrong. Arrays are no more evil than
robots, power saws, or toasting forks.

--
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 #14

P: n/a
On Fri, 16 Nov 2007 19:00:19 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
<snip>
>Please bear in mind that this thread is cross-posted, and I'm reading it in
comp.os.ms-windows.programmer.win32, where arrays are very much a part of
ordinary programming practice, not some kind of anathema to be shunned at
all costs.
I'm also reading from the Win32 group, and am not a C or C++
programmer. So can somebody explain the supposed evils of string
arrays? I know that languages traditionally have "issues" with
strings with indefinite sizes, since a lot of memory manipulation is
needed when they shrink or grow. But that doesn't seem to be the
"evil" being discussed here.

Best regards,
Bob Masta

DAQARTA v3.50
Data AcQuisition And Real-Time Analysis
www.daqarta.com
Scope, Spectrum, Spectrogram, FREE Signal Generator
Science with your sound card!
Nov 17 '07 #15

P: n/a
On Nov 17, 2:46 pm, NoS...@daqarta.com (Bob Masta) wrote:
On Fri, 16 Nov 2007 19:00:19 +0000, Richard
Heathfield<r...@see.sig.invalidwrote:
<snip>
Please bear in mind that this thread is cross-posted, and I'm
reading it in comp.os.ms-windows.programmer.win32, where
arrays are very much a part of ordinary programming practice,
not some kind of anathema to be shunned at all costs.
Not in C++. Not with professional Windows programmers, anyway.
I'm also reading from the Win32 group, and am not a C or C++
programmer. So can somebody explain the supposed evils of string
arrays?
There's no problem with string arrays. The problem is with a
particular type in C++, known as the C style array, because it
is inherited from C. Objects of this type don't obey any of the
rules of normal objects, and using objects of this type requires
significantly more work, for no gain. The result is the
professional software engineers avoid them.
I know that languages traditionally have "issues" with strings
with indefinite sizes, since a lot of memory manipulation is
needed when they shrink or grow. But that doesn't seem to be
the "evil" being discussed here.
Nothing to do with performance. That's another issue. Although
the semantics of C style arrays are killers for compiler
optimization, the replacements currently offered aren't
necessarily any faster. It's not an accident that when Java
fans want to show their language faster than C++, they write
benchmarks making extensive use of arrays of built-in types.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 18 '07 #16

P: n/a
James Kanze said:

<snip>
The problem is with a
particular type in C++, known as the C style array, because it
is inherited from C. Objects of this type don't obey any of the
rules of normal objects,
Er, yes, they do obey the rules of normal objects. They have a size, a
type, and a value.
and using objects of this type requires
significantly more work, for no gain.
I'm sorry, but I can't agree that using arrays is more work than using,
say, a std::vector. I can agree that it *can* be more work, but not that
it necessarily is more work.
The result is the professional software engineers avoid them.
Is this an example of the "true Scotsman" argument? That is, if you find
someone - such as myself - who does not avoid arrays, do you refuse to
consider them to be a professional software engineer?

<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 18 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.