run-time sized array

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

    run-time sized array

    Hi all,

    I have an array that I don't know the size requirement until run-time.
    The way I handle this is:

    int* piMyData;

    piMyData = (int*)LocalAllo c(LPTR, sizeof(int)*iDa taCount); //MS-specific


    I also have a 2D char array that I don't know the size requirement until
    run-time. Is there a similar method for handling this?
    --

    Best wishes,
    Allen

    No SPAM in my email !!




  • Alf P. Steinbach

    #2
    Re: run-time sized array

    On Fri, 22 Aug 2003 23:31:58 GMT, "Allen" <allen-terri-ng@att.SPAM.net > wrote:
    [color=blue]
    >Hi all,
    >
    > I have an array that I don't know the size requirement until run-time.
    >The way I handle this is:
    >
    >int* piMyData;
    >
    >piMyData = (int*)LocalAllo c(LPTR, sizeof(int)*iDa taCount); //MS-specific[/color]


    std::vector<int > v( dataCount );

    v[someIndex] = someValue;

    [color=blue]
    >I also have a 2D char array that I don't know the size requirement until
    >run-time. Is there a similar method for handling this?[/color]

    Use a vector of vectors.

    Comment

    • Greg P.

      #3
      Re: run-time sized array

      "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
      news:OJx1b.1096 48$3o3.7708325@ bgtnsc05-news.ops.worldn et.att.net...
      | I have an array that I don't know the size requirement until run-time.
      | The way I handle this is:
      |
      | int* piMyData;
      |
      | piMyData = (int*)LocalAllo c(LPTR, sizeof(int)*iDa taCount);
      //MS-specific

      Remember that you are posting to a "C++" newsgroup, not "C":
      piMyData = static_cast<int *>(LocalAlloc(L PTR, sizeof(int)*iDa taCount));

      | I also have a 2D char array that I don't know the size requirement
      until
      | run-time. Is there a similar method for handling this?

      Do you mean similar in terms of using native win32 (non standard) methods?
      That is off topic here. There is a simple way that many novices use to
      figure out the total size of any array (though it is frowned upon):

      size_t size = sizeof(array[0][0]) * sizeof(array);


      Comment

      • Allen

        #4
        Re: run-time sized array


        "Greg P." <no@spam.sam> wrote in message
        news:o1y1b.3347 $Ej6.699@newsre ad4.news.pas.ea rthlink.net...[color=blue]
        > "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
        > news:OJx1b.1096 48$3o3.7708325@ bgtnsc05-news.ops.worldn et.att.net...
        > | I have an array that I don't know the size requirement until[/color]
        run-time.[color=blue]
        > | The way I handle this is:
        > |
        > | int* piMyData;
        > |
        > | piMyData = (int*)LocalAllo c(LPTR, sizeof(int)*iDa taCount);
        > //MS-specific
        >
        > Remember that you are posting to a "C++" newsgroup, not "C":
        > piMyData = static_cast<int *>(LocalAlloc(L PTR, sizeof(int)*iDa taCount));
        >
        > | I also have a 2D char array that I don't know the size requirement
        > until
        > | run-time. Is there a similar method for handling this?
        >
        > Do you mean similar in terms of using native win32 (non standard) methods?
        > That is off topic here. There is a simple way that many novices use to
        > figure out the total size of any array (though it is frowned upon):
        >
        > size_t size = sizeof(array[0][0]) * sizeof(array);[/color]

        Hi Greg,

        No, I'm not trying to determine the size. Also, I'm using a very old
        compiler that doesn't have vectors and besides, I don't have any experience
        using the STL.

        I'm looking for a method similar to the first one I gave to create
        storage for a 2D array that I don't know the size of until run-time (at
        which time, I do).
        In the first example, I get iDataCount and create an array at piMyData.
        Then, I can:

        piMyData[i] = iSomeInt;

        I want to get iXCount and iYCount and create a 2D array that I can
        access:

        pszMyData[x][y] = "a";
        --

        Best wishes,
        Allen

        No SPAM in my email !!




        Comment

        • Jon Bell

          #5
          Re: run-time sized array

          In article <gty1b.109704$3 o3.7712266@bgtn sc05-news.ops.worldn et.att.net>,
          Allen <allen-terri-ng@att.SPAM.net > wrote:[color=blue]
          >
          > I want to get iXCount and iYCount and create a 2D array that I can
          >access:
          >
          >pszMyData[x][y] = "a";[/color]

          If you don't want to use a vector of vectors, then you'll have to use
          dynamic memory allocation with 'new'. This is covered in section 16.15 of
          the FAQ at <http://www.parashift.c om/c++-faq-lite/>.

          --
          Jon Bell <jtbellap8@pres by.edu> Presbyterian College
          Dept. of Physics and Computer Science Clinton, South Carolina USA

          Comment

          • John Harrison

            #6
            Re: run-time sized array

            > Hi Greg,[color=blue]
            >
            > No, I'm not trying to determine the size. Also, I'm using a very old
            > compiler that doesn't have vectors and besides, I don't have any[/color]
            experience[color=blue]
            > using the STL.
            >
            > I'm looking for a method similar to the first one I gave to create
            > storage for a 2D array that I don't know the size of until run-time (at
            > which time, I do).
            > In the first example, I get iDataCount and create an array at[/color]
            piMyData.[color=blue]
            > Then, I can:
            >
            > piMyData[i] = iSomeInt;
            >
            > I want to get iXCount and iYCount and create a 2D array that I can
            > access:
            >
            > pszMyData[x][y] = "a";
            > --
            >[/color]

            This is in the FAQ.



            question 16.15. You should be able to translate the use of new to
            LocalAlloc, or better still drop LocalAlloc and use new instead. Every C++
            compiler ever invented must have new, surely.

            john



            ---
            Outgoing mail is certified Virus Free.
            Checked by AVG anti-virus system (http://www.grisoft.com).
            Version: 6.0.512 / Virus Database: 309 - Release Date: 19/08/2003


            Comment

            • J. Campbell

              #7
              Re: run-time sized array

              "Allen" <allen-terri-ng@att.SPAM.net > wrote in message news:<OJx1b.109 648$3o3.7708325 @bgtnsc05-news.ops.worldn et.att.net>...[color=blue]
              > Hi all,
              >
              > I have an array that I don't know the size requirement until run-time.
              > The way I handle this is:
              >
              > int* piMyData;
              >
              > piMyData = (int*)LocalAllo c(LPTR, sizeof(int)*iDa taCount); //MS-specific
              >[/color]


              Why not use:

              {
              int* piMyData;
              int size_piMyData = my_array_has_th is_many_element s;

              Comment

              • Kevin Goodsell

                #8
                Re: run-time sized array

                Allen wrote:
                [color=blue]
                >
                > No, I'm not trying to determine the size. Also, I'm using a very old
                > compiler that doesn't have vectors[/color]

                Sounds like a good time to get a new compiler.
                [color=blue]
                > and besides, I don't have any experience
                > using the STL.[/color]

                Sounds like a good time to learn.

                Ask in a C++ group, get a C++ answer. Use vector. It's infinitely
                superior to anything you are likely to come up with.

                -Kevin
                --
                My email address is valid, but changes periodically.
                To contact me please use the address from a recent posting.

                Comment

                Working...