473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Jul 19 '05 #1
13 1599

"Roger" <gn****@hotmail.com> wrote in message
news:xg*******************@nwrddc02.gnilink.net...
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!

Jul 19 '05 #2
Dave wrote:
"Roger" <gn****@hotmail.com> wrote in message
news:xg*******************@nwrddc02.gnilink.net...
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!


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

Jul 19 '05 #3
On Mon, 20 Oct 2003 21:16:46 -0700, "Dave" <be***********@yahoo.com> wrote:

"Roger" <gn****@hotmail.com> wrote in message
news:xg*******************@nwrddc02.gnilink.net.. .
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!


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

Jul 19 '05 #4
Roger <gn****@hotmail.com> wrote in message
news:xg*******************@nwrddc02.gnilink.net...
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

Jul 19 '05 #5
On Tue, 21 Oct 2003, Roger wrote:
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
************************************************** ***

Jul 19 '05 #6

"Roger" <gn****@hotmail.com> wrote in message
news:Kw*******************@nwrddc01.gnilink.net...
Dave wrote:
"Roger" <gn****@hotmail.com> wrote in message
news:xg*******************@nwrddc02.gnilink.net...
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!


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!

Jul 19 '05 #7
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.
Jul 19 '05 #8

"Dave" <be***********@yahoo.com> wrote in message
news:cM2lb.61363$La.30402@fed1read02...

"Roger" <gn****@hotmail.com> wrote in message
news:Kw*******************@nwrddc01.gnilink.net...
Dave wrote:
"Roger" <gn****@hotmail.com> wrote in message
news:xg*******************@nwrddc02.gnilink.net...

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


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!


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...
Jul 19 '05 #9
Roger <gn****@hotmail.com> wrote in message
news:gR*******************@nwrddc01.gnilink.net...

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

Jul 19 '05 #10

"Roger" <gn****@hotmail.com> wrote in message
news:xg*******************@nwrddc02.gnilink.net...
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!
Jul 19 '05 #11
Roger <gn****@hotmail.com> writes:
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. :-)

--
Micah J. Cowan
mi***@cowan.name
Jul 19 '05 #12


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


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
kb******@gascad.at
Jul 19 '05 #13
Roger <gn****@hotmail.com> spoke thus:
Actually my instructor used void main().


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)cyberspace.org | don't, I need to know. Flames welcome.
Jul 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Goodyear | last post by:
At the moment im experimenting with ideas in C++ and would really like to solve the following, please please help. Sorry i dont even know what the subject is this would come under? :( Sorry if...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
28
by: kfrost | last post by:
I know this is probably simple but I have a C# form and the class for the form is called sbaSynch. I have a textbox name txtServerName. I'm creating a class to manipulate XML functions so I...
15
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. How can one get the name of the current project and the current class? This is the situation. Suppose there is a project called "P1".
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
11
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at...
5
by: Anan18 | last post by:
Hello sir, I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test...
1
by: DarkGiank | last post by:
Hi, im new to csharp and im trying to create a class that can change the application database without no rewriting all connection code... but cause some reason it is not working... it tells me that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.