On Tuesday, January 1, 2013 12:05:31 PM UTC-8, shady wrote: > > Why do we use pointers at all considering space as a factor.... other than > allocating memory dynamically does it have any other use ? > > to store an integer > (int *) = 8 bytes, and while making it point to an integer location would > cost another 4 bytes, total = 12 bytes ... compared to directly taking > (int) = 4 bytes >
Well.. you don't necessarily need them for speed or for dynamic management of memory or for most things. However, it depends on what language you are using. Keep in mind that the term 'pointer' is used in slightly different ways depending on the language you are using. There are algorithms where pointers may be quite useful and there are algorithms where they make the code much more complicated and provide no real benefit. One example where they 'might' be useful would be in the solution of a set of linear equations where the coefficient matrix which represents the values in the equations is sparse (ie. lots of zeros). You could use a very simple solution scheme where you store all of your coefficients in a very simple rectangular matrix. In this case, the use of pointers will get you nowhere. If your equations aren't too big, the use of pointers here will probably make your algorithm slower. However, for large equations, your memory usage may be off the charts (say 1 million equations with 1 million coefficients per equation). Now, since the majority of your coefficients are going to be zeros... you can save a LOT of memory by not having to allocate memory for each coefficient. Your solution algorithm will be more complicated now. However, by using pointers to point at the non-zero coefficients, you'll save a lot of memory and you'll also cut down on the actual math your algorithm has to perform. Pointers aren't necessarily needed here either, though, they often are used in this situation. So... in this case, the pointers make your problem solvable. Though, you still have to deal with a more complicated algorithm, it's worth it simply because a simpler algorithm just isn't tractable. As another example.... think of Linked List. These can be built without pointers also. However, they tend to be simpler with pointers. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
