basic question -- when a pointer is equal to 0

P

pauldepstein

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
address value.

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
 
A

Alf P. Steinbach

* (e-mail address removed):
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
address value.

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 )
{}



I'd be interested in your comments on
<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).
 
P

pauldepstein

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
question about the zero pointer.

Thank you,

Paul Epstein
 
K

Karl Heinz Buchegger

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

Greg

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
address value.

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.

See this link for more information about null pointers, 0 and the
proposed nullptr keyword:
http://www.research.att.com/~bs/N1488-nullptr.pdf

Greg
 
D

Dave Rahardja

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
 
B

ben

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top