How to initialize a char*?

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

    #16
    Re: How to initialize a char*?

    "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
    news:c5hbdn$e0l $1@chessie.cirr .com...[color=blue]
    > Peter <ybi10@yahoo.co m> spoke thus:
    >[color=green]
    > > char* ptr;
    > > func(..., ptr);[/color]
    >
    > This is bad - ptr is uninitialized (not even NULL), so passing it to a
    > function is either pointless or an invitation to disaster, a mistake
    > in any case.[/color]


    Why?

    [color=blue][color=green]
    > > How to properly initialize a char*? I used to use[/color]
    >[color=green]
    > > char* ptr = "";[/color]
    >
    > Depends on what you're doing with it. You probably were told that
    > because it should be
    >
    > const char *ptr="";[/color]


    And what would be the use of that?


    [color=blue]
    > Why? Because "" is a constant empty string; trying to modify it
    > invites disaster. Therefore, pointers to such string literals (so
    > they're called) should be declared const; that way if you try to
    > modify what it points to the compiler can inform you of your mistake.[/color]


    The above is entirely, completely useless.






    Ioannis Vranos

    Comment

    • Ioannis Vranos

      #17
      Re: How to initialize a char*?

      "Julie" <julie@nospam.c om> wrote in message
      news:407C4EE4.2 C85958D@nospam. com...[color=blue]
      >
      > Presume that func is declared as:
      >
      > void func(int input, char * & dest);[/color]


      You can't do that!


      [color=blue]
      > Your statement may only be correct if func is declared as:
      >
      > void func(int input, char * dest);[/color]


      I do not think his statement is correct.






      Ioannis Vranos

      Comment

      • Ioannis Vranos

        #18
        Re: How to initialize a char*?

        "Julie" <julie@nospam.c om> wrote in message
        news:407C5EC1.D BCC116F@nospam. com...[color=blue]
        >
        > Here is another example that is perfectly legitimate:
        >
        > void func(char * const input, int word_number, char * & ptr_to_word);
        > // find word in input and store address in ptr[/color]


        I do not think it is. You can take references from pointers only in
        templates. Even in this case passing an unitialised char * would not have
        any bad effect by its own.


        [color=blue]
        > No allocations, no (thread-hostile???) static buffers, no overflow.
        >[color=green]
        > > It needs to be rewritten to use std::string.[/color]
        >
        > As I previously replied, char * is a perfectly legitimate and usable type.
        > Blanket statements such as "use std::string" serve no purpose.
        >
        > The answer to the OP's question is simple and concise:
        >
        > Initialize a char * w/ NULL.[/color]


        Nope. If he chooses to make the unneeded initialisation he should use 0, NOT
        NULL.






        Ioannis Vranos

        Comment

        • Julie

          #19
          Re: How to initialize a char*?

          Ioannis Vranos wrote:[color=blue]
          >
          > "Julie" <julie@nospam.c om> wrote in message
          > news:407C4EE4.2 C85958D@nospam. com...[color=green]
          > >
          > > Presume that func is declared as:
          > >
          > > void func(int input, char * & dest);[/color]
          >
          > You can't do that![/color]

          ???

          Sure you can -- a reference to a variable of type char *.
          [color=blue][color=green]
          > > Your statement may only be correct if func is declared as:
          > >
          > > void func(int input, char * dest);[/color]
          >
          > I do not think his statement is correct.[/color]

          I do not understand what your point is. To whom are you referring to by "his"?

          Comment

          • David Harmon

            #20
            Re: How to initialize a char*?

            On Tue, 13 Apr 2004 15:47:58 -0700 in comp.lang.c++, Julie
            <julie@nospam.c om> wrote,[color=blue]
            >Yes, my statement was directed at the 'evil' part -- there is nothing 'evil'
            >about it, it just has the potential to lead to undefined behavior.[/color]

            This issue is covered in Marshall Cline's C++ FAQ. See the topic
            "[6.14] What does the FAQ mean by "such-in-such is evil"?".


            Comment

            • Ioannis Vranos

              #21
              Re: How to initialize a char*?

              "Julie" <julie@nospam.c om> wrote in message
              news:407C758E.8 D10840@nospam.c om...[color=blue]
              > Ioannis Vranos wrote:[color=green]
              > >
              > > "Julie" <julie@nospam.c om> wrote in message
              > > news:407C4EE4.2 C85958D@nospam. com...[color=darkred]
              > > >
              > > > Presume that func is declared as:
              > > >
              > > > void func(int input, char * & dest);[/color]
              > >
              > > You can't do that![/color]
              >
              > ???
              >
              > Sure you can -- a reference to a variable of type char *.
              >[color=green][color=darkred]
              > > > Your statement may only be correct if func is declared as:
              > > >
              > > > void func(int input, char * dest);[/color]
              > >
              > > I do not think his statement is correct.[/color]
              >
              > I do not understand what your point is. To whom are you referring to by[/color]
              "his"?


              Can you provide a working example with a main() using such a function
              accepting char * &?






              Ioannis Vranos

              Comment

              • Ioannis Vranos

                #22
                Re: How to initialize a char*?

                "Matt Wharton" <no_matt_wharto n@agilent.com_s pam> wrote in message
                news:1081894795 .688751@cswreg. cos.agilent.com ...[color=blue]
                >
                > This is true, but I think it would be fair to argue that it is always good
                > practice to initialize variables to something (most likely NULL or 0 in[/color]
                this[color=blue]
                > case). No harm comes from doing this (always initializing), whereas there
                > are many situations where NOT initializing will shoot you in the foot.[/color]


                An example?

                To make long things short, initialization doesn't protect you from anything.
                The only thing that it could "protect you" in theory is in the call of
                delete/delete[] family to an ititialised-to-0 pointer. In reality however
                such a call occurs at the end of a process and you will have done more
                things previously by mistake on that 0 initialised pointer (e.g.
                dereferencing it) that would be equally disastrous/undefined.

                Initialisation does not protect you from any mistake.






                Ioannis Vranos

                Comment

                • Ioannis Vranos

                  #23
                  Re: How to initialize a char*?

                  "Julie" <julie@nospam.c om> wrote in message
                  news:407C6E1E.1 7403F38@nospam. com...[color=blue]
                  >
                  > Absolutely, variables, as a general rule, should be initialized to a
                  > standardized default value.[/color]

                  Not absolutely.






                  Ioannis Vranos

                  Comment

                  • Ioannis Vranos

                    #24
                    Re: How to initialize a char*?

                    "Kevin Goodsell" <usenet2.spamfr ee.fusion@never box.com> wrote in message
                    news:s_Xec.8580 $A_4.7680@newsr ead1.news.pas.e arthlink.net...[color=blue]
                    >
                    > Never use any variable before you've given it a meaningful value. Doing
                    > so gives undefined behavior, and could crash your program or worse.[/color]

                    You reminded me a humorous text that i had read a long ago which i just
                    searched in google. Check it:




                    Of course it has nothing to do with the subject, but this "never" of yours
                    reminded it to me. :-)


                    [color=blue]
                    > This uses a dangerous and deprecated language feature -- the implicit
                    > conversion of a string literal to a (non-const) char*. I'd suggest
                    > making your compiler warn about this if it provides such a warning.[/color]

                    Eh?

                    [color=blue]
                    > Initialize it with whatever you need. You're the only one who knows what
                    > purpose it serves, so we can't suggest what to use. But leaving it
                    > uninitialized is an error, and doesn't make sense.[/color]

                    It is not an error!






                    Ioannis Vranos

                    Comment

                    • Julie

                      #25
                      Re: How to initialize a char*?

                      Ioannis Vranos wrote:[color=blue]
                      >
                      > "Julie" <julie@nospam.c om> wrote in message
                      > news:407C5EC1.D BCC116F@nospam. com...[color=green]
                      > >
                      > > Here is another example that is perfectly legitimate:
                      > >
                      > > void func(char * const input, int word_number, char * & ptr_to_word);
                      > > // find word in input and store address in ptr[/color]
                      >
                      > I do not think it is. You can take references from pointers only in
                      > templates. Even in this case passing an unitialised char * would not have
                      > any bad effect by its own.[/color]

                      Reference to a pointer type is perfectly legal, and has nothing to do w/
                      templates.

                      If you feel that I'm wrong, please correct me by referencing the C++ standard.
                      [color=blue][color=green]
                      > > No allocations, no (thread-hostile???) static buffers, no overflow.
                      > >[color=darkred]
                      > > > It needs to be rewritten to use std::string.[/color]
                      > >
                      > > As I previously replied, char * is a perfectly legitimate and usable type.
                      > > Blanket statements such as "use std::string" serve no purpose.
                      > >
                      > > The answer to the OP's question is simple and concise:
                      > >
                      > > Initialize a char * w/ NULL.[/color]
                      >
                      > Nope. If he chooses to make the unneeded initialisation he should use 0, NOT
                      > NULL.[/color]

                      NULL is perfectly acceptable, and is standard.

                      Comment

                      • Julie

                        #26
                        Re: How to initialize a char*?

                        Ioannis Vranos wrote:[color=blue]
                        > Can you provide a working example with a main() using such a function
                        > accepting char * &?[/color]

                        How about you go ahead and try it, and learn for yourself.

                        Comment

                        • Julie

                          #27
                          Re: How to initialize a char*?

                          David Harmon wrote:[color=blue]
                          >
                          > On Tue, 13 Apr 2004 15:47:58 -0700 in comp.lang.c++, Julie
                          > <julie@nospam.c om> wrote,[color=green]
                          > >Yes, my statement was directed at the 'evil' part -- there is nothing 'evil'
                          > >about it, it just has the potential to lead to undefined behavior.[/color]
                          >
                          > This issue is covered in Marshall Cline's C++ FAQ. See the topic
                          > "[6.14] What does the FAQ mean by "such-in-such is evil"?".
                          > http://www.parashift.com/c++-faq-lite/[/color]

                          Thanks for pointing it out.

                          After reading, that section definitely comes off as infantile and
                          inappropriate. I'd much prefer a more precise (and less humanity-subjective)
                          term as 'evil'. But I didn't write that FAQ, nor have any plans to write one,
                          so MC is free to write what he wants. Regardless, that doesn't preclude me
                          from stating that 'evil' is a poor word choice when discussing programmatic
                          constructs and behaviors.

                          Comment

                          • Julie

                            #28
                            Re: How to initialize a char*?

                            Ioannis Vranos wrote:[color=blue]
                            >
                            > "Julie" <julie@nospam.c om> wrote in message
                            > news:407C6E1E.1 7403F38@nospam. com...[color=green]
                            > >
                            > > Absolutely, variables, as a general rule, should be initialized to a
                            > > standardized default value.[/color]
                            >
                            > Not absolutely.[/color]

                            Yes absolutely.

                            From now on, in order to be considered a productive participant, you may want
                            to include more than simple phrase answers and actually provide some insight
                            into the matter.

                            Comment

                            • Ioannis Vranos

                              #29
                              Re: How to initialize a char*?

                              "Julie" <julie@nospam.c om> wrote in message
                              news:407C7FDE.A EB6117@nospam.c om...[color=blue]
                              > Ioannis Vranos wrote:[color=green]
                              > > Can you provide a working example with a main() using such a function
                              > > accepting char * &?[/color]
                              >
                              > How about you go ahead and try it, and learn for yourself.[/color]


                              Why don't you try? Just provide an empty definition of a function getting
                              char *&), create a main() with a valid call to it and if it compiles post it
                              here (i am not kidding you will see).






                              Regards,

                              Ioannis Vranos

                              Comment

                              • Julie

                                #30
                                Re: How to initialize a char*?

                                Ioannis Vranos wrote:[color=blue]
                                >
                                > "Matt Wharton" <no_matt_wharto n@agilent.com_s pam> wrote in message
                                > news:1081894795 .688751@cswreg. cos.agilent.com ...[color=green]
                                > >
                                > > This is true, but I think it would be fair to argue that it is always good
                                > > practice to initialize variables to something (most likely NULL or 0 in[/color]
                                > this[color=green]
                                > > case). No harm comes from doing this (always initializing), whereas there
                                > > are many situations where NOT initializing will shoot you in the foot.[/color]
                                >
                                > An example?
                                >
                                > To make long things short, initialization doesn't protect you from anything.
                                > The only thing that it could "protect you" in theory is in the call of
                                > delete/delete[] family to an ititialised-to-0 pointer. In reality however
                                > such a call occurs at the end of a process and you will have done more
                                > things previously by mistake on that 0 initialised pointer (e.g.
                                > dereferencing it) that would be equally disastrous/undefined.
                                >
                                > Initialisation does not protect you from any mistake.[/color]

                                That is true.

                                Perhaps it would be better to term this as 'zero initialization' , after all

                                char * ptr = (char *)12345;

                                is initialized, but not much better than leaving it uninitialized.

                                Comment

                                Working...