Why pointers?

S

saneman

I have written C++ code for some time now using both pointers and
references.

I was asked what the point was in using pointers and could not give a
short an explanatory example.

I would say that you use a pointer when data are updated across function
calls.

But does anyone have an idea to a minimal example where its impossible
to avoid pointer?
 
S

saneman

[8.6] When should I use references, and when should I use pointers?
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6

Thanks but I was looking for some code sample that illustrates the
differences. In the below code 'test' takes a pointer to a list. But I
never use 'new' to make it work. Why would it ever be necessary to use
'new' when I can just pass the address to the list to 'test' using '&'?


#include<iostream>
#include<list>
void test(std::list<int>* l) {
l->push_back(555);
}

int main() {

std::list<int> l;
std::list<int>::iterator list_it;
l.push_back(1);
l.push_back(2);
l.push_back(3);

for (list_it = l.begin(); list_it != l.end(); list_it++) {
printf("%d\n",*list_it);
}
std::cout<<std::endl;
test(&l);
for (list_it = l.begin(); list_it != l.end(); list_it++) {
printf("%d\n",*list_it);
}
return 0;
}
 
S

Szabolcs Ferenczi

But does anyone have an idea to a minimal example where its impossible
to avoid pointer?

Think about polymorphism in C++.

Best Regards,
Szabolcs
 
E

Erik Wikström

I have written C++ code for some time now using both pointers and
references.

I was asked what the point was in using pointers and could not give a
short an explanatory example.

I would say that you use a pointer when data are updated across function
calls.

But does anyone have an idea to a minimal example where its impossible
to avoid pointer?

In any situation where you need to be able to "reseat" the reference. On
example would be walking through the nodes in a linked list without
using recursion, something like:

node* n;
while (n != 0)
{
// do something with n
n = n->next;
}
 
D

Daniel T.

saneman said:
(e-mail address removed) wrote:
[8.6] When should I use references, and when should I use pointers?
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6

Thanks but I was looking for some code sample that illustrates the
differences. In the below code 'test' takes a pointer to a list. But I
never use 'new' to make it work. Why would it ever be necessary to use
'new' when I can just pass the address to the list to 'test' using '&'?

That's a change of subject. First you ask "why pointers?", now you are
asking "why new?".

What is it that you really want to ask?
 
J

Jeff Schwab

saneman said:
I have written C++ code for some time now using both pointers and
references.

I was asked what the point was in using pointers and could not give a
short an explanatory example.

I would say that you use a pointer when data are updated across function
calls.

But does anyone have an idea to a minimal example where its impossible
to avoid pointer?

You can have a vector of pointers, but not a vector of references.

As a particular case of the "reseating" issue already noted by Erik,
consider a class that may (according to your design philosophy) have a
member either a reference member or a pointer member. If the target
object of the pointer or reference (the referee?) is not yet available
when the referring (pointy?) object is constructed, then a reference is
out of the question. Code like the following even flags a compile-time
error from g++:

struct S {
S() { }
void set_i(int*);
private:
int& i_;
};

g++ -ansi -pedantic -Wall -c -o main.o main.cc
main.cc: In constructor 'S::S()':
main.cc:2: error: uninitialized reference member 'S::i_'
make: *** [main.o] Error 1

A pointer, by contrast, can be temporarily initialized to a reasonable
default value, and later assigned a more permanent value.

A pointer is an object in its own right, with its very own address in
memory. A reference is just a way of referring to an existing object,
and may or may not require run-time support from pointer-like entities.
You can have a reference to a pointer, but not a reference to a reference.
 
S

Salt_Peter

[8.6] When should I use references, and when should I use pointers?
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6

Thanks but I was looking for some code sample that illustrates the
differences. In the below code 'test' takes a pointer to a list. But I
never use 'new' to make it work. Why would it ever be necessary to use
'new' when I can just pass the address to the list to 'test' using '&'?

What makes you think that pointers and new are related in any way?
To get back on topic, isn't the code below an example where a
reference is preferred over a pointer?
#include<iostream>
#include<list>
void test(std::list<int>* l) {
l->push_back(555);

}

Note the difference between:

std::list<int>* l; // pointer can be reseated
const std::list<int>* c_l; // same, any const list will do
std::list<int>* const c_l; // const pointer, can't be reseated, list
is mutable
const std::list<int>* const c_l_c;

Thats is, the above function should really have the following
signature if you absolutely must use a pointer:

void test(std::list<int>* const l) { ... } // pointer can't be
reseated

Which then makes the following better. Its intentions are clear to the
user:
 
S

saneman

Daniel said:
saneman said:
(e-mail address removed) wrote:
[8.6] When should I use references, and when should I use pointers?
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6
Thanks but I was looking for some code sample that illustrates the
differences. In the below code 'test' takes a pointer to a list. But I
never use 'new' to make it work. Why would it ever be necessary to use
'new' when I can just pass the address to the list to 'test' using '&'?

That's a change of subject. First you ask "why pointers?", now you are
asking "why new?".

What is it that you really want to ask?

Sorry for the confusion. I would like an example where its impossible to
avoid use 'new' (dynamic allocation).

The reason is that I often use pointers and references but seldom the
keyword 'new'.
 
L

Lionel B

Try creating a linked list without pointers.

Sure it's possible... clunky, painful and probably inefficient, but
possible (e.g. use arrays for storage and arrays of indices for access).
 
P

Pete Becker

Sure it's possible... clunky, painful and probably inefficient, but
possible (e.g. use arrays for storage and arrays of indices for access).

But given that it's hypothetically impossible to give such an example
(a Turing machine doesn't have pointers), examples that are clunky,
painful, and inefficient without pointers are good answers.
 
D

Daniel T.

saneman said:
Sorry for the confusion. I would like an example where its impossible to
avoid use 'new' (dynamic allocation).

The reason is that I often use pointers and references but seldom the
keyword 'new'.

You seldom use the keyword 'new'? Sounds like you have answered your own
question. Try to go back and do the same thing without the keyword. :)

In C++, you should be using 'new' very rarely.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top