470,815 Members | 1,256 Online

basic question -- when a pointer is equal to 0

I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They
construct a vector class which contains the variable vec, a float*
variable where the length of the array (number of components in the
vector) is given by the variable name veclength.

That is what I _do_ understand. What I don't understand is the
coding for the default constructor which includes vec=0;

What does it mean for a pointer to be equal to 0? Presumably this
means that the address of the pointer is 0? What is the significance
of this?

Here is my wild guess. The choice of 0 for the address is arbitrary
and the purpose is to see when the default constructor is operating.
The destructor contains the code if(vec!=0)delete [] vec;

My guess: the important thing is that the 0 in the default constructor
matches the 0 in the destructor. We don't need for this address to be
0 so long as the default address value matches with the destructor

I reiterate again that this is a guess (which is why I'm asking for
confirmation).

I'm actually a bit uncomfortable with the whole concept of assigning an
address to zero but perhaps I completely misunderstand what's going on.

Thank you,

Paul Epstein

Nov 4 '05 #1
6 2269
* pa**********@att.net:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They
construct a vector class which contains the variable vec, a float*
variable where the length of the array (number of components in the
vector) is given by the variable name veclength.

That is what I _do_ understand. What I don't understand is the
coding for the default constructor which includes vec=0;

What does it mean for a pointer to be equal to 0? Presumably this
means that the address of the pointer is 0? What is the significance
of this?

Here is my wild guess. The choice of 0 for the address is arbitrary
and the purpose is to see when the default constructor is operating.
The destructor contains the code if(vec!=0)delete [] vec;

My guess: the important thing is that the 0 in the default constructor
matches the 0 in the destructor. We don't need for this address to be
0 so long as the default address value matches with the destructor

I reiterate again that this is a guess (which is why I'm asking for
confirmation).

I'm actually a bit uncomfortable with the whole concept of assigning an
address to zero but perhaps I completely misunderstand what's going on.

0 is a special pointer value that doesn't point to anything and that can
be checked for.

It has its uses, but the use you describe is unnecessary and just makes
the class more complicated than necessary.

Instead they should just have a single constructor like

GrimoVector( std::size_t size = 0 )
: vec( new float[size] ), veclength( size )
{}

<url:
http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_03__alpha.doc.pdf>.

(Except that I forgot to add in a para explaining shortcomings of
'stringFrom' function in the middle there; I've now just added
parentheses in that representation, more code instead of more text).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 4 '05 #2
Sorry, I don't really understand your grimovector construction.

I'm a bit new to classes and therefore am rather dependent on the
constructions and conventions I have been exposed to.

The layout I'm familiar with is the absolutely most standard and basic

class vector{ private: variables... public functions...};

vector::vector { basic code for default constructor}

void vector::print {some basic printing function}

other basic function types declared above in the prototype.

It's hard for me to comment on your GrimoVector because I don't know
and understand those conventions.

Anyway, the good news is that you have completely cleared up my

Thank you,

Paul Epstein

Nov 4 '05 #3
pa**********@att.net wrote:

It's hard for me to comment on your GrimoVector because I don't know
and understand those conventions.

Grab your favourite C++ textbook and look up: "Initializer list"

--
Karl Heinz Buchegger
Nov 4 '05 #4
pa**********@att.net wrote:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They
construct a vector class which contains the variable vec, a float*
variable where the length of the array (number of components in the
vector) is given by the variable name veclength.

That is what I _do_ understand. What I don't understand is the
coding for the default constructor which includes vec=0;

What does it mean for a pointer to be equal to 0? Presumably this
means that the address of the pointer is 0? What is the significance
of this?

Here is my wild guess. The choice of 0 for the address is arbitrary
and the purpose is to see when the default constructor is operating.
The destructor contains the code if(vec!=0)delete [] vec;

My guess: the important thing is that the 0 in the default constructor
matches the 0 in the destructor. We don't need for this address to be
0 so long as the default address value matches with the destructor

I reiterate again that this is a guess (which is why I'm asking for
confirmation).

I'm actually a bit uncomfortable with the whole concept of assigning an
address to zero but perhaps I completely misunderstand what's going on.

Thank you,

Paul Epstein

The value 0 in the context of a pointer represents the null pointer
constant. The null pointer constant is a pointer value that is
guaranteed to point to no valid object.

The actual address that the null pointer references may not in fact be
memory address 0 for a given architecture; but the compiler will always
map the 0 to an appropriate invalid pointer address, if necessary. This
overloaded meaning of "0" is confusing; so it's likely that the next
revision of C++ will have a nullptr keyword that can be used for this
purpose.

proposed nullptr keyword:
http://www.research.att.com/~bs/N1488-nullptr.pdf

Greg

Nov 4 '05 #5
On 4 Nov 2005 05:12:18 -0800, pa**********@att.net wrote:
It's hard for me to comment on your GrimoVector because I don't know
and understand those conventions.

If you find the "vector" part of the example confusing, it is because the
example uses a vector, which is a container class defined in the STL (Standard
Template Library). You can think of it as a "better array" (although it is
much more than that), which will delete itself when it is destroyed.

-dr
Nov 5 '05 #6
> It has its uses, but the use you describe is unnecessary and just makes
the class more complicated than necessary.

Instead they should just have a single constructor like

GrimoVector( std::size_t size = 0 )
: vec( new float[size] ), veclength( size )
{}

In my opinion, the reason why the author chooses to use a pointer to a
vector instead of just a vector object is that he or she wants to delay
the construction of the vector object or to refer to an already
constructed vector object (for example, to facilitate content sharing
between multiple instances.) The zero initialization for the pointer,
therefore, has a valid ground, in my opinion nonetheless.

Ben
Nov 5 '05 #7