7 Replies - 936 Views - Last Post: 29 December 2009 - 05:08 PM Rate Topic: -----

#1 iCoCoA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 09

Concept vs Facts

Posted 29 December 2009 - 03:44 PM

I folks it's me again. I'm here with a simple question that I have been trying to find an answer to for about a year now.

WHY use a POINTER rather than declaring a new "regular" VARIABLE?

I just don't understand the point of pointers... I guess?

Before explaining, I do know how to declare pointers, and use them. But I just don't know why would I want to do that. I'm basically looking for an example where it would be better to use a pointer rather than declaring a new "regular" variable. Please in your responses include as detailed of examples as you can, I need a visual, because I've learned that reading has no effect on me.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Concept vs Facts

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: Concept vs Facts

Posted 29 December 2009 - 03:52 PM

From this PDF:

Quote

Why Have Pointers?
Pointers solve two common software problems. First, pointers allow different sections of code to share information easily. You can get the same effect by copying information back and forth, but pointers solve the problem better. Second, pointers enable complex "linked" data structures like linked lists and binary trees.

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

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

Re: Concept vs Facts

Posted 29 December 2009 - 04:09 PM

Well a pointer is the address of a block of memory, turns out this can be very useful.

For example pointers are used to pass arguments by reference. For example if you need to return more than 1 value...

void swap(int *a, int *b) {
int temp = *b;
*b = *a;
a* = temp;
}
Here pointers are used to swap two values... this could not be done using return values...

Pointers are also good for passing arrays -- do you really want to make a COPY of the entire array?

Pointers are good for passing in large structures -- again, do you really want to COPY a large structure?

Pointers are used to make dynamic structures like linked lists.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: Concept vs Facts

Posted 29 December 2009 - 04:19 PM

Also consider large objects. A "regular," or stack-allocated variable, is allocated on the stack (natch); a pointer's backing memory is allocated from the heap. The stack is a far more limited resource than the heap.
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Concept vs Facts

Posted 29 December 2009 - 04:25 PM

If you don't run into a problem where a pointer (a memory address) can help, then you have no reason to use one. The most common feature of a bad C++ program seems to be gross misuse of pointers in situations where there were no reasons to use them. In reality, "raw pointers" are alot less useful in C++ than you might think, unlike 'C' where they're used for most things, but the abstract concept of a pointer is still used all over the place in C++ (usually somewhat hidden from plain sight).

If you'd like to get an idea of one instance where manipulating pointers can be useful, take a look at STL iterators - the concept of pointers have been wrapped up to allow "stepping over" elements stored in STL containers - The concept of a 'pointer' as an index/address for memory is recycled; but representing the position or place-in-sequence of data stored in an STL container.

for example, with a vector, you have access to two iterators called 'begin' and 'end'. These are useful for being able to describe the entire range of data stored in the vector without actually referring to the vector itself (begin and end represent the first, and then one-past-the-end respectively).
These sequence points can be used without ever needing any information anything about the container itself, such as its size or position of any other elements - the iterators are able to 'jump over' every element from begin to end just as you might do with an index - the big difference is that an index needs you to know about the container whereas the iterator doesn't (It provides direct access on its own). This ability is extremely useful for all kinds of algorithms and operations involving sets/sequences of data:
#include <vector>
#include <algorithm> 
std::vector<int> vec;
vec.push_back(3);
vec.push_back(5);
vec.push_back(1);
vec.push_back(2);

std::sort( vec.begin(), vec.end() ); 
Internally, 'sort' will take a copy of 'begin' and use that copy to step over every element within vec until it reaches 'end'.

You could argue that its possible to do this with indexes by writing your own 'sort' function (if you wanted to reinvent the wheel), since vectors allow random access; However, the sort function would then need details about the container itself. if you swapped the vector for a std::list, there would be no random access (no indexes), but the 'iterator' interface is exactly the same, and so is the swap function
std::list<int> mylist;
mylist.push_back(3);
mylist.push_back(5);
mylist.push_back(1);
mylist.push_back(2);

std::sort( mylist.begin(), mylist.end() ); 


Again, std::sort requires 'placeholder objects' as its arguments (which are either iterators or pointers) to act as reference points, from which it is able to manipulate every item stored within a container or array.


If you want to begin to understand how pointers are useful, take some time to learn about iterators, and how they interact with vector/list/etc. Their use with containers are conceptually identical to the way pointers are used to 'step over' an array; The idea is perhaps a new way of thinking about sets of data, if you're only familiar with thinking about data "belonging" to an array or container, and being indexed from 0-to-N


edit If you wanted to do something yourself with an iterator, you could use it to print the list
std::list<int>::iterator i = mylist.begin();
for( ; i != mylist.end(); ++i )
    std::cout << *i << std::endl; 
(note the pointer-ish syntax!)

This post has been edited by Bench: 29 December 2009 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Concept vs Facts

Posted 29 December 2009 - 04:27 PM

It's simple: write a linked list data structure without pointers. That attempt should be enlightening as to why pointers exist.
Was This Post Helpful? 0
  • +
  • -

#7 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

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

Re: Concept vs Facts

Posted 29 December 2009 - 04:48 PM

One explicit example could be.
Ok, lets say you write some assembly code, for example.

[ORG  0x100]
DW 0x250A 



That code would define a word (Two bytes) at the current memory address the program is at.

Lets say you didnt want to use complex assembly code, what if you wanted to define a word value in your memory using C++. A normal variable would not do that, you can use pointers instead. For example

int *memoryLocation = (int *)0x100;

*memoryLocation = 0x250A;



And there you go, as for the usage, you can write operating systems in C, so you might use pointers to put values into video memory for text output for example.

also, what about binary trees, you could make pointers to a node in the tree. here is a full binary tree example although, thats a whole different story..

#include <iostream>
#include <stdio.h>

using namespace std;

// Out tree structure
struct node
{
	int data;
	node *left;
	node *right;
};

// pointer to our tree
node *tree = NULL;

// returns the size of a given binary tree
int size(node *tree)
{
	if (tree == NULL)
		return 0;
	else
		return size(tree->left) + 1 + size(tree->right); 
}

// Prints out the tree in order
void inorder(node *tree)
{
	if(tree != NULL)
	{
		inorder(tree->left);
		cout << tree->data << endl;
		inorder(tree->right);
	}
}

// Insert node into binary tree
node *insert(node *tree, int value)
{
	if(tree == NULL)
	{
		tree = new node;
		tree->left = tree->right = NULL;
		tree->data = value;
	}
	else
		if(value <= tree->data)
			tree->left = insert(tree->left, value);
		else
			tree->right = insert(tree->right, value);

	return tree;
}

// Main
int main()
{
	tree = insert(tree, 100);
	tree = insert(tree, 120);
	tree = insert(tree, 20);
	tree = insert(tree, 1);
	tree = insert(tree, 300);
	tree = insert(tree, 145);

	inorder(tree);

	cout << size(tree);

	cin.get();
	return 0;
}







Was This Post Helpful? 0
  • +
  • -

#8 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

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

Re: Concept vs Facts

Posted 29 December 2009 - 05:08 PM

Sorry posted this by accident

This post has been edited by Aphex19: 29 December 2009 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1