474,015 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1645

"Roger" <gn****@hotmail .com> wrote in message
news:xg******** ***********@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 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******** ***********@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 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******* ************@nw rddc02.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******** ***********@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
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******** ***********@nwr ddc01.gnilink.n et...
Dave wrote:
"Roger" <gn****@hotmail .com> wrote in message
news:xg******** ***********@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 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.6136 3$La.30402@fed1 read02...

"Roger" <gn****@hotmail .com> wrote in message
news:Kw******** ***********@nwr ddc01.gnilink.n et...
Dave wrote:
"Roger" <gn****@hotmail .com> wrote in message
news:xg******** ***********@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 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******** ***********@nwr ddc01.gnilink.n et...

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

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

Similar topics

1
1656
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 this is elsewhere in the newsgroup & please reply with links to threads or something? (I dont know what to look for :( ) (Terms are prob. all wrong as well :( ) The problem: I have a data structure represented by a class. Use a date class as an
13
2407
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. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
28
4404
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 added a class to project and it's named XmlApi(). In the XmlAPI() class I have simple code as following XmlAPI() { string str = "Some Text";
15
2014
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
2149
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 property and 2 methods, along with a constructor (where you use New(), I think, yes?). So, I decided to create a class that represents a die, which can be rolled - using a Roll() method - and which has a property to find out the value on the face of...
9
2111
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, this is not a homework assignment. In fact it was a question on my class midterm that everyone bombed. Unfortunately we never covered this in class prior to the midterm. Anyway, please help if you would be so kind. Here is what I have. I...
6
4048
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 types of objects (classes) can only be defined in Class modules.
11
2462
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 the point is at the end of the program. //week 6 Inventory3 // by Tina Jones // IT 215 // october 07/2007
5
6625
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 program to check the correctness of the sequence class. I cannot figure out what the problem is and keep looking back and forth at the textbook and its not helping, can someone please please help me , i cannot figure out the errors in the...
1
2786
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 im not creating the object but im doing it,,, please help im a newbie to c# using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SQLite; using MySql.Data; using MySql.Data.MySqlClient; using...
0
10462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10282
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12013
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10217
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7766
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6571
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3900
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.