8 Replies - 64053 Views - Last Post: 22 July 2010 - 06:27 PM Rate Topic: -----

#1 Codebug   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Why use pointers? What's the point?

Post icon  Posted 19 July 2010 - 02:04 PM

I think my title pretty much speaks for itself. I don't know much C or C++ at all, but I am definitely interested in learning. Up until now, I have programmed exclusively in java and as a result, I haven't had to worry about pointers much. After a little research, I've determined what pointers are, a variable that holds the address of another variable. Sounds cool, but why is this actually useful? Why would you need to do this? What purpose do pointers serve? I don't want to come across sounding like I hate pointers. I know they are obviously important and I am sincerely curious to know what functional purpose they serve.

Thanks

Is This A Good Question/Topic? 1
  • +

Replies To: Why use pointers? What's the point?

#2 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Why use pointers? What's the point?

Posted 19 July 2010 - 02:38 PM

If you have no reason to use a pointer, then don't use one; Chances are that if you've not come across a situation where using them will make your life easier, then the explanation of "why they're useful" will make little sense to you.

Java uses pointers behind-the-scenes in the form of references, and also iterators to a certain extent (A pointer in C++ is a kind of random-access iterator). If you're familiar with the "for each" syntax in java - i.e. for(object o : my_collection), the o is a reference-to (pointer-to) an object within my_collection; the caveat is that the Java for-each loop does not permit you random access, it only allows you to 'step-over' each object from the beginning to end.

If you're learning C++ rather than C, then don't worry too much about pointers straight away, since they're significantly less useful to a complete beginner than in 'C' code (They're generally indispensible in C for almost everything). You'll be better off getting to grips with the basics, and feeling your way around the differences between Java-vs-C++; (especially the standard libraries) before you'll reach a stage where pointers are actually useful to you.


The random-access iterator comparison ought to make more sense when you begin to play around with vectors (Something which should hopefully appear early on in your course/book - a vector in C++ is similar to an ArrayList in Java); If you can figure out how to use a vector in conjunction with a vector-iterator, then you're 99% on your way to understanding pointers.

This post has been edited by Bench: 19 July 2010 - 02:51 PM

Was This Post Helpful? 1
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Why use pointers? What's the point?

Posted 19 July 2010 - 02:39 PM

there are tons of use's for them, i find them difficult to use because i have a hard time grasping what all they can do, dynamic memory allocation is something i'm striving to understand, say you declare a varible using the "new" key word, the function it was declared in is not where that memory is going to be accessed form, so you set and return a pointer to that newly allocated space so it dose not become lost in memory. like i said there are tons of uses, look at linked lists or try to look at how stl containers work. it's all littered with pointers.
Was This Post Helpful? 1
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why use pointers? What's the point?

Posted 19 July 2010 - 02:40 PM

*
POPULAR

I think that everyone goes though this phase of not really understanding the purpose of pointers -- and truth to be known in C++ you will use references (which are very closely related to pointer but kind of hide the "address" part from you).

So you know that a pointer is a variable that holds the address of "something" in memory, it might be another variable, it might be a buffer, it might be the entry-point into a function.

So lets start by talking about "pass by reference":

There are (basically) two ways that arguments get passed to functions:

Pass-by-value where a copy of the variable is pushed onto the stack for the function to use.
Pass-by-reference where the address of a variable is pushed onto the stack for the function to use.

So the first immediately obvious point is, if you have a large structure, if you pass it by value, the computer has to push a copy of it onto the stack -- wasting both time, and stack-space! But if you just pass over the address, then you don't need to make a big copy and you only push an address onto the stack.

Another point about pass-by-reference, is that when the function modifies the structure, those changes persist outside of the function, BUT when you pass by value, when the function exits, its "copy" of the structure is lost -- so passing by reference lets a function make changes whatever gets passed to it. This can be used to return values, to manipulate data in arrays/buffers/structures etc.

Next, pointers allow you to make very abstract data structures. Take for example a simple little array type:

struct MyArray {
int* thearray;
int size;
}


Here I can use the pointer to allocate a block of memory for MyArray::thearray point to. So lets say that I would like to change the size of the array... well I can just alloate a new block of memory, copy over the old data, and then update my pointer "thearray" with the new address.

Pointers are used to make linked list (each node has the address of the next node so we know how to find it), trees (each node has a link to the left and right nodes) and many other data structures.


Java of course uses pointers too -- but java hides them from you (pointers are a vital part of a running program as it need to be able to find things and the only way to find something is via its address) -- C/C++ are meant to be kind of low level languages that allow you to dive much deeper into the system than very high level languages like Java.

For example (this is more old DOS programming than modern OS but its just an example) there is a buffer in the computer where the keyboard stores keystrokes until they get read by OS. That buffer has a fixed address (hardware generally uses fixed addresses for its IO) -- so with a pointer you can directly address and access the keyboard buffer. True with modern OS's things change and the OS will not let you address the keyboard's buffer directly (unless your program is a driver), BUT the os is constantly sending you addresses on where to find data, or asking you for an address where it can store the data you are asking for.

For OOP: When you create a class you are not really creating a "mini-program" that the computer passes arround (I used to think that if you had a function foo() then there was a new copy of the code for foo() for each object you created -- I was wrong). When you have a class function the compiler generates one copy of that code, but it also adds a hidden argument to the function.

I write:
class Foo{
int v;
public: 
int foo(int a) { return a * v; }
}


The compile generates something along the lines of:
struct Foo_ {
int v;
};

int foo_(Foo_* this, int a) { return a * this->v; }


the compiler added a "hidden" parameter to pass in a pointer to the instance of the "class" (read: structure containing the data i.e. state of the object) and the function uses that pointer to access the particular data for that object.

so this: myFoo.foo(10); // is really the call foo_(&myFoo, 10)

So if you stop and really think about pointers they are very helpful and honestly a computer could not work without them. A language CAN hide them from you, but C/C++ is not about "hiding" what the computer is doing or can do, if you don't want to deal with pointers, use VB or Java (though even there you nearly always end up needing to deal with pointers).
Was This Post Helpful? 23
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5227
  • View blog
  • Posts: 14,010
  • Joined: 18-April 07

Re: Why use pointers? What's the point?

Posted 19 July 2010 - 02:45 PM

Number one reason to use them, memory management. They can point to large data structures that can take up a significant amount of resources. I would make sure you learn them because they do still come in use for C++. You will see the main advantage when you study passing parameters by value or reference and when it comes to handling algorithms on larger objects. For instance, you don't want to be moving around large structures when doing a sort. Instead you can use a sort on a list of pointers to objects instead. This increases efficiency and overall speed because you are not copying around large objects.

You may see pointers when you are doing something like a linked list. The list of nodes are linked together, but the objects in memory are not contiguous. :)
Was This Post Helpful? 3
  • +
  • -

#6 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Why use pointers? What's the point?

Posted 19 July 2010 - 03:00 PM

there are some tutorials on pointers here that might help you understand, i had one of my first "now i get it" moments with pointers after finely understanding linked lists. i had some others while looking at dynamic memory allocation, i started to get how destruction/construction worked(delete/new) and why to use pointers for this, i also realized why a 32bit system can only use a little under 4gb of ram, it's because it can only use numbers up ULONG_MAX, ULONG_MAX number of bytes comes out to 3.99gb, how ever 64bit numbers can index some ungodly amount of data. look at how memory works, i started to understand more when i was messing around with a program called "Cheat Engine" its a memory scanner that allows you to change the value held at an address, it comes with a little tutorial thing, it goes over how pointers work at a pretty low level witch helped me to understand them better.
Was This Post Helpful? 0
  • +
  • -

#7 godmoktail   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 10-May 08

Re: Why use pointers? What's the point?

Posted 19 July 2010 - 03:45 PM

Also, in C++, Polymorphism is allowed due to pointers.
Edit: Or should I say, polymorphism is achieved through pointers.

This post has been edited by godmoktail: 19 July 2010 - 03:46 PM

Was This Post Helpful? 1
  • +
  • -

#8 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Why use pointers? What's the point?

Posted 19 July 2010 - 03:54 PM

Because of pointers in C++ we can make some interesting data structures, like linked lists, and binary trees, etc.

You might check out C++ references too.
Was This Post Helpful? 0
  • +
  • -

#9 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Why use pointers? What's the point?

Posted 22 July 2010 - 06:27 PM

In a nutshell, because pointers allow you to access memory addresses and pass by reference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1