newbie -- trying to use new

P

pauldepstein

Could someone explain why the following code gives two different
addresses? It's as simple as I can make it.

I expected Addresses A and B to be the same but they returned different
values. I used the Bloodshed Dev-Cpp compiler

Thank you very much for your help.

Paul Epstein

#include <iostream>
using namespace std;

class tritree {
public:
int* nodeArray;
tritree();
};

tritree::tritree()
{

int *nodeArray = new int[1];
cout << "tritree::tritree nodeArray" << nodeArray << endl;
} // Address A

int main()

{
tritree examplett;
cout << "nodeArray in main "<<examplett.nodeArray; // Address B

system("PAUSE");
return 0;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
Could someone explain why the following code gives two different
addresses? It's as simple as I can make it.

I expected Addresses A and B to be the same but they returned different
values. I used the Bloodshed Dev-Cpp compiler

Thank you very much for your help.

Paul Epstein

#include <iostream>
using namespace std;

class tritree {
public:
int* nodeArray;
tritree();
};

tritree::tritree()
{

int *nodeArray = new int[1];

This declares a local variable 'nodeArray' and initializes it.

It's not the member of the same name.
 
V

Victor Bazarov

Could someone explain why the following code gives two different
addresses? It's as simple as I can make it.

I expected Addresses A and B to be the same
Why?

> but they returned different
values. I used the Bloodshed Dev-Cpp compiler

It doesn't matter here, in most cases.
Thank you very much for your help.

Paul Epstein

#include <iostream>
using namespace std;

class tritree {
public:
int* nodeArray;
tritree();
};

tritree::tritree()
{

int *nodeArray = new int[1];
cout << "tritree::tritree nodeArray" << nodeArray << endl;

You've declared a _local_ variable 'nodeArray' here, and initialised it,
and printed its value. The _member_ variavle 'nodeArray' is *hidden* as
soon as you declare a local variable with the same name.
} // Address A

int main()

{
tritree examplett;
cout << "nodeArray in main "<<examplett.nodeArray; // Address B

Here you print the _member_ variable 'nodeArray'.
system("PAUSE");
return 0;
}

V
 
O

Old Wolf

Could someone explain why the following code gives two different
addresses? It's as simple as I can make it.

#include <iostream>
using namespace std;

class tritree {
public:
int* nodeArray;
tritree();
};

tritree::tritree()
{

int *nodeArray = new int[1];

Now you have two variables named 'nodeArray' -- the one in the
class, and another one local to the constructor.
cout << "tritree::tritree nodeArray" << nodeArray << endl;

This prints the value of the nodeArray local to the constructor...
} // Address A

int main()

{
tritree examplett;
cout << "nodeArray in main "<<examplett.nodeArray; // Address B

.... and this prints the address of the nodeArray that is a member
of the class. (Actually this causes undefined behaviour because
you have never initialized that variable).

Perhaps it would be clearer to you if you wrote your constructor
as:

tritree::tritree()
{
int *foo = new int[1];
cout << "tritree::tritree nodeArray" << foo << endl;
} // Address A

(which is exactly equivalent code).
system("PAUSE");

Try and avoid doing that, as it only works in some operating
systems. Instead:

getchar();
 
J

John Harrison

Could someone explain why the following code gives two different
addresses? It's as simple as I can make it.

I expected Addresses A and B to be the same but they returned different
values. I used the Bloodshed Dev-Cpp compiler

Thank you very much for your help.

Paul Epstein

#include <iostream>
using namespace std;

class tritree {
public:
int* nodeArray;
tritree();
};

tritree::tritree()
{

int *nodeArray = new int[1];

This is not what you meant to write, it should be

nodeArray = new int[1];
cout << "tritree::tritree nodeArray" << nodeArray << endl;
} // Address A

int main()

{
tritree examplett;
cout << "nodeArray in main "<<examplett.nodeArray; // Address B

system("PAUSE");
return 0;
}

You had two variables called nodeArray, one in your class and one in
your constructor. Don't forget, compiler does exactly what you say, not
what you meant to say.

john
 
R

Ron Natalie

class tritree {
public:
int* nodeArray;
tritree();
};

tritree::tritree()
{

int *nodeArray = new int[1];
cout << "tritree::tritree nodeArray" << nodeArray << endl;
} // Address A

It is almost always a mistake for newbies to use new. This is a fine
example. Why are you allocating an array with new? If this is always
a one element array, use
int nodeArray[1];
in the class definition, if it needs to be variable size, use a vector:

class tritree {
public:
std::vector<int> nodeArray;
tritree() : nodeArray(1) { }
};
 
P

pauldepstein

Old said:
Try and avoid doing that, as it only works in some operating
systems. Instead:

getchar();

O.k. I did use getchar(); It's not quite the same -- system("PAUSE");
gives the useful message "Press any character to continue."

Perhaps I should just insert "Press any character..." via a cout
statement?

Thank you,

Paul Epstein
 

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,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top