2D-array of pointers, set all to null?

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

    2D-array of pointers, set all to null?

    Hello, is there something neat in the c++ standard library that can help
    me set all elements of a two-dimensional array of pointers to NULL? Like
    fill_n(), maybe? Right now, I'm using two for loops (one nested) and
    wanted to know what alternatives there are. My compiler ships with a
    preliminary TR1-implementation.
    Another thing I need to do is to call delete on the pointers in the
    array at some other point in the program, am I stuck with the two for
    loops there?

    - Eric
  • Juha Nieminen

    #2
    Re: 2D-array of pointers, set all to null?

    WP wrote:
    Hello, is there something neat in the c++ standard library that can help
    me set all elements of a two-dimensional array of pointers to NULL? Like
    fill_n(), maybe? Right now, I'm using two for loops (one nested) and
    wanted to know what alternatives there are. My compiler ships with a
    preliminary TR1-implementation.
    Another thing I need to do is to call delete on the pointers in the
    array at some other point in the program, am I stuck with the two for
    loops there?
    Well, you could do it with a single memset() call, but I'm not
    absolutely sure how portable that is (although I believe it's portable
    *in practice*).

    Comment

    • Pete Becker

      #3
      Re: 2D-array of pointers, set all to null?

      On 2008-07-12 06:28:49 -0400, WP <invalid@invali d.invalidsaid:
      Hello, is there something neat in the c++ standard library that can
      help me set all elements of a two-dimensional array of pointers to
      NULL? Like fill_n(), maybe? Right now, I'm using two for loops (one
      nested) and wanted to know what alternatives there are. My compiler
      ships with a preliminary TR1-implementation.
      Another thing I need to do is to call delete on the pointers in the
      array at some other point in the program, am I stuck with the two for
      loops there?
      >
      What's wrong with two loops? That matches the data structure.

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

      Comment

      • James Kanze

        #4
        Re: 2D-array of pointers, set all to null?

        On Jul 12, 12:53 pm, Juha Nieminen <nos...@thanks. invalidwrote:
        WP wrote:
        Hello, is there something neat in the c++ standard library
        that can help me set all elements of a two-dimensional array
        of pointers to NULL? Like fill_n(), maybe? Right now, I'm
        using two for loops (one nested) and wanted to know what
        alternatives there are. My compiler ships with a preliminary
        TR1-implementation.
        Another thing I need to do is to call delete on the pointers
        in the array at some other point in the program, am I stuck
        with the two for loops there?
        Well, you could do it with a single memset() call, but I'm not
        absolutely sure how portable that is (although I believe it's
        portable *in practice*).
        You can't set pointers to null with memset. And it's not
        portable in practice (although it will work on a few widely used
        systems).

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

        • Juha Nieminen

          #5
          Re: 2D-array of pointers, set all to null?

          James Kanze wrote:
          You can't set pointers to null with memset. And it's not
          portable in practice (although it will work on a few widely used
          systems).
          Could you mention, as an example, a system for which a C++ compiler is
          available and where a pointer cannot be nulled with memset?

          Comment

          • Gennaro Prota

            #6
            Re: 2D-array of pointers, set all to null?

            James Kanze wrote:
            On Jul 12, 12:53 pm, Juha Nieminen <nos...@thanks. invalidwrote:
            >WP wrote:
            >>Hello, is there something neat in the c++ standard library
            >>that can help me set all elements of a two-dimensional array
            >>of pointers to NULL? Like fill_n(), maybe? Right now, I'm
            >>using two for loops (one nested) and wanted to know what
            >>alternative s there are. My compiler ships with a preliminary
            >>TR1-implementation.
            >>Another thing I need to do is to call delete on the pointers
            >>in the array at some other point in the program, am I stuck
            >>with the two for loops there?
            >
            >Well, you could do it with a single memset() call, but I'm not
            >absolutely sure how portable that is (although I believe it's
            >portable *in practice*).
            >
            You can't set pointers to null with memset. And it's not
            portable in practice (although it will work on a few widely used
            systems).
            Right :-) However using an empty initializer list in the OP case
            should be fine, as the pointer elements get zero-initialized. This
            wasn't clearly specified but has always been the intent:

            <http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.htm l#277>

            So, unless your compiler vendor fails to know the intent... :-)

            --
            Gennaro Prota | <https://sourceforge.net/projects/breeze/>
            Do you need expertise in C++? I'm available.

            Comment

            • James Kanze

              #7
              Re: 2D-array of pointers, set all to null?

              On Jul 12, 10:10 pm, Juha Nieminen <nos...@thanks. invalidwrote:
              James Kanze wrote:
              You can't set pointers to null with memset. And it's not
              portable in practice (although it will work on a few widely
              used systems).
              Could you mention, as an example, a system for which a C++
              compiler is available and where a pointer cannot be nulled
              with memset?
              I think you've got that a little backwards. I don't know every
              system around, and neither do you. So on what basic do you say
              that it will work in practice? On a guarantee in the standard?
              On some set of "reasonable assumptions" about what an
              implementation can or might do? Or on what?

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

              • Juha Nieminen

                #8
                Re: 2D-array of pointers, set all to null?

                James Kanze wrote:
                I think you've got that a little backwards. I don't know every
                system around, and neither do you. So on what basic do you say
                that it will work in practice? On a guarantee in the standard?
                On some set of "reasonable assumptions" about what an
                implementation can or might do? Or on what?
                The standard doesn't guarantee a lot of things, yet it may still be
                reasonable to expect them anyways. For example, the standard doesn't
                guarantee that argv[0] actually contains something. However, it's still
                reasonable, in *practical* systems, to expect something to be there.

                Comment

                • James Kanze

                  #9
                  Re: 2D-array of pointers, set all to null?

                  On Jul 14, 4:42 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                  James Kanze wrote:
                  I think you've got that a little backwards. I don't know
                  every system around, and neither do you. So on what basic
                  do you say that it will work in practice? On a guarantee in
                  the standard? On some set of "reasonable assumptions" about
                  what an implementation can or might do? Or on what?
                  The standard doesn't guarantee a lot of things, yet it may
                  still be reasonable to expect them anyways. For example, the
                  standard doesn't guarantee that argv[0] actually contains
                  something. However, it's still reasonable, in *practical*
                  systems, to expect something to be there.
                  The standard actually does guarantee "something" in argv[0],
                  just not what you might want. And of course, if you want to use
                  it in portable code, you have to jump through some hoops, not
                  because of the standard, but in this case, because almost no
                  implementation is really conform.

                  In fact, what you can count on in argv[0] is what you've gotten,
                  historically. And historically, memset didn't work for nulling
                  pointers, so you can't argue historical tradition for it.

                  My point is: if you want some (more or less) guarantee beyond
                  what the standard gives, you need to base it on some reason.
                  The fact that it works on a couple of widespread implementations
                  is not sufficient. Given the number of implementations I've
                  seen and heard of, if I'd never heard of an implementation where
                  it didn't work, I'd perhaps consider it (although I'm not sure);
                  given that I've actually heard of implementations where it
                  didn't work, obviously, you can't use that reason, regardless of
                  whether it is acceptable. (An example of where I would accept
                  that reason:

                  std::vector< char v ;
                  int ch = std::cin.get() ;
                  while ( ch != EOF && ch != '\n' ) {
                  v.push_back( ch ) ;
                  }

                  Implementation defined, according to the standard, and the C
                  standard even explicitly mentions the possibility of an
                  implementation defined signal. However, given that no
                  implementation has, to my knowledge, made it fail *AND* given
                  that this is just a rewrite of the basic way K&R said to do
                  character input, back in the first edition of the C book, and
                  that the idiom is enormously widespread, among extremely
                  competent people, I think it's probably safe to say that no
                  implementation would dare break it. But there are a lot of
                  conditions involved.

                  You could raise the argument that using memset to set pointers
                  to null is widespread, and thus, implementations won't dare to
                  break it. Except that they have in the past, so apparently, it
                  wasn't widespread enough. And it's a very valid implementation
                  technique on some architectures. (Come to think of it, I rather
                  doubt that it would work on a Unisys MCP. There's a tag bit in
                  their words, and I sort of think that it has to be 1 if the word
                  is a pointer.)

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

                  • James Kanze

                    #10
                    Re: 2D-array of pointers, set all to null?

                    On Jul 14, 3:12 pm, Joe Greer <jgr...@doublet ake.comwrote:
                    WP <inva...@invali d.invalidwrote in news:6drfb6F41p mqU1
                    @mid.individual .net:
                    Hello, is there something neat in the c++ standard library
                    that can help me set all elements of a two-dimensional array
                    of pointers to NULL? Like fill_n(), maybe? Right now, I'm
                    using two for loops (one nested) and wanted to know what
                    alternatives there are. My compiler ships with a preliminary
                    TR1-implementation.
                    Another thing I need to do is to call delete on the pointers
                    in the array at some other point in the program, am I stuck
                    with the two for loops there?
                    The standard certainly implies that you can treat:
                    char * arr[X][Y]
                    as
                    char * arr[X*Y]
                    Where? I know that when the original C standard was being
                    drafted, great care was taken to allow "fat" bounds checking
                    pointers, and that every time there was a doubt, the C committee
                    has clarified that they meant what they said. Your suggestion
                    is undefined behavior. (In practice, of course, I can't imagine
                    an effective implementation where it didn't work.)

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

                    • Juha Nieminen

                      #11
                      Re: 2D-array of pointers, set all to null?

                      James Kanze wrote:
                      The standard actually does guarantee "something" in argv[0],
                      just not what you might want.
                      No. The standard actually allows argc to be 0, in which case argv[0]
                      will be a null pointer (as per the standard).
                      Even if argc 0, the standard still allows argv[0] to be an empty string.
                      But how many CLI programs have you seen that actually check that
                      argc>0 and that argv[0] actually contains something else than an empty
                      string before referring to argv[0] (eg. to print the command-line
                      syntax)? :P

                      (Ok, I have to admit I got this information from the freely-available
                      C++ standard draft. I would be surprised if the official standard said
                      something different, though.)

                      Comment

                      • Joe Greer

                        #12
                        Re: 2D-array of pointers, set all to null?

                        James Kanze <james.kanze@gm ail.comwrote in news:ece30ee7-d337-4ed0-8293-
                        76afc35e8a11@l4 2g2000hsc.googl egroups.com:
                        >
                        Where? I know that when the original C standard was being
                        drafted, great care was taken to allow "fat" bounds checking
                        pointers, and that every time there was a doubt, the C committee
                        has clarified that they meant what they said. Your suggestion
                        is undefined behavior. (In practice, of course, I can't imagine
                        an effective implementation where it didn't work.)
                        >
                        Well, all the arithmetic mumbo jumbo in dcl.Array/6,7,8 implies it to me.
                        Note, I didn't say it says that, just implies it. Having read it again
                        with my lawyer hat on and turning off any assumptions, it does allow for
                        padding between arrays. So, my ill thought out example of how to use
                        std::fill() causes undefined behavior, much like the memset() example else
                        thread. I suspect it would actually work with most implementations though.

                        joe

                        Comment

                        • James Kanze

                          #13
                          Re: 2D-array of pointers, set all to null?

                          On Jul 14, 7:36 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                          James Kanze wrote:
                          The standard actually does guarantee "something" in argv[0],
                          just not what you might want.
                          No. The standard actually allows argc to be 0, in which case argv[0]
                          will be a null pointer (as per the standard).
                          Which is something: you can test for it.
                          Even if argc 0, the standard still allows argv[0] to be an empty string.
                          But how many CLI programs have you seen that actually check that
                          argc>0 and that argv[0] actually contains something else than an empty
                          string before referring to argv[0] (eg. to print the command-line
                          syntax)? :P
                          Good question. (The first question, obviously, is how many
                          access argv[0] at all? The Unix tradition says that error
                          messages should start with the name of the program, but I find a
                          lot of programs don't do this, or simply hardware the name in.)
                          Posix allows the invoking process to specify the arg list, and
                          specifies "The argument arg0 should point to a filename that is
                          associated with the process being started by one of the exec
                          functions." It doesn't, however, say what happens if this isn't
                          the case (the call to exec fails?), and of course, "associated
                          with the process being started" is even vaguer than the wording
                          in the C++ standard (``argv[0] shall be the pointer to the
                          initial character of a NTMBS that represents the name used to
                          invoke the program or ""'').

                          (A quick check shows that both Solaris and Linux pass a null
                          pointer for argv[0], and the invoked process has argc == 0 and
                          argv[0] == NULL. Arguably, they should return with an EINVAL
                          error, given the text I just quoted.)

                          FWIW: the function I usually use for parsing the argument list
                          does an `` assert( argc 0 ) ''. So I do test it. (And you
                          can argue that under Unix, at least, a program with argc == 0
                          has not been correctly started, and should just abort.)

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

                          Working...