class question - please help

R

Roger

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.
 
D

Dave

Roger said:
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.

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

Dave

Roger said:
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.

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

Josh Lessard

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??

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
http://www.cs.uwaterloo.ca
*****************************************************
 
R

Roger

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

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??
 
A

Alf P. Steinbach

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

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"?
 
D

David White

Roger said:
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

delete test;
}

I am kind of confused because the first one was given by my instructor.
How is the memory allocated in the first situation??

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.
Is there any
benefit or drawback of not using dynamic memory allocation??

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
 
D

Dave

Roger said:
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??


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!
 
R

Roger

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() ).

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.
 
D

Dave

Dave said:
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!

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...
 
D

David White

Roger said:
Actually my instructor used void main().

Try and find another instructor.
However, the odd thing was that I could compile the source code without
any error and the output was right. ???

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.
It's amazing how fast you can get your answer in a newsgroup.

We try to please.

DW
 
K

Karl Heinz Buchegger

Roger said:
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. ???

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 :)
 
M

Micah Cowan

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

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

This is the first one:

main()

main() has an int return type.
{
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??

It's not. The program invokes undefined behavior.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top