Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why pointers?

Reply
Thread Tools

Why pointers?

 
 
saneman
Guest
Posts: n/a
 
      02-13-2008
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?
 
Reply With Quote
 
 
 
 
pastbin@gmail.com
Guest
Posts: n/a
 
      02-13-2008
[8.6] When should I use references, and when should I use pointers?
http://www.parashift.com/c++-faq-lit...s.html#faq-8.6
 
Reply With Quote
 
 
 
 
saneman
Guest
Posts: n/a
 
      02-13-2008
wrote:
> [8.6] When should I use references, and when should I use pointers?
> http://www.parashift.com/c++-faq-lit...s.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;
}
 
Reply With Quote
 
Szabolcs Ferenczi
Guest
Posts: n/a
 
      02-13-2008
On Feb 13, 10:57*pm, saneman <y...@dd.com> wrote:

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


Think about polymorphism in C++.

Best Regards,
Szabolcs
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      02-13-2008
On 2008-02-13 22:57, saneman wrote:
> 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;
}

--
Erik Wikström
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      02-14-2008
saneman <> wrote:
> wrote:


> > [8.6] When should I use references, and when should I use pointers?
> > http://www.parashift.com/c++-faq-lit...s.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?
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      02-14-2008
saneman wrote:
> 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.
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      02-14-2008
On Feb 13, 5:11 pm, saneman <y...@dd.com> wrote:
> past...@gmail.com wrote:
> > [8.6] When should I use references, and when should I use pointers?
> >http://www.parashift.com/c++-faq-lit...s.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:

void test(std::list<int>& l) { ... }

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

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      02-14-2008
On Feb 13, 5:24 pm, Szabolcs Ferenczi <szabolcs.feren...@gmail.com>
wrote:
> On Feb 13, 10:57 pm, saneman <y...@dd.com> wrote:
>
> > But does anyone have an idea to a minimal example where its impossible
> > to avoid pointer?

>
> Think about polymorphism in C++.
>
> Best Regards,
> Szabolcs


Polymorphism works quite nicely with references too.
 
Reply With Quote
 
saneman
Guest
Posts: n/a
 
      02-14-2008
Daniel T. wrote:
> saneman <> wrote:
>> wrote:

>
>>> [8.6] When should I use references, and when should I use pointers?
>>> http://www.parashift.com/c++-faq-lit...s.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'.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57