Array of pointers + memory Location

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

    Array of pointers + memory Location

    I have more of a conceptual question now. Let us say I do this:-

    char *str[10]; --create an array of pointers
    str[1]= "John";

    I thought this would automatically put John at some memory space and
    point str[1] to it.
    My colleague argues that in flat bed memory this should not be done. A
    new should be used to allocate the memory and then point to it. Of
    course he didnt do the best job explaining, hence my question to you
    guys, if you can shed some more light on this.

    Thanks in anticipation
  • Nick Keighley

    #2
    Re: Array of pointers + memory Location

    On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
    I have more of a conceptual question now. Let us say I do this:-
    >
    char *str[10]; --create an array of pointers
    str[1]= "John";
    >
    I thought this would automatically put John at some memory space and
    point str[1] to it.
    yes
    My colleague argues that in flat bed memory
    in *what*?
    this should not be done.
    why not?
    A
    new should be used to allocate the memory and then point to it.
    why? Why waste the cost of a new (and they are quite expensive)
    when it isn't necessary?

    Of
    course he didnt do the best job explaining, hence my question to you
    guys, if you can shed some more light on this.
    I've little sympathy with people who give apparently motiveless
    technical advice. If he knows the reason why doesn't he tell you?


    --
    Nick Keighley


    Comment

    • Victor Bazarov

      #3
      Re: Array of pointers + memory Location

      Nick Keighley wrote:
      On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
      >
      >I have more of a conceptual question now. Let us say I do this:-
      >>
      >char *str[10]; --create an array of pointers
      >str[1]= "John";
      >>
      >I thought this would automatically put John at some memory space and
      >point str[1] to it.
      >
      yes
      Except that it shouldn't compile since "John" is an array of const
      char and 'str[1]' is a pointer to non-const char. There is no
      conversion without a cast. The intent probably was to declare 'str'
      as 'char const* str[10];'
      [..]
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • Slain

        #4
        Re: Array of pointers + memory Location

        On Mar 5, 9:57 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        Nick Keighley wrote:
        On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
        >
        I have more of a conceptual question now. Let us say I do this:-
        >
        char *str[10]; --create an array of pointers
        str[1]= "John";
        >
        I thought this would automatically put John at some memory space and
        point str[1] to it.
        >
        yes
        >
        Except that it shouldn't compile since "John" is an array of const
        char and 'str[1]' is a pointer to non-const char. There is no
        conversion without a cast. The intent probably was to declare 'str'
        as 'char const* str[10];'
        >
        [..]
        >
        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask
        It does compile as long as it is inside main or any function. So may
        be it does a type conversion.

        Comment

        • Slain

          #5
          Re: Array of pointers + memory Location

          On Mar 5, 9:57 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          Nick Keighley wrote:
          On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
          >
          I have more of a conceptual question now. Let us say I do this:-
          >
          char *str[10]; --create an array of pointers
          str[1]= "John";
          >
          I thought this would automatically put John at some memory space and
          point str[1] to it.
          >
          yes
          >
          Except that it shouldn't compile since "John" is an array of const
          char and 'str[1]' is a pointer to non-const char. There is no
          conversion without a cast. The intent probably was to declare 'str'
          as 'char const* str[10];'
          >
          [..]
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask
          It does compile as long as it is inside main or any function. So may
          be it does a type conversion.

          Comment

          • Jeff Schwab

            #6
            Re: Array of pointers + memory Location

            Victor Bazarov wrote:
            Nick Keighley wrote:
            >On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
            >>
            >>I have more of a conceptual question now. Let us say I do this:-
            >>>
            >>char *str[10]; --create an array of pointers
            >>str[1]= "John";
            >>>
            >>I thought this would automatically put John at some memory space and
            >>point str[1] to it.
            >yes
            Agreed, "John" now lives in static storage.
            Except that it shouldn't compile since "John" is an array of const
            char and 'str[1]' is a pointer to non-const char. There is no
            conversion without a cast.
            There is such a conversion, although it is deprecated. From
            [conv.array]: "This conversion is considered only when there is an
            explicit appropriate pointer target type, and not when there is a
            general need to convert from an lvalue to an rvalue."
            The intent probably was to declare 'str'
            as 'char const* str[10];'
            I believe any attempt to modify the string is undefined behavior, so (as
            you suggest) the OP is surely better off with char const*. Even better,
            IMO, would be replacing the raw array of raw pointers with a vector (or
            tr1 array) of std::string.

            Comment

            • Micah Cowan

              #7
              Re: Array of pointers + memory Location

              "Victor Bazarov" <v.Abazarov@com Acast.netwrites :
              Nick Keighley wrote:
              >On 5 Mar, 14:38, Slain <Slai...@gmail. comwrote:
              >>
              >>I have more of a conceptual question now. Let us say I do this:-
              >>>
              >>char *str[10]; --create an array of pointers
              >>str[1]= "John";
              >>>
              >>I thought this would automatically put John at some memory space and
              >>point str[1] to it.
              >>
              >yes
              >
              Except that it shouldn't compile since "John" is an array of const
              char and 'str[1]' is a pointer to non-const char. There is no
              conversion without a cast. The intent probably was to declare 'str'
              as 'char const* str[10];'
              No cast is required, for compatibility with C (it's a, deprecated,
              standard conversion, and only applies to string literals).

              No diagnostic is required, but decent implementations will issue one
              anyway.

              --
              Micah J. Cowan
              Programmer, musician, typesetting enthusiast, gamer...

              Comment

              • Pete Becker

                #8
                Re: Array of pointers + memory Location

                On 2008-03-06 07:07:36 -0500, utab <umut.tabak@gma il.comsaid:
                char * p1=0,p2=0; // **
                This is equivalent to

                char *p1 = 0;
                char p2 = 0;

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

                Comment

                • utab

                  #9
                  Re: Array of pointers + memory Location

                  On Mar 6, 1:59 pm, Pete Becker <p...@versatile coding.comwrote :
                  On 2008-03-06 07:07:36 -0500, utab <umut.ta...@gma il.comsaid:
                  >
                  char * p1=0,p2=0; // **
                  >
                  This is equivalent to
                  >
                  char *p1 = 0;
                  char p2 = 0;
                  >
                  --
                  Pete
                  Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                  Standard C++ Library Extensions: a Tutorial and Reference
                  (www.petebecker.com/tr1book)
                  Ah yes, I missed that. Thanks

                  Comment

                  • Triple-DES

                    #10
                    Re: Array of pointers + memory Location

                    On 6 Mar, 13:07, utab <umut.ta...@gma il.comwrote:

                    [snip]
                    >
                    And the last question is that I have an array of pointers to char, how
                    can I get the start address of each char array that are pointed by the
                    pointers in the array? This is a bit related to my 1st question where
                    I thought I can get it with &(str[0][0]), but apparently there is sth
                    I am missing.
                    If you have an array of pointers to char, then the start address of
                    each char array is the pointer itself (at least unformally). So
                    &(str[0][0]) == str[0]

                    DP

                    Comment

                    Working...