Dynamic allocate string array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • huohaodian@gmail.com

    Dynamic allocate string array

    Hi,

    I have a char * mFileList[];

    how can i dynamically allocate it to save some strings?

    Thanks in advance
  • huohaodian@gmail.com

    #2
    Re: Dynamic allocate string array

    On Feb 28, 2:53 pm, huohaod...@gmai l.com wrote:
    Hi,
    >
    I have a char * mFileList[];
    >
    how can i dynamically allocate it to save some strings? I'd like to allocate it to and array of POINTERS to char strings, some thing like char *mFileList[1000]
    >
    Thanks in advance

    Comment

    • Victor Bazarov

      #3
      Re: Dynamic allocate string array

      huohaodian@gmai l.com wrote:
      I have a char * mFileList[];
      >
      how can i dynamically allocate it to save some strings?
      std::vector<std ::stringmFileLi st;

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • huohaodian@gmail.com

        #4
        Re: Dynamic allocate string array

        On Feb 28, 3:12 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        huohaod...@gmai l.com wrote:
        I have a char * mFileList[];
        >
        how can i dynamically allocate it to save some strings?
        >
        std::vector<std ::stringmFileLi st;
        >
        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask
        I tried char * mFileList[] = new char[index]; but got a syntax error.

        Comment

        • Victor Bazarov

          #5
          Re: Dynamic allocate string array

          huohaodian@gmai l.com wrote:
          [..]
          I tried char * mFileList[] = new char[index]; but got a syntax error.
          char **mFileList = new char*[size];

          But let me reiterate, do NOT presume managing dynamic memory is
          easy or recommended. Try to stick to standard containers before
          you learn enough. Do

          std::vector<std ::stringmFileLi st(size);

          It's basically all you need, trust me.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • huohaodian@gmail.com

            #6
            Re: Dynamic allocate string array

            On Feb 28, 3:46 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            huohaod...@gmai l.com wrote:
            [..]
            I tried char * mFileList[] = new char[index]; but got a syntax error.
            >
            char **mFileList = new char*[size];
            >
            But let me reiterate, do NOT presume managing dynamic memory is
            easy or recommended. Try to stick to standard containers before
            you learn enough. Do
            >
            std::vector<std ::stringmFileLi st(size);
            >
            It's basically all you need, trust me.
            >
            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask
            Thanks a lot for the help

            Comment

            • Default User

              #7
              Re: Dynamic allocate string array

              huohaodian@gmai l.com wrote:
              Hi,
              >
              I have a char * mFileList[];
              This is an invalid declaration. You must either have an initializer
              list or give that array a dimension.
              how can i dynamically allocate it to save some strings?
              Do what Victor said. Use the tools that have been developed to aid the
              programmer.




              Brian

              Comment

              • Jim Langston

                #8
                Re: Dynamic allocate string array

                huohaodian@gmai l.com wrote:
                Hi,
                >
                I have a char * mFileList[];
                >
                how can i dynamically allocate it to save some strings?
                >
                Thanks in advance
                If possible, try std::vector<std ::stringmFileLi st;
                If not possible, try std::vector<cha r*mFileList;

                The std::vector should be as effective as anything manaul, such as new
                char*[count] and with a known value you can do:

                std::vector<cha r*mFileList( count );
                Which will create a dynamic array of count elements of char*.

                Still, std::vector<std ::stringis much prefered, but it really depeneds on
                your code.


                --
                Jim Langston
                tazmaster@rocke tmail.com


                Comment

                Working...