dynamic allocation of an array of pointers

Discussion in 'C++' started by jimjim, Mar 25, 2006.

  1. jimjim

    jimjim Guest

    Hello,

    I am coming from a C background and the below dynamic allocation of an array
    of pointers makes sense to me:
    #define SIZE 2
    int **p;
    p = malloc ( SIZE * sizeof ( int * ));
    for(int j=0; j<SIZE; j++)
    p[j] = malloc( SIZE * sizeof ( int ) );

    In C++, I have found out that the dynamic allocation of an array of pointers
    looks like below:
    int ** a ;
    *a = new (int*)[SIZE];
    for(int j=0; j<SIZE; j++)
    a[SIZE] = new int[SIZE];

    cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
    << endl << endl;


    1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as in
    C? The C++ compiler compains: "Cannot convert 'int *' to 'int * *' in
    function main()"
    2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
    understand the last two zero addresses.

    TIA
    jimjim
    jimjim, Mar 25, 2006
    #1
    1. Advertising

  2. jimjim

    Heinz Ozwirk Guest

    "jimjim" <> schrieb im Newsbeitrag news:0NbVf.41831$...
    > Hello,
    >
    > I am coming from a C background and the below dynamic allocation of an array
    > of pointers makes sense to me:
    > #define SIZE 2
    > int **p;
    > p = malloc ( SIZE * sizeof ( int * ));
    > for(int j=0; j<SIZE; j++)
    > p[j] = malloc( SIZE * sizeof ( int ) );
    >
    > In C++, I have found out that the dynamic allocation of an array of pointers
    > looks like below:
    > int ** a ;
    > *a = new (int*)[SIZE];
    > for(int j=0; j<SIZE; j++)
    > a[SIZE] = new int[SIZE];
    >
    > cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
    > << endl << endl;
    >
    >
    > 1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as in
    > C? The C++ compiler compains: "Cannot convert 'int *' to 'int * *' in
    > function main()"


    Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also compile, at least it does with Comeau's online compiler, but several other compilers seem to be confused by those extra parentheses, which looks too similiar to a cast, I guess.

    > 2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
    > understand the last two zero addresses.


    What would you expect with your C background when you see

    p = calloc(SIZE, sizeof(int*));
    p[SIZE] = calloc(SITE, sizeof(int));

    ?

    You asked for undefined behaviour and you got it.

    Regards
    Heinz
    Heinz Ozwirk, Mar 25, 2006
    #2
    1. Advertising

  3. jimjim

    Kai-Uwe Bux Guest

    jimjim wrote:

    > Hello,
    >
    > I am coming from a C background and the below dynamic allocation of an
    > array
    > of pointers makes sense to me:
    > #define SIZE 2
    > int **p;
    > p = malloc ( SIZE * sizeof ( int * ));
    > for(int j=0; j<SIZE; j++)
    > p[j] = malloc( SIZE * sizeof ( int ) );
    >
    > In C++, I have found out that the dynamic allocation of an array of
    > pointers looks like below:
    > int ** a ;
    > *a = new (int*)[SIZE];
    > for(int j=0; j<SIZE; j++)
    > a[SIZE] = new int[SIZE];
    >
    > cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " << &a[0][0]
    > << endl << endl;


    I think, this should be:

    #include <iostream>

    const unsigned SIZE = 50;

    int main ( void ) {
    int ** a ;
    a = new int* [SIZE]; // note: there are no parentheses around int*
    for(int j=0; j<SIZE; j++)
    a[SIZE] = new int[SIZE];
    std::cout << &a << " " << a << " "
    << &a[0] << " " << a[0] << " " << &a[0][0]
    << std::endl << std::endl;

    }


    > 1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as


    Try: "a = new int* [SIZE];"


    > in C? The C++ compiler compains: "Cannot convert 'int *' to 'int * *' in
    > function main()"
    > 2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
    > understand the last two zero addresses.


    What do you expect to see instead?



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 25, 2006
    #3
  4. jimjim

    Kai-Uwe Bux Guest

    Heinz Ozwirk wrote:

    > "jimjim" <> schrieb im Newsbeitrag
    > news:0NbVf.41831$...
    >> Hello,

    [snip]
    >> In C++, I have found out that the dynamic allocation of an array of
    >> pointers looks like below:
    >> int ** a ;
    >> *a = new (int*)[SIZE];
    >> for(int j=0; j<SIZE; j++)
    >> a[SIZE] = new int[SIZE];
    >>
    >> cout << &a << " " << a << " " << &a[0] << " " << a[0] << " " <<
    >> &a[0][0] << endl << endl;
    >>
    >>
    >> 1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as
    >> in C? The C++ compiler compains: "Cannot convert 'int *' to 'int * *' in
    >> function main()"

    >
    > Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also
    > compile, at least it does with Comeau's online compiler, but several other
    > compilers seem to be confused by those extra parentheses, which looks too
    > similiar to a cast, I guess.


    Actually, "a = new (int*)[SIZE]" does *not* compile with Comeau:

    "ComeauTest.c", line 7: error: a value of type "int *" cannot be assigned to
    an
    entity of type "int **"
    a = new (int*) [SIZE];
    ^

    [snip]


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 25, 2006
    #4
  5. jimjim

    jimjim Guest

    >> Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also
    >> compile, at least it does with Comeau's online compiler, but several
    >> other
    >> compilers seem to be confused by those extra parentheses, which looks too
    >> similiar to a cast, I guess.

    >
    > Actually, "a = new (int*)[SIZE]" does *not* compile with Comeau:
    >
    > "ComeauTest.c", line 7: error: a value of type "int *" cannot be assigned
    > to
    > an entity of type "int **"
    > a = new (int*) [SIZE];
    > ^
    >

    a = new (int*)[SIZE] does compile with Borland_bcc_5.5 !
    jimjim, Mar 26, 2006
    #5
  6. jimjim

    jimjim Guest

    >Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should also
    >compile, at least it does with Comeau's online
    >compiler, but several other compilers seem to be confused by those extra
    >parentheses, which looks too similiar to a
    >cast, I guess.


    You reckon the parentheses makes it look like a cast? What does the "new
    (int*) [SIZE]" statement do?

    > What would you expect with your C background when you see
    >
    > p = calloc(SIZE, sizeof(int*));
    > p[SIZE] = calloc(SITE, sizeof(int));
    >
    > You asked for undefined behaviour and you got it.


    That was a typo! It should be p[j] indeed !
    jimjim, Mar 26, 2006
    #6
  7. jimjim

    jimjim Guest

    >> 1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];" as
    >
    > Try: "a = new int* [SIZE];"
    >

    Thanx for pointing it out!

    What does the "new (int*) [SIZE]" statement do?

    >> in C? The C++ compiler compains: "Cannot convert 'int *' to 'int * *' in
    >> function main()"
    >> 2. cout outputs: 0012FF88 008521D4 008521D4 00000000 00000000 . I cannot
    >> understand the last two zero addresses.

    >
    > What do you expect to see instead?
    >

    That was a typo! It should be p[j] indeed !
    jimjim, Mar 26, 2006
    #7
  8. jimjim

    Kai-Uwe Bux Guest

    jimjim wrote:

    >>> 1. why is it "*a = new (int*)[SIZE];" and not "a = new (int*)[SIZE];"
    >>> as

    >>
    >> Try: "a = new int* [SIZE];"
    >>

    > Thanx for pointing it out!
    >
    > What does the "new (int*) [SIZE]" statement do?


    I think it does the following:

    new (T)

    and

    new T

    both allocate an object of type T and returns a T* to that object. However,

    new T [SIZE]

    and

    new (T) [SIZE]

    are parsed differently: the first does what you expect, the second parses as

    ( new (T) ) [SIZE]

    in other words, an object of type T is created and a pointer T* is returned;
    then that pointer is reinterpreted as an array and the entry at index SIZE
    is returned. The whole expression, therefore, has type T. This explains why
    you were able to write

    *a = new (int*) [Size];

    the right hand has type int* as does the left hand.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 26, 2006
    #8
  9. jimjim

    jimjim Guest

    > in other words, an object of type T is created and a pointer T* is
    > returned;
    > then that pointer is reinterpreted as an array and the entry at index SIZE
    > is returned. The whole expression, therefore, has type T. This explains
    > why
    > you were able to write
    >
    > *a = new (int*) [Size];
    >
    > the right hand has type int* as does the left hand.
    >

    Hi..thx for the reply!

    *a is an int*, agreed.

    You said that "The whole expression, therefore, has type T". I cannot
    understand how you have reached to the conclusion that the left hand side is
    a int*, tough.

    TIA
    jimjim, Mar 26, 2006
    #9
  10. jimjim

    Kai-Uwe Bux Guest

    jimjim wrote:

    >> in other words, an object of type T is created and a pointer T* is
    >> returned;
    >> then that pointer is reinterpreted as an array and the entry at index
    >> SIZE is returned. The whole expression, therefore, has type T. This
    >> explains why
    >> you were able to write
    >>
    >> *a = new (int*) [Size];
    >>
    >> the right hand has type int* as does the left hand.
    >>

    > Hi..thx for the reply!
    >
    > *a is an int*, agreed.
    >
    > You said that "The whole expression, therefore, has type T". I cannot
    > understand how you have reached to the conclusion that the left hand side
    > is a int*, tough.


    Huh? The left hand is a* and a* is an int*.

    If you meant the right hand side, let's just apply what was said:

    new (T) [Size] has type T

    thus, with T=int*

    new (int*) [Size] has type int*


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Mar 26, 2006
    #10
  11. jimjim <> wrote:
    >>> Actually it is "a = new int*[SIZE];". "a = new (int*)[SIZE]" should
    >>> also compile, at least it does with Comeau's online compiler, but
    >>> several other
    >>> compilers seem to be confused by those extra parentheses, which
    >>> looks too similiar to a cast, I guess.

    >>
    >> Actually, "a = new (int*)[SIZE]" does *not* compile with Comeau:
    >>
    >> "ComeauTest.c", line 7: error: a value of type "int *" cannot be
    >> assigned to
    >> an entity of type "int **"
    >> a = new (int*) [SIZE];
    >> ^
    >>

    > a = new (int*)[SIZE] does compile with Borland_bcc_5.5 !


    Expect Comeau to be right here. VS8 also rejects your code,
    suggesting as well that Comeau is right.

    regards
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Mar 27, 2006
    #11
  12. jimjim

    perstam Guest

    OK, I have a follow-up question

    I have to pass a pointer as a parameter to a function by reference an
    have to dynamically allocate an array of arrays that is referenced b
    that parameter pointer

    Here's my function header
    void CreateStructure (short * & Ptr

    How do I modify the syntax in the previous posts to this thread t
    dynamically allocate the array of arrays, then what is the syntax fo
    assigning an address to, say, the first element of that array o
    pointers
    perstam, Apr 5, 2006
    #12
  13. jimjim

    Fei Liu Guest

    "perstam" <-spam.invalid> wrote in message
    news:...
    > OK, I have a follow-up question.
    >
    > I have to pass a pointer as a parameter to a function by reference and
    > have to dynamically allocate an array of arrays that is referenced by
    > that parameter pointer.
    >
    > Here's my function header:
    > void CreateStructure (short * & Ptr)
    >
    > How do I modify the syntax in the previous posts to this thread to
    > dynamically allocate the array of arrays, then what is the syntax for
    > assigning an address to, say, the first element of that array of
    > pointers?
    >


    Please quote what you are replying to so we can see the context and
    references of your questions.
    Fei Liu, Apr 5, 2006
    #13
  14. jimjim

    Kai-Uwe Bux Guest

    perstam wrote:

    > OK, I have a follow-up question.
    >
    > I have to pass a pointer as a parameter to a function by reference and
    > have to dynamically allocate an array of arrays that is referenced by
    > that parameter pointer.
    >
    > Here's my function header:
    > void CreateStructure (short * & Ptr)
    >
    > How do I modify the syntax in the previous posts to this thread to
    > dynamically allocate the array of arrays, then what is the syntax for
    > assigning an address to, say, the first element of that array of
    > pointers?


    You are better off not knowing the syntax. Redesign the whole thing to use a
    vector of vectors instead of an array of arrays. That way, memory handling
    is taken care of. In case you actually want a matrix class, there are some
    suggestions in the FAQ about that.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Apr 5, 2006
    #14
    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. Replies:
    5
    Views:
    363
    -berlin.de
    Dec 24, 2004
  2. srini

    Dynamic mem allocation for pointers

    srini, Dec 14, 2005, in forum: C Programming
    Replies:
    4
    Views:
    345
    Barry Schwarz
    Dec 18, 2005
  3. Replies:
    1
    Views:
    273
    Bill Pursell
    May 23, 2006
  4. Ken
    Replies:
    24
    Views:
    3,831
    Ben Bacarisse
    Nov 30, 2006
  5. chris
    Replies:
    6
    Views:
    971
    chris
    Oct 28, 2005
Loading...

Share This Page