run-time sized array

Discussion in 'C++' started by Allen, Aug 23, 2003.

  1. Allen

    Allen Guest

    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*)LocalAlloc(LPTR, sizeof(int)*iDataCount); //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 !!
    Allen, Aug 23, 2003
    #1
    1. Advertising

  2. On Fri, 22 Aug 2003 23:31:58 GMT, "Allen" <> wrote:

    >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*)LocalAlloc(LPTR, sizeof(int)*iDataCount); //MS-specific



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

    v[someIndex] = someValue;


    >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?


    Use a vector of vectors.
    Alf P. Steinbach, Aug 23, 2003
    #2
    1. Advertising

  3. Allen

    Greg P. Guest

    "Allen" <> wrote in message
    news:OJx1b.109648$...
    | 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*)LocalAlloc(LPTR, sizeof(int)*iDataCount);
    //MS-specific

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

    | 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);
    Greg P., Aug 23, 2003
    #3
  4. Allen

    Allen Guest

    "Greg P." <> wrote in message
    news:eek:1y1b.3347$...
    > "Allen" <> wrote in message
    > news:OJx1b.109648$...
    > | 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*)LocalAlloc(LPTR, sizeof(int)*iDataCount);
    > //MS-specific
    >
    > Remember that you are posting to a "C++" newsgroup, not "C":
    > piMyData = static_cast<int*>(LocalAlloc(LPTR, sizeof(int)*iDataCount));
    >
    > | 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);


    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 = 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 !!
    Allen, Aug 23, 2003
    #4
  5. > 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 = iSomeInt;
    >
    > I want to get iXCount and iYCount and create a 2D array that I can
    > access:
    >
    > pszMyData[x][y] = "a";
    > --
    >


    This is in the FAQ.

    http://www.parashift.com/c -faq-lite/freestore-mgmt.html

    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
    John Harrison, Aug 23, 2003
    #5
  6. Allen

    Jon Bell Guest

    In article <gty1b.109704$>,
    Allen <> wrote:
    >
    > I want to get iXCount and iYCount and create a 2D array that I can
    >access:
    >
    >pszMyData[x][y] = "a";


    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.com/c++-faq-lite/>.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Aug 23, 2003
    #6
  7. Allen wrote:

    >
    > No, I'm not trying to determine the size. Also, I'm using a very old
    > compiler that doesn't have vectors


    Sounds like a good time to get a new compiler.

    > and besides, I don't have any experience
    > using the STL.


    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.
    Kevin Goodsell, Aug 23, 2003
    #7
  8. Allen

    J. Campbell Guest

    "Allen" <> wrote in message news:<OJx1b.109648$>...
    > 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*)LocalAlloc(LPTR, sizeof(int)*iDataCount); //MS-specific
    >



    Why not use:

    {
    int* piMyData;
    int size_piMyData = my_array_has_this_many_elements;
    J. Campbell, Aug 23, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thechaosengine

    Oddly sized sized password textbox

    thechaosengine, Sep 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    569
    David Hearn
    Sep 15, 2005
  2. Daniel T.

    Dynamic sized array?

    Daniel T., Oct 17, 2006, in forum: C++
    Replies:
    4
    Views:
    312
    Sumit Rajan
    Oct 17, 2006
  3. Pierre Yves
    Replies:
    2
    Views:
    470
    Pierre Yves
    Jan 10, 2008
  4. Leo Havmøller

    Sized array w/initializer

    Leo Havmøller, Apr 21, 2010, in forum: C Programming
    Replies:
    4
    Views:
    356
    Gil Johnson
    Apr 22, 2010
  5. MartinBroadhurst

    List as a dynamic array of increasing-sized arrays

    MartinBroadhurst, Oct 21, 2010, in forum: C Programming
    Replies:
    13
    Views:
    1,050
    MartinBroadhurst
    Nov 3, 2010
Loading...

Share This Page