I don't understand this assignment...

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

    I don't understand this assignment...

    In the code below, what does this mean?

    mine = (short *)0;


    --------------
    #include <iostream>

    int main()
    {
    typedef short Example;

    short *mine;
    mine = (short *)0;
    if(mine)
    {
    delete [] mine;
    }
    mine = new short[10];

    for (int i = 0; i < 10; ++i)
    mine[i] = 0;

    for (int i = 0; i < 10; ++i)
    std::cout << mine[i]
    << " short"
    << std::endl;

    }

    ------------

    Cheers,

    Deets
  • Cy Edmunds

    #2
    Re: I don't understand this assignment...

    "Anon Email" <anonemail1@fas tmail.fm> wrote in message
    news:83b3ca3.04 02171516.7e0146 63@posting.goog le.com...[color=blue]
    > In the code below, what does this mean?
    >
    > mine = (short *)0;[/color]

    That means to cast the zero to a short pointer and assign it to mine. The
    cast is not necessary, by the way. There are many more mysteries in the code
    that follows.
    [color=blue]
    >
    >
    > --------------
    > #include <iostream>
    >
    > int main()
    > {
    > typedef short Example;[/color]

    OK, but not used.
    [color=blue]
    >
    > short *mine;
    > mine = (short *)0;
    > if(mine)[/color]

    This means if mine is NOT 0. But of course we just set it to 0, so why
    bother?
    [color=blue]
    > {
    > delete [] mine;
    > }
    > mine = new short[10];[/color]

    The 7 lines above could have been written

    short *mine = new short[10];

    Of course it would have been better to avoid "new" altogether and just write

    short mine[10];

    That way we can't have a memory leak as we do in this code.
    [color=blue]
    >
    > for (int i = 0; i < 10; ++i)
    > mine[i] = 0;
    >
    > for (int i = 0; i < 10; ++i)
    > std::cout << mine[i]
    > << " short"
    > << std::endl;
    >
    > }
    >
    > ------------
    >
    > Cheers,
    >
    > Deets[/color]

    You called this an "assignment " ... did your instructor write this?
    <shudder>

    --
    Cy



    Comment

    • John Harrison

      #3
      Re: I don't understand this assignment...


      "Anon Email" <anonemail1@fas tmail.fm> wrote in message
      news:83b3ca3.04 02171516.7e0146 63@posting.goog le.com...[color=blue]
      > In the code below, what does this mean?
      >
      > mine = (short *)0;
      >
      >[/color]

      0 is one way of writing the null pointer. Have you heard of that? If not
      look it up in your favourite C++ book.

      The cast is unnecessary and stylistically wrong.

      mine = 0;

      is prefectly good.

      john


      Comment

      • David White

        #4
        Re: I don't understand this assignment...

        "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message
        news:gsxYb.6632 8$n62.10194@twi ster.nyroc.rr.c om...[color=blue]
        > You called this an "assignment " ... did your instructor write this?
        > <shudder>[/color]

        This is an assignment regardless of who wrote it:
        mine = (short *)0;

        DW



        Comment

        • Dan Cernat

          #5
          Re: I don't understand this assignment...


          "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message
          news:gsxYb.6632 8$n62.10194@twi ster.nyroc.rr.c om...

          <snip/>[color=blue]
          >
          > You called this an "assignment " ... did your instructor write this?
          > <shudder>
          >
          > --
          > Cy
          > http://home.rochester.rr.com/cyhome/
          >[/color]
          The assignment was to find out what was wrong with the code :-)

          /dan


          Comment

          • Anon Email

            #6
            Re: I don't understand this assignment...

            I assign to myself assignments which investigate unusual C++
            assignments.

            Huh?

            Seriously, I was considering the null pointer assignment discussed,
            not a homework assignment! I probably unintentionally attracted a bit
            of attention in this forum, as the title of my post sounds like a plea
            for help with course work! (Sorry about that.) But I am in fact my own
            instructor <shudder!> :)

            Anyway, lets take the following code:

            short *mine;
            mine = (short *)0;
            mine = new short[10];

            So, in summary, it's possible to write these three lines like this?

            short mine[10];

            Cheers,

            Deets

            P.S. Where's the memory leak?

            Comment

            • jeffc

              #7
              Re: I don't understand this assignment...


              "John Harrison" <john_andronicu s@hotmail.com> wrote in message
              news:c0u9bl$1al hqj$1@ID-196037.news.uni-berlin.de...[color=blue]
              >
              > mine = 0;
              >
              > is prefectly good.[/color]

              There are just certain words that should never be mispeled.


              Comment

              • John Harrison

                #8
                Re: I don't understand this assignment...


                "Anon Email" <anonemail1@fas tmail.fm> wrote in message
                news:83b3ca3.04 02180651.4389d9 7b@posting.goog le.com...[color=blue]
                > I assign to myself assignments which investigate unusual C++
                > assignments.
                >
                > Huh?
                >
                > Seriously, I was considering the null pointer assignment discussed,
                > not a homework assignment! I probably unintentionally attracted a bit
                > of attention in this forum, as the title of my post sounds like a plea
                > for help with course work! (Sorry about that.) But I am in fact my own
                > instructor <shudder!> :)
                >
                > Anyway, lets take the following code:
                >
                > short *mine;
                > mine = (short *)0;
                > mine = new short[10];
                >
                > So, in summary, it's possible to write these three lines like this?
                >
                > short mine[10];[/color]

                The major difference between these two is the lifetime of the memory
                allocated. In the first example the memory pointed to by mine is only
                deallocated when you explicitly say delete[] using the address of the memory
                allocated. In the second example the memory is deallocated when the scope
                containing the declaration mine is exited.

                {
                short * mine1 = new short[10];
                short mine2[10];
                } // end of scope

                At the point labelled end of scope mine1 and mine2 no longer exist, but the
                memory that was pointed to by mine1 still exists but is no longer accessible
                (a memory leak).
                [color=blue]
                >
                > Cheers,
                >
                > Deets
                >
                > P.S. Where's the memory leak?[/color]

                In the first example when you fail to use delete[], in the second example
                there can be no memory leak.

                john


                Comment

                • Thomas Matthews

                  #9
                  Re: I don't understand this assignment...

                  Anon Email wrote:
                  [color=blue]
                  > I assign to myself assignments which investigate unusual C++
                  > assignments.
                  >
                  > Huh?
                  >
                  > Seriously, I was considering the null pointer assignment discussed,
                  > not a homework assignment! I probably unintentionally attracted a bit
                  > of attention in this forum, as the title of my post sounds like a plea
                  > for help with course work! (Sorry about that.) But I am in fact my own
                  > instructor <shudder!> :)
                  >
                  > Anyway, lets take the following code:
                  >
                  > short *mine;
                  > mine = (short *)0;
                  > mine = new short[10];
                  >
                  > So, in summary, it's possible to write these three lines like this?
                  >
                  > short mine[10];
                  >
                  > Cheers,
                  >
                  > Deets
                  >
                  > P.S. Where's the memory leak?[/color]

                  There is a memory leak if:
                  1. You allocate memory and never delete/free it.
                  2. The location of the allocated memory is lost:
                  short * mine;
                  short * yours;
                  mine = new short[20];
                  yours = new short[30];
                  mine = new short [10]; // this causes leak.
                  3. Variables declared without using dynamic
                  memory will be destroyed automatically by
                  the program; the programmer doesn't have to
                  remember to destroy them.
                  4. Using delete instead of delete[] will also
                  create leaks in memory.

                  There is a section about this in the C++ FAQ:

                  One should consult the FAQ before posting.

                  --
                  Thomas Matthews

                  C++ newsgroup welcome message:

                  C++ Faq: http://www.parashift.com/c++-faq-lite
                  C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                  alt.comp.lang.l earn.c-c++ faq:

                  Other sites:
                  http://www.josuttis.com -- C++ STL Library book

                  Comment

                  • Karl Heinz Buchegger

                    #10
                    Re: I don't understand this assignment...

                    Anon Email wrote:[color=blue]
                    >
                    > Anyway, lets take the following code:
                    >
                    > short *mine;
                    > mine = (short *)0;
                    > mine = new short[10];
                    >
                    > So, in summary, it's possible to write these three lines like this?
                    >
                    > short mine[10];[/color]

                    They do different things, but in the end: yes you can.

                    The first does:
                    create a pointer, called mine.
                    assign the value 0 to that pointer.
                    allocate memory for 10 short integers and assign the starting address
                    of that memory to mine.

                    The second does:
                    create an array of 10 integers
                    [color=blue]
                    >
                    > P.S. Where's the memory leak?[/color]

                    Anything allocated with new has to be freed with delete.
                    Anything allocated with new[] has to be freed with delete [].

                    Now look in your posted example. You use new[], but where is
                    the corresponding delete[] ?

                    PS: I hope you use a good textbook.

                    --
                    Karl Heinz Buchegger
                    kbuchegg@gascad .at

                    Comment

                    • Anon Email

                      #11
                      Re: I don't understand this assignment...

                      Thanks guys. That has really helped.

                      To Karl: Yes, I'm using a good C++ book - Bjarne Stroustrup's "The C++
                      Programming Language". I normally only post when I've researched the
                      material on the Internet or in a book first. If I don't understand,
                      then I post. To be honest, I have difficulties reading - my eyes get
                      tired very easily. I also find that I understand topics better if I
                      talk about them - that's why I like this forum so much.

                      Anyway, thanks once more. I'll try to be more thorough in my research
                      before posting.

                      Cheers,

                      Deets

                      Comment

                      Working...