interesting pointer problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Davy

    #1

    interesting pointer problem?

    I found a interesting pointer problem.

    What does the pointer mean?
    EXTERN short (*blocks)[64];

    And what does the malloc mean?
    blocks =
    (short (*)[64])malloc(count*s izeof(short [64]));

    Any suggestions will be appreciated!
    Best regards,
    Davy

  • Lawrence Kirby

    #2
    Re: interesting pointer problem?

    On Tue, 06 Sep 2005 01:40:01 -0700, Davy wrote:
    [color=blue]
    > I found a interesting pointer problem.
    >
    > What does the pointer mean?
    > EXTERN short (*blocks)[64];[/color]

    EXTERN isn't a standard identifier, it is probably defined as a macro
    somewhere expanding to extern or nothing.

    blocks is a pointer to an array of 64 shorts.
    [color=blue]
    > And what does the malloc mean?
    > blocks =
    > (short (*)[64])malloc(count*s izeof(short [64]));[/color]

    This allocates in effect a 2D array (in fact an array of arrays) of count
    * 64 shorts whose elements can be accessed using blocks[a][b]. Assuming
    the malloc() call succeeds of course.

    Note this is a case where C and C++ have different approaches. In C you
    would tend to use the malloc() call without the cast, in C++ you would
    tend to use the new operator.

    Lawrence

    Comment

    • Rolf Magnus

      #3
      Re: interesting pointer problem?

      Davy wrote:
      [color=blue]
      > I found a interesting pointer problem.
      >
      > What does the pointer mean?
      > EXTERN short (*blocks)[64];[/color]

      Assuming EXTERN is just #defined to be the same as extern, it means that a
      variable named 'blocks' is declared (not defined) that is a pointer to an
      array of 64 short values.
      [color=blue]
      > And what does the malloc mean?
      > blocks =
      > (short (*)[64])malloc(count*s izeof(short [64]));[/color]

      It allocates 'count' arrays of 64 short and assigns the address of the first
      one to 'blocks'.

      Comment

      • Davy

        #4
        Re: interesting pointer problem?

        Hi,

        Thank you for your help!

        So the "blocks" is a pointer to a dynamic 2-D array, is it?

        I am confused with
        " short (*blocks)[64];"
        and " short [64] (*blocks);"
        and "short *blocks [64];".

        What's their difference?

        Best regards,
        Davy

        Comment

        • John Carson

          #5
          Re: interesting pointer problem?

          "Davy" <zhushenli@gmai l.com> wrote in message
          news:1126010573 .382090.155720@ g14g2000cwa.goo glegroups.com[color=blue]
          > Hi,
          >
          > Thank you for your help!
          >
          > So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]

          It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
          allocate several of these, one after the other, and make blocks point to the
          first of them, then it is pointing to the first row of a 2D array. blocks[0]
          will get row 0, blocks[1] will get the row 1 and so on. blocks[i][j] will
          get the (i,j)th element of a 2D array.
          [color=blue]
          >
          > I am confused with
          > " short (*blocks)[64];"
          > and " short [64] (*blocks);"
          > and "short *blocks [64];".
          >
          > What's their difference?[/color]

          The first has been explained, the second is not valid C++ at all, and the
          second is an ordinary array comprising 64 pointers to short.

          --
          John Carson

          Comment

          • Karl Heinz Buchegger

            #6
            Re: interesting pointer problem?

            Davy wrote:[color=blue]
            >
            > Hi,
            >
            > Thank you for your help!
            >
            > So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]

            No. It is a pointer to an 1D array.
            [color=blue]
            >
            > I am confused with
            > " short (*blocks)[64];"[/color]

            start at the variable name. Then read to the right until you reach
            a ')' or ']'. Continue reading at the left until you hit a '(' or '['
            where you continue to the right and so on. (This is called the left/right rule)

            blocks blocks is
            blocks) oops, stop reading right, continue left
            *blocks) blocks is a pointer
            (*blocks) oops, stop reading left, continue to the right
            (*blocks)[ blocks is a pointer to an array
            (*blocks)[64 blocks is a pointer to an array of size 64
            (*blocks)[64] oops, stop reading right, continue to the left
            short (*blocks)[64] blocks is a pointer to an array of size 64 of short

            [color=blue]
            > and " short [64] (*blocks);"[/color]

            blocks blocks is
            blocks) -> left
            *blocks) blocks is a pointer
            (*blocks) -> right
            (*blocks) nothing on the right, continue left
            ](*blocks) syntax error, there cannot be a '[' at this position

            [color=blue]
            > and "short *blocks [64];".[/color]

            blocks blocks is
            blocks [ blocks is an array
            blocks [64 blocks is an array of size 64
            blocks [64] -> right
            * blocks [64] blocks is an array of size 64 of pointer
            short * blocks [64] blocks is an array of size 64 of pointer to short



            So the first one (used in a program) can be used to build up this data structure

            blocks
            +---------+ +-------+ ----+
            | o----------------->| | |
            +---------+ +-------+ |
            | | |
            ^ +-------+
            | | | 64 short's
            | ...
            | | | |
            | +-------+ |
            | | | |
            | +-------+ ----+
            |
            | ^
            | |
            | Array of short
            |
            Pointer to Array of short's

            While the third one, can be used to build this:


            blocks
            +--------+ +-----+
            | o-------------------------->| |
            +--------+ +-----+ +-----+
            | o----------------->| |
            +--------+ +-----+ +-----+
            | o----------------------------->| |
            ..... +-----+ +-----+
            | o-------------------->| |
            +--------+ +-----+ +-----+
            | o------------------------------->| |
            +--------+ +-----+

            ^
            |
            An array of pointers, where each pointer points to a short


            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Karl Heinz Buchegger

              #7
              Re: interesting pointer problem?

              John Carson wrote:[color=blue]
              >
              > "Davy" <zhushenli@gmai l.com> wrote in message
              > news:1126010573 .382090.155720@ g14g2000cwa.goo glegroups.com[color=green]
              > > Hi,
              > >
              > > Thank you for your help!
              > >
              > > So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]
              >
              > It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
              > allocate several of these, one after the other, and make blocks point to the
              > first of them, then it is pointing to the first row of a 2D array. blocks[0]
              > will get row 0, blocks[1] will get the row 1 and so on. blocks[i][j] will
              > get the (i,j)th element of a 2D array.[/color]

              No way. That indexing will not work.

              --
              Karl Heinz Buchegger
              kbuchegg@gascad .at

              Comment

              • Rolf Magnus

                #8
                Re: interesting pointer problem?

                Karl Heinz Buchegger wrote:
                [color=blue][color=green]
                >> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
                >> allocate several of these, one after the other, and make blocks point to
                >> the first of them, then it is pointing to the first row of a 2D array.
                >> blocks[0] will get row 0, blocks[1] will get the row 1 and so on.
                >> blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
                >
                > No way. That indexing will not work.[/color]

                Why not?

                Comment

                • John Carson

                  #9
                  Re: interesting pointer problem?

                  "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
                  news:431D986E.D 2037182@gascad. at[color=blue]
                  > John Carson wrote:[color=green]
                  >>
                  >> "Davy" <zhushenli@gmai l.com> wrote in message
                  >> news:1126010573 .382090.155720@ g14g2000cwa.goo glegroups.com[color=darkred]
                  >>> Hi,
                  >>>
                  >>> Thank you for your help!
                  >>>
                  >>> So the "blocks" is a pointer to a dynamic 2-D array, is it?[/color]
                  >>
                  >> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it
                  >> you allocate several of these, one after the other, and make blocks
                  >> point to the first of them, then it is pointing to the first row of
                  >> a 2D array. blocks[0] will get row 0, blocks[1] will get the row 1
                  >> and so on. blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
                  >
                  > No way. That indexing will not work.[/color]

                  Seems to work fine:

                  #include <iostream>
                  #include <iomanip>
                  using namespace std;

                  int main()
                  {
                  const size_t rowcount = 3;
                  const size_t colcount = 16;

                  short (*blocks)[colcount];

                  blocks = (short (*)[colcount])
                  malloc(rowcount *sizeof(short [colcount]));

                  for(size_t i=0; i<rowcount; ++i)
                  {
                  for(size_t j=0; j<colcount; ++j)
                  blocks[i][j] = i*100 + j;
                  }
                  for(size_t i=0; i<rowcount; ++i)
                  {
                  for(size_t j=0; j<colcount; ++j)
                  cout << setw(3) << blocks[i][j] << ' ';
                  cout << endl;
                  }
                  free(blocks);
                  }


                  --
                  John Carson

                  Comment

                  • Karl Heinz Buchegger

                    #10
                    Re: interesting pointer problem?

                    John Carson wrote:[color=blue]
                    >
                    > "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
                    > news:431D986E.D 2037182@gascad. at[color=green]
                    > > John Carson wrote:[color=darkred]
                    > >>
                    > >> "Davy" <zhushenli@gmai l.com> wrote in message
                    > >> news:1126010573 .382090.155720@ g14g2000cwa.goo glegroups.com
                    > >>> Hi,
                    > >>>
                    > >>> Thank you for your help!
                    > >>>
                    > >>> So the "blocks" is a pointer to a dynamic 2-D array, is it?
                    > >>
                    > >> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it
                    > >> you allocate several of these, one after the other, and make blocks
                    > >> point to the first of them, then it is pointing to the first row of
                    > >> a 2D array. blocks[0] will get row 0, blocks[1] will get the row 1
                    > >> and so on. blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
                    > >
                    > > No way. That indexing will not work.[/color]
                    >
                    > Seems to work fine:
                    >[/color]

                    Mea culpa.
                    Sorry. I read something else in your explanation.


                    --
                    Karl Heinz Buchegger
                    kbuchegg@gascad .at

                    Comment

                    • Karl Heinz Buchegger

                      #11
                      Re: interesting pointer problem?

                      Rolf Magnus wrote:[color=blue]
                      >
                      > Karl Heinz Buchegger wrote:
                      >[color=green][color=darkred]
                      > >> It is a pointer to a 1 dimensional array of 64 shorts. Of course, it you
                      > >> allocate several of these, one after the other, and make blocks point to
                      > >> the first of them, then it is pointing to the first row of a 2D array.
                      > >> blocks[0] will get row 0, blocks[1] will get the row 1 and so on.
                      > >> blocks[i][j] will get the (i,j)th element of a 2D array.[/color]
                      > >
                      > > No way. That indexing will not work.[/color]
                      >
                      > Why not?[/color]

                      I made a mistake. Somehow I mixed the rows with the columns when thinking
                      through and came to the conclusion that ....

                      But John is fine. It will work.

                      --
                      Karl Heinz Buchegger
                      kbuchegg@gascad .at

                      Comment

                      • Emmanuel Delahaye

                        #12
                        Re: interesting pointer problem?

                        John Carson wrote on 06/09/05 :[color=blue]
                        > Seems to work fine:
                        >
                        > #include <iostream>[/color]

                        <...>

                        Not a C-code.

                        --
                        Emmanuel
                        The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                        The C-library: http://www.dinkumware.com/refxc.html

                        "It's specified. But anyone who writes code like that should be
                        transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC


                        Comment

                        • Davy

                          #13
                          Re: interesting pointer problem?

                          Hi Karl and John,

                          Thank you all for your generous help! I understand!

                          Best regards,
                          Davy

                          Comment

                          Working...