How to avoid using arrays for strings???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mike3

    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?
  • peter koch

    #2
    Re: How to avoid using arrays for strings???

    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

    Comment

    • Richard Heathfield

      #3
      Re: How to avoid using arrays for strings???

      [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

      Comment

      • mike3

        #4
        Re: How to avoid using arrays for strings???

        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:



        "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

        Comment

        • Richard Heathfield

          #5
          Re: How to avoid using arrays for strings???

          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:
          >

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

          Comment

          • James Kanze

            #6
            Re: How to avoid using arrays for strings???

            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:james.kan ze@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

            Comment

            • Pete Becker

              #7
              Re: How to avoid using arrays for strings???

              On 2007-11-16 08:31:37 -0500, James Kanze <james.kanze@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)

              Comment

              • Richard Heathfield

                #8
                Re: How to avoid using arrays for strings???

                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

                Comment

                • James Kanze

                  #9
                  Re: How to avoid using arrays for strings???

                  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:james.kan ze@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

                  Comment

                  • Richard Heathfield

                    #10
                    Re: How to avoid using arrays for strings???

                    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

                    Comment

                    • Richard Heathfield

                      #11
                      Re: How to avoid using arrays for strings???

                      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

                      Comment

                      • Nemanja Trifunovic

                        #12
                        Re: How to avoid using arrays for strings???

                        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.

                        Comment

                        • mike3

                          #13
                          Re: How to avoid using arrays for strings???

                          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?

                          Comment

                          • Richard Heathfield

                            #14
                            Re: How to avoid using arrays for strings???

                            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

                            Comment

                            • Bob Masta

                              #15
                              Re: How to avoid using arrays for strings???

                              On Fri, 16 Nov 2007 19:00:19 +0000, Richard Heathfield
                              <rjh@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
                              DAQARTA (Data AcQuisition And Real-Time Analysis) is FREE software that turns your Windows sound card into a full-featured oscilloscope, FFT spectrum analyzer (with color spectrograms/voiceprints), signal averager, frequency counter, voltmeter, sound level meter, phase meter, LCR meter, THD and IMD distortion meters, sound-activated recorder, plus 8-channel signal generator (including engine crank/cam sensor simulator) and DaqMusiq and Music_from_Anything MIDI music generators. Arduino and Numato support. Advanced options for lab or personal use. Science (and fun) with your Sound Card!

                              Scope, Spectrum, Spectrogram, FREE Signal Generator
                              Science with your sound card!

                              Comment

                              Working...