How to make two-dimensional array put 'new' to use in C++?

Discussion in 'C++' started by chajs226@gmail.com, Jan 6, 2007.

  1. Guest

    Hi everyone.. I'm doing programing a maze problem.
    I want to make a maze(array[5][7]) that use two-dimensional array...

    but I don't know this...

    {
    int *a[7];
    a[7] = new int [5];
    }

    this is result in segmentation error


    help me... Thanks.
    , Jan 6, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi everyone.. I'm doing programing a maze problem.
    > I want to make a maze(array[5][7]) that use two-dimensional array...
    >
    > but I don't know this...
    >
    > {
    > int *a[7];
    > a[7] = new int [5];
    > }
    >
    > this is result in segmentation error


    See FAQ.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 6, 2007
    #2
    1. Advertising

  3. fungus Guest

    wrote:
    > {
    > int *a[7];
    > a[7] = new int [5];
    > }
    >
    > this is result in segmentation error
    >


    When you create a[7] then elements are
    in the range 0 to 6. "7" is past the end
    of the array.

    --
    <\___/>
    / O O \
    \_____/ FTB. For email, remove my socks.


    We¡¯re judging how a candidate will handle a nuclear
    crisis by how well his staff creates campaign ads.
    It¡¯s a completely nonsensical process.
    fungus, Jan 6, 2007
    #3
  4. Sean Fritz Guest

    fungus wrote:

    > wrote:
    >> {
    >> int *a[7];
    >> a[7] = new int [5];
    >> }
    >>
    >> this is result in segmentation error
    >>

    >
    > When you create a[7] then elements are
    > in the range 0 to 6. "7" is past the end
    > of the array.
    >


    Definitely have to be careful with array ranges in C++, most compilers won't
    give you an error if you try to go outside the index.
    Sean Fritz, Jan 6, 2007
    #4
  5. Salt_Peter Guest

    wrote:
    > Hi everyone.. I'm doing programing a maze problem.
    > I want to make a maze(array[5][7]) that use two-dimensional array...
    >
    > but I don't know this...
    >
    > {
    > int *a[7];
    > a[7] = new int [5];
    > }
    >
    > this is result in segmentation error
    >
    >
    > help me... Thanks.


    there are 7 numbers from 0 to 6 and 7 isn't one of them.
    Salt_Peter, Jan 6, 2007
    #5
  6. David Harmon Guest

    On 5 Jan 2007 21:06:10 -0800 in comp.lang.c++, ""
    <> wrote,
    >Hi everyone.. I'm doing programing a maze problem.
    >I want to make a maze(array[5][7]) that use two-dimensional array...


    Avoid 'new' and bare naked arrays in application-level code.

    std::vector< std::vector<int> > array(5, std::vector<int>(7));
    David Harmon, Jan 6, 2007
    #6
    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. Alf P. Steinbach
    Replies:
    0
    Views:
    423
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,914
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    641
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    963
    Venkat
    Dec 5, 2003
  5. Kenneth Brody

    Two-dimensional array to array of arrays

    Kenneth Brody, Dec 21, 2006, in forum: C Programming
    Replies:
    2
    Views:
    497
    Simon Biber
    Dec 21, 2006
Loading...

Share This Page