Archived

This topic is now archived and is closed to further replies.

Chigaimasu

Pointers- - Pointless?

Recommended Posts

Chigaimasu    122
That''s certainly how I feel right now. I may be proven wrong soon, but, personally, I just can''t see a practical application for pointers. I understand the syntax, I understand some of the more ''practical'' algoritims; I just don''t see how _I_ could use them. Anyone who could tell me otherwise (with examples!) would definetly be a help. Thanks!

Share this post


Link to post
Share on other sites
falkone    444
There are many points to pointers... (har har har..)

The big one is using pointers to change values passed to a function. When a variable is passed normally, a copy of the variable is made and the data is copied into it. With a pointer, a reference to the variable is created. Becuase passing by value creates a copy, changing the new variable will not change the one that was passed. With pointers, that variable can be changed. The other advantage to a reference as opposed to byvalue passing is that if you have a 12mb data structure, you don''t have to copy 12mb every time you call the function... you can just access it. Pointers are also useful for getting directly at the bits.. like if you want to access certain system information (such as the video buffer).. you''ll need a pointer to store the address to access it.. though far pointers used like that are more or less obsolete..

Share this post


Link to post
Share on other sites
rypyr    252
I disagree that the big use of pointers is to change a value of a variable passed as a pointer. This can be done with a reference.

The 2 big uses of pointers is:

1) dynamic memory allocation . Allocate memory at runtime returns a pointer to that memory location.

2) polymorphism . This is the ability for a pointer to point to an object of a subtype at runtime.

Regards,
Jeff

Edit: Added #2.

[edited by - rypyr on April 29, 2003 11:10:02 AM]

Share this post


Link to post
Share on other sites
drslush    122
Without pointers (in C/C++ at least), you''d have no way of making some of the fundamental constructs of computer science. For example, linked lists or binary trees. However, there are those who agree with you that pointers are pointless. That was one of the principles used in designing Java, the elimination of pointers.

Share this post


Link to post
Share on other sites
bastard2k5    238
how about dynamic memory allocation, that would be one example of where pointers will cease to be pointless. I also wouldn''t really want to create a linked list without pointers, even Java has pointers in the language of sorts(this, for example), the programmer just cannot actually create pointers of their own. so that you don''t ever have to deal with pointers. In C, before C++ existed, I think pointers were the only way to pass variables into a function and be able to change the values, for say a swap() function, that will actually swap the two variable''s values.Although I think C++''s introduction of references got rid of a lot of the use for pointers in the language, they still are very useful constructs.

Share this post


Link to post
Share on other sites
rypyr    252
Example of dynamic memory allocation:


    
class StrBuf {
char* m_pBuf;
public:
StrBuf(int len) { m_pBuf = new char[len]; }
~StrBuf() { delete[] m_pBuf; }
};


Here is the typical example of polymorphism that people often show:


  
class IShape {
public:
virtual void draw() = 0;
};

class Circle : public IShape {
public:
void draw() { draw circle here }
};

class Rectangle : public IShape {
public:
void draw() { draw rectangle here }
};

class Polygon : public IShape {
public:
void draw() { draw rectangle here }
};

void main() {
IShape* vecShapes[3];

vecShapes[0] = new Circle();
vecShapes[1] = new Rectangle();
vecShapes[2] = new Polygon();

// draw all shapes

for(int loop = 0; loop < 3; ++loop) {
vecShapes[loop]->draw();
}

for(int loop = 0; loop < 3; ++loop) {
delete vecShapes[loop];
}
}


[edited by - rypyr on April 29, 2003 11:21:53 AM]

Share this post


Link to post
Share on other sites
Dalik    122
with a reference you cant change the address you assign to it.
So its a use once and if your done with it then you cant use it again or bad stuff happens ref = address1 you cant change it to ref = address2 doesnt work.

With a pointer you can reassign address to that variable and use it over and over.

That is the difference between them.

Pointers make your coding life a lot easier and you will end up using them more and more. Its very normal for people to ask what is the use for them, but soon enough you will see why pointers and regerences are so important.

Share this post


Link to post
Share on other sites
stubble    158
rypyr: Actually that sort of polymorphism can be achieved with references too. You can reference a subtype just like you can with a pointer. The only constraint being that you can''t re-assign a reference like you can a pointer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The biggest use for pointeres in my opinion is dynamic memory allocation. In a game, for example, a newbie might do a CEnemyMonster aMonster[30]; for the monsters, a CGunShot aProjectile[50]; for the bullets, or something similar. When the best way to do this would be to dynamicly allocate the objects within the array, so you can have any number of them, without using memory for useless objects.

Trust me you WILL you it alot. (not to mention all the functions which demand a pointer as a parameter)

Share this post


Link to post
Share on other sites
RuneLancer    253
You don''t need pointers at all; even for dynamic memory allocation! Just create a huge, massive honkin'' array of, say, 10000 entries! :D Problem solved, right? :D

(yes, this is sarcastic. ;P)

Share this post


Link to post
Share on other sites
stubble    158
Dynamic memory allocation isn''t the bees knees tho. It can degrade application performance (when you allocate/deallocate memory on the fly - memory has to be dynamicaly found/returned = performance hit if you have lots of objects being created and destroyed every frame). Also compilers can optimise away virtual table lookups on staticaly allocated objects - so if you know that your final app will use subclass x in a tight loop then it can be benificial to create this object on the stack - rather than dynamically.

Share this post


Link to post
Share on other sites
drslush    122
Sure, allocations/deallocations can slow down an app, but I''d like to see you try to statically allocate a BSP tree ;-D No matter how you dice it dynamically allocated memory speeds up tasks.

I don''t even understand why anyone is even talking about comparing the two... Its apples to oranges most of the time.

Share this post


Link to post
Share on other sites
AzCoder    150
Every variable in every app is stored in memory. Therefore all variables have an address. A pointer is nothing more than a variable that holds an address to something else. There are many benefits of this approach that have already been enumerated. But for the most part, it all revolves around control. Languages that do not provide pointers still store variables in memory. They just do not provide a mechanism to manipulate those variable via a dereferenced address.

Pointers are a good thing. They provide more flexibilty. Use them if you want.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
As the previous poster said, pointers are a necessary construct in providing an analog to the way that the computer actually operates. Without them, you are depriving the programmer of this field of control over the program and the system in consequence. This makes sense in languages like Visual Basic or Java, where the focus is more on application development rather than system-related tasks like writing drivers or controlling hardware on a basic level. In a system programming language like C/C++, pointers, in that they represent the basic functionality of the machine, are a necessity.

Share this post


Link to post
Share on other sites
directrix    181
My advice is that if you can''t see any practical application for them don''t use them. As your programs get more advanced you''ll gradually see their uses and begin to use them. If you don''t, you need to start writing more advanced programs

btw, don''t take my advice if the programs your talking about are for school in which case you should use them since you might fail if you don''t.

digital radiation

Share this post


Link to post
Share on other sites
stubble    158
drslush: I agree totaly - dynamic memory alloc is there for a reason - there are datastructures etc which depend on it etc etc - but a lot of people use it for the sake of using it - when in many cases creating things on the stack would be a much better option.

Share this post


Link to post
Share on other sites
Eelco    301
well, another question from me. what the **** are references good for? afaik its just a const pointer, with some stupid constrains to it. is there actually one task references can do faster/better than pointers? exuse my ignorance if there is: i can definitly see the use of pointers, im not a beginner, but references, why? and i dont take extra security against stray pointers as an argument: just learn to code properly!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Eelco
well, another question from me. what the **** are references good for? afaik its just a const pointer, with some stupid constrains to it. is there actually one task references can do faster/better than pointers? exuse my ignorance if there is: i can definitly see the use of pointers, im not a beginner, but references, why? and i dont take extra security against stray pointers as an argument: just learn to code properly!


this link should answer your question:
http://www.embedded.com/story/OEG20010311S0024

Share this post


Link to post
Share on other sites
rypyr    252
"just learn to code properly"...that''s a good one...

If you dislike references so much, don''t use them, it''s that simple. You don''t need to get upset about them.

However, the main argument for references is to ensure that what you''re "pointing at" is valid and not NULL. It''s very hard to get around this if you''re using references.

If you don''t accept that argument, the only other ones I can think of are: 1) it''s easier to declare a const reference (i.e. a pointer to an object that you cannot modify) than it is for pointers and 2) you can still use the dot notation with references.

Generally, when programming interfaces, it is better to use const references wherever possible.

Regards,
Jeff

Share this post


Link to post
Share on other sites
fizban75    130
pointer vs reference:


f1(A& a) {
// a is guaranteed to "point" to a valid object
}

f2(A* a) {
// a is not guaranteed to point to anything, therefore I would have to check whether it's valid first before using it (extra overhead)
}


Now do you see the benefit of references?

[edited by - fizban75 on April 30, 2003 1:44:30 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by rypyr
"just learn to code properly"...that''s a good one...

If you dislike references so much, don''t use them, it''s that simple. You don''t need to get upset about them.

However, the main argument for references is to ensure that what you''re "pointing at" is valid and not NULL. It''s very hard to get around this if you''re using references.

If you don''t accept that argument, the only other ones I can think of are: 1) it''s easier to declare a const reference (i.e. a pointer to an object that you cannot modify) than it is for pointers and 2) you can still use the dot notation with references.

Generally, when programming interfaces, it is better to use const references wherever possible.

Regards,
Jeff


From the link that was posted earlier
"Some people take the fact that a reference can''t be null to mean that references are somehow safer than pointers. They may be a little safer, but I don''t think they''re a lot safer. Although a valid reference can''t be null, an invalid one can be null. In fact, there''s a ton of ways programs can produce invalid references, not just null references. For example, you can define a reference so that it refers to the object addressed by a pointer, as in:


int *p;
...
int &r = *p;

If the pointer happens to be null at the time of the reference definition, the reference is a null reference. Technically, the error is not in binding the reference, but in dereferencing the null pointer. Dereferencing a null pointer produces undefined behavior, which means lots of things could happen, and most of them aren''t good. It''s likely that, when the program binds reference r to *p (the object that p points to), it won''t actually dereference p. Rather, it will just copy the value of p to the pointer that implements r. The program will keep running only to have the error manifest itself more overtly sometime later during program execution. Oh joy."

Share this post


Link to post
Share on other sites
kdogg    204
It''s certainly possible to have invalid references in your program. They are not necessarily any safer than using pointers. To me, the advantage to references is that they''re easier to read. Pointers tend to make you have 100 ''*''s in your program. There are things you can only do with references, but there are also things you can only do with pointers. After gaining a good understanding of how your computer stores data, pointers are rather intuitive and references are more abstract. Usually, it''s obvious when you should use pointers, when you should use references, and when it doesn''t matter which one you use.. In the last case, it''s just a matter of preference.

Share this post


Link to post
Share on other sites
Eelco    301
well im not upset about them really... but maybe a little, true. if i read any docs about them, all authors seem completely brainwashed by refereces, while failing to explain to me why theyre so enthousiastic about them. almost like they got money to propagade them..

and besides, i think the last post has the most important argument for me: theyre less intuitive than pointers. i still dont get witch way compilers handle references, witch i dont like much: i like to know what im doing to my computer.

and whats the thing(s) mentioned you can only do with a reference and not with a pointer?

Share this post


Link to post
Share on other sites
kdogg    204
quote:

and whats the thing(s) mentioned you can only do with a reference and not with a pointer?


I''m not positive, but it seems like there are some instances when you must use references to overload an operator. At least, that what i was refering to. There are probably others.

Share this post


Link to post
Share on other sites