473,836 Members | 1,542 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
16 2553
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
On Nov 16, 2:17 am, "Alf P. Steinbach" <al...@start.no wrote:
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
On Nov 16, 1:02 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
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
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
On Fri, 16 Nov 2007 19:00:19 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
<snip>
>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.
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
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.invali dwrote:
<snip>
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.
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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 18 '07 #16
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3692
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
12627
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
18664
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
6463
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
7199
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
7570
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
15998
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
2186
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
6258
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
9825
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
10846
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
10595
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
10254
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...
1
7793
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
6979
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
5650
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
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4458
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

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.