Is this code correct?

Discussion in 'C++' started by highli, Jul 30, 2004.

  1. highli

    highli Guest

    Array2D = new (int*)[M];

    for(int i = 0; i < M; ++i)
    Array2D = new int[N];


    for(int i = 0; i < M; ++i)
    delete [] Array2D[n];

    delete [] Array2D;


    What is meaning of "(int*)[M]"?

    Is it necessary to "delete [] Array2D;", or just "delete Array2D;"?

    Thanks in advance!
     
    highli, Jul 30, 2004
    #1
    1. Advertising

  2. highli wrote:
    > Array2D = new (int*)[M];


    Huh? Shouldn't this be

    Array2D = new int*[M];

    ?

    >
    > for(int i = 0; i < M; ++i)
    > Array2D = new int[N];
    >
    >
    > for(int i = 0; i < M; ++i)
    > delete [] Array2D[n];
    >
    > delete [] Array2D;


    This is apparently a fragment. As with every fragment, the judgment
    of its correctness depends highly on what it's a fragment of.

    >
    >
    > What is meaning of "(int*)[M]"?


    It's a syntax error. If you need to get the meaning, you should ask
    somebody who wrote it.

    >
    > Is it necessary to "delete [] Array2D;", or just "delete Array2D;"?


    Yes, considering that you remove the parentheses from the first 'new'
    expression.

    Victor
     
    Victor Bazarov, Jul 30, 2004
    #2
    1. Advertising

  3. highli wrote:
    > Array2D = new (int*)[M];
    >
    > for(int i = 0; i < M; ++i)
    > Array2D = new int[N];
    >
    >
    > for(int i = 0; i < M; ++i)
    > delete [] Array2D[n];
    >
    > delete [] Array2D;
    >


    This code is correct (although I would write it differenly)

    >
    > What is meaning of "(int*)[M]"?


    It's a type - an array of M pointers to int.

    >
    > Is it necessary to "delete [] Array2D;", or just "delete Array2D;"?


    It *is* neccessary.

    This is how I would do it though.

    http://tinyurl.com/65yv5
     
    Gianni Mariani, Jul 30, 2004
    #3
  4. highli

    Rolf Magnus Guest

    highli wrote:

    > Array2D = new (int*)[M];
    >
    > for(int i = 0; i < M; ++i)
    > Array2D = new int[N];
    >
    >
    > for(int i = 0; i < M; ++i)
    > delete [] Array2D[n];


    delete [] Array2D;

    > delete [] Array2D;
    >
    >
    > What is meaning of "(int*)[M]"?


    M-Element array of pointer to int.

    > Is it necessary to "delete [] Array2D;", or just "delete Array2D;"?


    Whatever you allocated with new[] has to be deallocated with delete[],
    so yes, you need delete[] here.
     
    Rolf Magnus, Jul 30, 2004
    #4
  5. Gianni Mariani wrote:
    > highli wrote:
    >
    >> Array2D = new (int*)[M];
    >>
    >> for(int i = 0; i < M; ++i)
    >> Array2D = new int[N];
    >>
    >>
    >> for(int i = 0; i < M; ++i)
    >> delete [] Array2D[n];
    >>
    >> delete [] Array2D;
    >>

    >
    > This code is correct (although I would write it differenly)
    >
    >>
    >> What is meaning of "(int*)[M]"?

    >
    >
    > It's a type - an array of M pointers to int.


    Does that mean that the parentheses are unnecessary? I would
    think that they are actually interfering with the type-id that
    needs to be there... It doesn't seem to work in VC++ v 7.1..

    >
    >>
    >> Is it necessary to "delete [] Array2D;", or just "delete Array2D;"?

    >
    >
    > It *is* neccessary.
     
    Victor Bazarov, Jul 30, 2004
    #5
  6. highli

    marbac Guest

    Hi,

    I got following Version from a Book (2D Array):

    int z=5, s=6;

    int** mat=new int* [z]; //> int **Array2D; Array2D = new int* [M];
    for (int i=0;i<z;i++) //>for(int i = 0; i < M; ++i)
    mat =new int ; //> Array2D = new int[N];

    mat[j]=12;

    for (int i=0;i<z;i++) //> for(int i = 0; i < M; ++i)
    delete [] mat;//> delete [] Array2D[n]; <- i!!!!

    delete [] mat; //> delete [] Array2D;

    highli wrote:
    > Array2D = new (int*)[M];


    Remove the brackets (), looks like casting.
    Make sure that Array2D is of type int**

    >
    > for(int i = 0; i < M; ++i)
    > Array2D = new int[N];
    >
    >
    > for(int i = 0; i < M; ++i)
    > delete [] Array2D[n];


    "delete [] Array2D[n];" Has to be "delete [] Array2D;"

    >
    > delete [] Array2D;
    >
    >
    > What is meaning of "(int*)[M]"?


    The meaning of "Array2D = new int* [M];" is that you allocate M Pointers
    of type int which are accessable by Array2D (**).

    >
    > Is it necessary to "delete [] Array2D;", or just "delete Array2D;"?
    >

    Yes it is necessary, because you allocated an array (field of objects)
    and not a single object.

    > Thanks in advance!
    >
    >
    >
    >



    regards marbac
     
    marbac, Jul 30, 2004
    #6
  7. Victor Bazarov wrote:
    > Gianni Mariani wrote:

    ...
    >>>
    >>> What is meaning of "(int*)[M]"?

    >>
    >>
    >>
    >> It's a type - an array of M pointers to int.

    >
    >
    > Does that mean that the parentheses are unnecessary? I would
    > think that they are actually interfering with the type-id that
    > needs to be there... It doesn't seem to work in VC++ v 7.1..


    I overlooked the "()" - it is wrong.

    I suspect the OP meant as you pointed out in the other post.

    new int*[M]

    It's interesting that GCC actually gives an interesting message.

    xxx.cpp: In function `int main()':
    xxx.cpp:7: error: array bound forbidden after parenthesized type-id
    xxx.cpp:7: note: try removing the parentheses around the type-id
     
    Gianni Mariani, Jul 30, 2004
    #7
  8. highli

    Rolf Magnus Guest

    Gianni Mariani wrote:

    > I suspect the OP meant as you pointed out in the other post.
    >
    > new int*[M]
    >
    > It's interesting that GCC actually gives an interesting message.


    I'd be surprised if an interesting message would not be interesting ;-)

    > xxx.cpp: In function `int main()':
    > xxx.cpp:7: error: array bound forbidden after parenthesized type-id
    > xxx.cpp:7: note: try removing the parentheses around the type-id


    So gcc seems to have understood what you wanted to do, but rejects it
    because it's not allowed.
     
    Rolf Magnus, Jul 30, 2004
    #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. joon
    Replies:
    1
    Views:
    527
    Roedy Green
    Jul 8, 2003
  2. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    459
  3. J.Ram
    Replies:
    7
    Views:
    669
  4. Dhananjay
    Replies:
    0
    Views:
    751
    Dhananjay
    Mar 26, 2009
  5. froil
    Replies:
    12
    Views:
    321
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page