CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 1999
    Posts
    79

    How to get any big array

    Hello:
    I have a array like:int MyArray[n][m];
    I can't know the value of 'n' and 'm'.They're
    must had run the program,get the two value.
    I want use point but there was a question.
    I need help!
    Welcome any advice!
    Thank you.


  2. #2
    Join Date
    Apr 1999
    Posts
    306

    Re: How to get any big array

    Try this:

    int **MyArray;

    Somewhere in the program:

    for(int i(0);i<n;i++)
    MyArray(i) = new int (m);

    Dont forget to delete your memory,
    ric


  3. #3
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: How to get any big array

    For a dynamic array at run time, try this :


    // to allocate (assume type int for array)
    int *pArray ;

    pArray = new int[x_array_size * y_array_size] ;

    // to access using indexes i for x and j for y
    value = pArray[i + j * y_array_size] ;

    // to delete array
    delete []pArray ;





    You will have to deal with making sure that the values of i,j do not go outside the array bounds that you allocate.

    HTH





    Roger Allen
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  4. #4
    Join Date
    May 1999
    Location
    United Kingdom
    Posts
    136

    Re: How to get any big array

    I suppose you've noticed the mistake? To get a m*n array do the following:


    int **MyArray;

    MyArray = new int*[m];

    for (int x = 0; x < m; i++)
    {
    MyArray[x] = new int[n];
    }

    int value_at_x_y = MyArray[x][y];




    Then, don't forget to delete everything:


    for (int x = 0; x < m; x++)
    {
    delete MyArray[x];
    }
    delete MyArray;




    Tada,
    Oliver.


  5. #5
    Join Date
    Apr 1999
    Posts
    306

    Re: How to get any big array

    OK, you are right, but I run my code on a Borland 3.1 compiler and it doesnot spit out any "Null Pointer
    Assignment" so I supposed that it works.
    Of course, we must alloc first for the pointers then for ints.

    Thanks


  6. #6
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: How to get any big array

    A bug!
    The line

    delete MyArray ;
    // should read
    delete []MyArray ;




    I thought I taught you better than that Oli!
    (ps I work with him)


    Roger Allen
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  7. #7
    Join Date
    Apr 1999
    Posts
    79

    Re: How to get any big array

    Hello:
    Thank you very much.
    I receive your message.Now My Array like this:
    MyArray[m][8];
    In fact the value of 'm',I can't get when
    the program start run.The value can get when
    open file message is selected.(I use VC++6.0 MFC)
    Welcome any advice!
    Thank you very much.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured