class question - please help

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

    class question - please help

    Can anyone tell me what is the difference between these two?

    class nodeType
    {
    int data;
    nodeType *link;
    };

    This is the first one:

    main()
    {
    nodeType *test;
    test->data=10;
    cout << nodeType->data; // 10
    }

    and this is the second one:

    main()
    {
    nodeType *test=new nodeType;
    test->data=10;
    cout << test->data; // 10
    }

    I am kind of confused because the first one was given by my instructor.
    How is the memory allocated in the first situation?? Is there any
    benefit or drawback of not using dynamic memory allocation??

    Thanks in advance.

  • Dave

    #2
    Re: class question - please help


    "Roger" <gnucpp@hotmail .com> wrote in message
    news:xg2lb.4260 6$mp1.39060@nwr ddc02.gnilink.n et...[color=blue]
    > Can anyone tell me what is the difference between these two?
    >
    > class nodeType
    > {
    > int data;
    > nodeType *link;
    > };
    >
    > This is the first one:
    >
    > main()
    > {
    > nodeType *test;
    > test->data=10;
    > cout << nodeType->data; // 10
    > }
    >
    > and this is the second one:
    >
    > main()
    > {
    > nodeType *test=new nodeType;
    > test->data=10;
    > cout << test->data; // 10
    > }
    >
    > I am kind of confused because the first one was given by my instructor.
    > How is the memory allocated in the first situation?? Is there any
    > benefit or drawback of not using dynamic memory allocation??
    >
    > Thanks in advance.
    >[/color]

    Well, looks like your instructor made a blunder in the first case! Until a
    pointer points at something, it *can't* be used!



    Comment

    • Roger

      #3
      Re: class question - please help

      Dave wrote:
      [color=blue]
      > "Roger" <gnucpp@hotmail .com> wrote in message
      > news:xg2lb.4260 6$mp1.39060@nwr ddc02.gnilink.n et...
      >[color=green]
      >>Can anyone tell me what is the difference between these two?
      >>
      >>class nodeType
      >>{
      >> int data;
      >> nodeType *link;
      >>};
      >>
      >>This is the first one:
      >>
      >>main()
      >>{
      >> nodeType *test;
      >> test->data=10;
      >> cout << nodeType->data; // 10
      >>}
      >>
      >>and this is the second one:
      >>
      >>main()
      >>{
      >> nodeType *test=new nodeType;
      >> test->data=10;
      >> cout << test->data; // 10
      >>}
      >>
      >>I am kind of confused because the first one was given by my instructor.
      >>How is the memory allocated in the first situation?? Is there any
      >>benefit or drawback of not using dynamic memory allocation??
      >>
      >>Thanks in advance.
      >>[/color]
      >
      >
      > Well, looks like your instructor made a blunder in the first case! Until a
      > pointer points at something, it *can't* be used!
      >
      >
      >[/color]

      At first I also thought about that way. But after I compiled the
      program, no error was found and the reasult print out successfully.
      I am wondering why this happened??

      Comment

      • Alf P. Steinbach

        #4
        Re: class question - please help

        On Mon, 20 Oct 2003 21:16:46 -0700, "Dave" <better_cs_now@ yahoo.com> wrote:
        [color=blue]
        >
        >"Roger" <gnucpp@hotmail .com> wrote in message
        >news:xg2lb.426 06$mp1.39060@nw rddc02.gnilink. net...[color=green]
        >> Can anyone tell me what is the difference between these two?
        >>
        >> class nodeType
        >> {
        >> int data;
        >> nodeType *link;
        >> };
        >>
        >> This is the first one:
        >>
        >> main()
        >> {
        >> nodeType *test;
        >> test->data=10;
        >> cout << nodeType->data; // 10
        >> }
        >>
        >> and this is the second one:
        >>
        >> main()
        >> {
        >> nodeType *test=new nodeType;
        >> test->data=10;
        >> cout << test->data; // 10
        >> }
        >>
        >> I am kind of confused because the first one was given by my instructor.
        >> How is the memory allocated in the first situation?? Is there any
        >> benefit or drawback of not using dynamic memory allocation??
        >>
        >> Thanks in advance.
        >>[/color]
        >
        >Well, looks like your instructor made a blunder in the first case! Until a
        >pointer points at something, it *can't* be used![/color]

        He or she made at least five blunders, _assuming_ the code was not
        meant to illustrate the following defects:

        1) There's nothing accessible in that class.
        2) Illegal declaration of 'main'.
        3) Forgetting to allocate an object.
        4) Forgetting to include <iostream>
        5) Forgetting to output a logical newline at the end.

        But how do we know this question isn't homework in disguise, "find 5
        defects in the following code"?

        Comment

        • David White

          #5
          Re: class question - please help

          Roger <gnucpp@hotmail .com> wrote in message
          news:xg2lb.4260 6$mp1.39060@nwr ddc02.gnilink.n et...[color=blue]
          > Can anyone tell me what is the difference between these two?
          >
          > class nodeType
          > {
          > int data;
          > nodeType *link;
          > };
          >
          > This is the first one:
          >
          > main()
          > {
          > nodeType *test;
          > test->data=10;
          > cout << nodeType->data; // 10
          > }
          >
          > and this is the second one:
          >
          > main()
          > {
          > nodeType *test=new nodeType;
          > test->data=10;
          > cout << test->data; // 10[/color]

          delete test;
          [color=blue]
          > }
          >
          > I am kind of confused because the first one was given by my instructor.
          > How is the memory allocated in the first situation??[/color]

          It's not. It's a bug serious enough to crash a program, or, worse, keep
          running but with undefined behaviour that you might or might not notice.
          [color=blue]
          > Is there any
          > benefit or drawback of not using dynamic memory allocation??[/color]

          If you want to be sure your program will work, your pointer _has_ to point
          to a real object before you dereference it, whether it be dynamically,
          statically or automatically allocated.

          DW



          Comment

          • Josh Lessard

            #6
            Re: class question - please help

            On Tue, 21 Oct 2003, Roger wrote:
            [color=blue]
            > Can anyone tell me what is the difference between these two?
            >
            > class nodeType
            > {
            > int data;
            > nodeType *link;
            > };
            >
            > This is the first one:
            >
            > main()
            > {
            > nodeType *test;
            > test->data=10;
            > cout << nodeType->data; // 10
            > }
            >
            > and this is the second one:
            >
            > main()
            > {
            > nodeType *test=new nodeType;
            > test->data=10;
            > cout << test->data; // 10
            > }
            >
            > I am kind of confused because the first one was given by my instructor.
            > How is the memory allocated in the first situation?? Is there any
            > benefit or drawback of not using dynamic memory allocation??[/color]

            You're correct in being confused. The first example won't work at all.
            You have a pointer to nodeType, but it doesn't point at anything. If
            you're lucky, it will crash...if you're not, it will produce nonsensical
            results. If your instructor was trying to show you an automatic variable
            (which is allocated on the system stack), he should have given something
            like this:

            int main()
            {
            nodeType test;
            test.data=10;
            cout << nodeType.data; // 10
            }

            Also, your instructor is giving you nonstandard C++ code. The main
            function must always be defined with a return type of int. (ie, int
            main() instead of main() ).

            *************** *************** *************** ********
            Josh Lessard
            Master's Student
            School of Computer Science
            Faculty of Mathematics
            University of Waterloo
            (519)888-4567 x3400
            Waterloo's Cheriton School of Computer Science is home to world-renowned faculty committed to excellence in teaching and advancing computer science research.

            *************** *************** *************** ********

            Comment

            • Dave

              #7
              Re: class question - please help


              "Roger" <gnucpp@hotmail .com> wrote in message
              news:Kw2lb.2168 8$Ee6.19721@nwr ddc01.gnilink.n et...[color=blue]
              > Dave wrote:
              >[color=green]
              > > "Roger" <gnucpp@hotmail .com> wrote in message
              > > news:xg2lb.4260 6$mp1.39060@nwr ddc02.gnilink.n et...
              > >[color=darkred]
              > >>Can anyone tell me what is the difference between these two?
              > >>
              > >>class nodeType
              > >>{
              > >> int data;
              > >> nodeType *link;
              > >>};
              > >>
              > >>This is the first one:
              > >>
              > >>main()
              > >>{
              > >> nodeType *test;
              > >> test->data=10;
              > >> cout << nodeType->data; // 10
              > >>}
              > >>
              > >>and this is the second one:
              > >>
              > >>main()
              > >>{
              > >> nodeType *test=new nodeType;
              > >> test->data=10;
              > >> cout << test->data; // 10
              > >>}
              > >>
              > >>I am kind of confused because the first one was given by my instructor.
              > >>How is the memory allocated in the first situation?? Is there any
              > >>benefit or drawback of not using dynamic memory allocation??
              > >>
              > >>Thanks in advance.
              > >>[/color]
              > >
              > >
              > > Well, looks like your instructor made a blunder in the first case![/color][/color]
              Until a[color=blue][color=green]
              > > pointer points at something, it *can't* be used!
              > >
              > >
              > >[/color]
              >
              > At first I also thought about that way. But after I compiled the
              > program, no error was found and the reasult print out successfully.
              > I am wondering why this happened??
              >[/color]


              It worked purely by serendipity. Believe it, this is a bad program and it
              cannot be relied on, even if it seems to work at times!



              Comment

              • Roger

                #8
                Re: class question - please help

                [color=blue]
                > like this:
                >
                > int main()
                > {
                > nodeType test;
                > test.data=10;
                > cout << nodeType.data; // 10
                > }
                >
                > Also, your instructor is giving you nonstandard C++ code. The main
                > function must always be defined with a return type of int. (ie, int
                > main() instead of main() ).
                >[/color]

                Sorry, that was my mistake. and I did not put #include <iostream> and
                using namespace std; Actually my instructor used void main().
                However, the odd thing was that I could compile the source code without
                any error and the output was right. ???

                It's amazing how fast you can get your answer in a newsgroup.


                Comment

                • Dave

                  #9
                  Re: class question - please help


                  "Dave" <better_cs_now@ yahoo.com> wrote in message
                  news:cM2lb.6136 3$La.30402@fed1 read02...[color=blue]
                  >
                  > "Roger" <gnucpp@hotmail .com> wrote in message
                  > news:Kw2lb.2168 8$Ee6.19721@nwr ddc01.gnilink.n et...[color=green]
                  > > Dave wrote:
                  > >[color=darkred]
                  > > > "Roger" <gnucpp@hotmail .com> wrote in message
                  > > > news:xg2lb.4260 6$mp1.39060@nwr ddc02.gnilink.n et...
                  > > >
                  > > >>Can anyone tell me what is the difference between these two?
                  > > >>
                  > > >>class nodeType
                  > > >>{
                  > > >> int data;
                  > > >> nodeType *link;
                  > > >>};
                  > > >>
                  > > >>This is the first one:
                  > > >>
                  > > >>main()
                  > > >>{
                  > > >> nodeType *test;
                  > > >> test->data=10;
                  > > >> cout << nodeType->data; // 10
                  > > >>}
                  > > >>
                  > > >>and this is the second one:
                  > > >>
                  > > >>main()
                  > > >>{
                  > > >> nodeType *test=new nodeType;
                  > > >> test->data=10;
                  > > >> cout << test->data; // 10
                  > > >>}
                  > > >>
                  > > >>I am kind of confused because the first one was given by my[/color][/color][/color]
                  instructor.[color=blue][color=green][color=darkred]
                  > > >>How is the memory allocated in the first situation?? Is there any
                  > > >>benefit or drawback of not using dynamic memory allocation??
                  > > >>
                  > > >>Thanks in advance.
                  > > >>
                  > > >
                  > > >
                  > > > Well, looks like your instructor made a blunder in the first case![/color][/color]
                  > Until a[color=green][color=darkred]
                  > > > pointer points at something, it *can't* be used!
                  > > >
                  > > >
                  > > >[/color]
                  > >
                  > > At first I also thought about that way. But after I compiled the
                  > > program, no error was found and the reasult print out successfully.
                  > > I am wondering why this happened??
                  > >[/color]
                  >
                  >
                  > It worked purely by serendipity. Believe it, this is a bad program and it
                  > cannot be relied on, even if it seems to work at times!
                  >
                  >
                  >[/color]

                  And as a second note, it's not surprising it compiled OK with regard to the
                  pointer error. That part of the program is syntactically legal, it's just
                  not semantically legal (again, you can't use a pointer that doesn't point at
                  anything; but the compiler cannot detect that in the general case at compile
                  time). Of course, as Alf pointed out, there are other problems besides the
                  pointer problem which the compiler should have balked at...


                  Comment

                  • David White

                    #10
                    Re: class question - please help

                    Roger <gnucpp@hotmail .com> wrote in message
                    news:gR2lb.2179 9$Ee6.15070@nwr ddc01.gnilink.n et...[color=blue]
                    >
                    > Actually my instructor used void main().[/color]

                    Try and find another instructor.
                    [color=blue]
                    > However, the odd thing was that I could compile the source code without
                    > any error and the output was right. ???[/color]

                    It worked by accident. That's all. The pointer just happened to point to a
                    writable/readable address in memory that, apparently, didn't cause any
                    damage or misbehaviour when accessed in your program.
                    [color=blue]
                    >
                    > It's amazing how fast you can get your answer in a newsgroup.[/color]

                    We try to please.

                    DW



                    Comment

                    • Dave

                      #11
                      Re: class question - please help


                      "Roger" <gnucpp@hotmail .com> wrote in message
                      news:xg2lb.4260 6$mp1.39060@nwr ddc02.gnilink.n et...[color=blue]
                      > Can anyone tell me what is the difference between these two?
                      >
                      > class nodeType
                      > {
                      > int data;
                      > nodeType *link;
                      > };
                      >
                      > This is the first one:
                      >
                      > main()
                      > {
                      > nodeType *test;
                      > test->data=10;
                      > cout << nodeType->data; // 10
                      > }
                      >
                      > and this is the second one:
                      >
                      > main()
                      > {
                      > nodeType *test=new nodeType;
                      > test->data=10;
                      > cout << test->data; // 10
                      > }
                      >
                      > I am kind of confused because the first one was given by my instructor.
                      > How is the memory allocated in the first situation?? Is there any
                      > benefit or drawback of not using dynamic memory allocation??
                      >
                      > Thanks in advance.
                      >[/color]

                      Well, looks like your instructor made a blunder in the first case! Until a
                      pointer points at something, it *can't* be used!


                      Comment

                      • Micah Cowan

                        #12
                        Re: class question - please help

                        Roger <gnucpp@hotmail .com> writes:
                        [color=blue]
                        > Can anyone tell me what is the difference between these two?
                        >
                        > class nodeType
                        > {
                        > int data;
                        > nodeType *link;
                        > };
                        >
                        > This is the first one:
                        >
                        > main()[/color]

                        main() has an int return type.
                        [color=blue]
                        > {
                        > nodeType *test;
                        > test->data=10;
                        > cout << nodeType->data; // 10
                        > }
                        >
                        > and this is the second one:
                        >
                        > main()
                        > {
                        > nodeType *test=new nodeType;
                        > test->data=10;
                        > cout << test->data; // 10
                        > }
                        >
                        > I am kind of confused because the first one was given by my instructor.
                        > How is the memory allocated in the first situation??[/color]

                        It's not. The program invokes undefined behavior.

                        Your instructor is broken. Buy a new one. :-)

                        --
                        Micah J. Cowan
                        micah@cowan.nam e

                        Comment

                        • Karl Heinz Buchegger

                          #13
                          Re: class question - please help



                          Roger wrote:[color=blue]
                          >[color=green]
                          > > like this:
                          > >
                          > > int main()
                          > > {
                          > > nodeType test;
                          > > test.data=10;
                          > > cout << nodeType.data; // 10
                          > > }
                          > >
                          > > Also, your instructor is giving you nonstandard C++ code. The main
                          > > function must always be defined with a return type of int. (ie, int
                          > > main() instead of main() ).
                          > >[/color]
                          >
                          > Sorry, that was my mistake. and I did not put #include <iostream> and
                          > using namespace std; Actually my instructor used void main().
                          > However, the odd thing was that I could compile the source code without
                          > any error and the output was right. ???[/color]

                          The compiler only cares about the syntax (the grammer). Syntactically there
                          is nothing wrong with this program. All the statements are well formed and
                          fullfill the rules of the language. Same as in plain english:

                          The car eats the cake.

                          From a syntactical point of view there is nothing wrong with that sentence.
                          It has a subject, a predicate an object and those are presented in an order
                          which form an english sentence. But of course this sentence is still
                          nonsense. The semantic (the logic) is wrong. Cars can't eat.
                          Making the logic right is your job (and this is definitly the harder part :-)

                          --
                          Karl Heinz Buchegger
                          kbuchegg@gascad .at

                          Comment

                          • Christopher Benson-Manica

                            #14
                            Re: class question - please help

                            Roger <gnucpp@hotmail .com> spoke thus:
                            [color=blue]
                            > Actually my instructor used void main().[/color]

                            Then he's broken than you originally made him out to be :)

                            --
                            Christopher Benson-Manica | I *should* know what I'm talking about - if I
                            ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                            Comment

                            Working...