How to create dynamic char array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    #1

    How to create dynamic char array?

    I am working on dynamic array which grows with input data size. Its size may grow from 30 to 30000000 character. I don't want to initialize an array of size 30000000. Like

    char* array = new char[30000001];

    How can i do it dynamically? Thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Don't use arrays use a vector!

    Anyway what you have given as what you don't want to do is the dynamic way to do things, your question is not clear.

    Comment

    • Man4ish
      New Member
      • Mar 2008
      • 151

      #3
      Thanks

      vector solve the pblm but i don't want to use STL.It will slow down the performance.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        It is very unlikely that a vector will slow down performance since under optimisation it is as efficient as a normal array.

        Comment

        • Man4ish
          New Member
          • Mar 2008
          • 151

          #5
          Thanks for reply. As per my project demand i am not supposed to use stl and string class. Below is an example of my need.


          #include <iostream>
          using namespace std;
          int main()
          {
          char str1[20] = "00000000000000 00000";
          char str2[10] = "000000000" ;

          // insert str2 into str1 at position 5 in str1
          return 0;
          }
          How this can be done? Since this needs size of str1 to be 30 which is now 20. How it can be increased. It is possible in string easily. But i want to use char*.

          Thanks again for your reply.

          Comment

          • hype261
            New Member
            • Apr 2010
            • 207

            #6
            Can you use the CString standard header for your project? It provides a lot of useful functions when working with old school strings.

            Here is a reference...

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              The way to do this in C is to use realloc each time you need to increase the size of the string. The copy implicit to realloc can be handy, but it may be more efficient to use malloc and an explicit copy if your string typically grows because you insert a substring into the middle of it. Likewise, it might be substantially more efficient to always request more memory than you need to reduce the number of allocation calls.

              However, C++ programs tend to be more elegant than brute-force C. <wink>

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                Man4ish,

                Is this a school project?

                If so, please take a cut at writing the answer, then ask specific questions about details. Anything more is way out of bounds from an academic integrity standpoint.

                Cheers!
                Oralloy

                Comment

                Working...