By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,372 Members | 1,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,372 IT Pros & Developers. It's quick & easy.

basic question -- when a pointer is equal to 0

P: n/a
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

Nov 4 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* 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
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).

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

P: n/a
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

Nov 4 '05 #3

P: n/a
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
kb******@gascad.at
Nov 4 '05 #4

P: n/a
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
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

Nov 4 '05 #5

P: n/a
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

P: n/a
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
Nov 5 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.