10 Replies - 1942 Views - Last Post: 10 March 2011 - 09:28 PM Rate Topic: ***** 1 Votes

#1 Lesco   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Proper usage of pointers and why

Posted 10 March 2011 - 06:06 PM

Hello, it's been a while DIC.

Today I am trying to completely understand pointers. I believe that I am very close to understanding how to use a pointer, although I am having one problem. I don't exactly understand when it is proper to use pointers in my programs, and why I should use them.

I understand what they do, I just don't understand the benefits of a pointer instead of a normal variable.

I was just wondering if someone around here might be able to put this in better words for me to understand. I'm not asking for a novel so don't feel burdened, but possibly just a paragraph or two describing where and when to properly use pointers and why?

Thank you,

Is This A Good Question/Topic? 1
  • +

Replies To: Proper usage of pointers and why

#2 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

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

Re: Proper usage of pointers and why

Posted 10 March 2011 - 06:34 PM

Pointers are actually most useful when you look at how they work on a low level, take a look at this C code to start with.

char *str = "Hello, world";
str[10] = 'A';


Notice that "str" is a pointer to a location in memory that contains the string, and this pointer is being dereferenced (we are looking at the value at the address the pointer points to) on the second line. How could we do this without pointers? I doubt there would be an intuitive solution, infact, I doubt there would be any tangible solution without a way to reference a memory address that contains data, such as strings.

To clarify, take a look at the disassembly of that code.

mov   dword ptr [str],offset string "Hello, world" (0DFC994h)  
mov   eax,dword ptr [str]  
mov   byte ptr [eax+0Ah],41h  


The first line is taking the address of the string "Hello, world" (0DFC994h) and putting that address at another address, somewhere decided at runtime. After this, "str" will now point to this string data.

The second line moves the address of the string into the EAX registers.

And lastly, here's where we make use of the pointer. The Assembly code takes the value of 0x41 ('A' ASCII) and places that value at an effective address formed by taking the address of the string (in EAX) and adding 10 to it (the index in our array).

I'm not sure how much this would help you, but just realise that pointers are very important to the functing of good software and are not simply an extra or optional part of a program, as some may think on face value. All computer systems and architectures use pointers of some sort.

This post has been edited by Aphex19: 10 March 2011 - 06:37 PM

Was This Post Helpful? 3
  • +
  • -

#3 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Proper usage of pointers and why

Posted 10 March 2011 - 06:36 PM

Well say you have some horribly massive class that contains hundreds of functions and hundreds of instance variables. Obviously that is quite a large chunk of data to be passing around between functions especially if you have hundreds of instances of this class and data and lots of operations are called upon these instances for the purpose of interacting with the class.

It is at this point in time you would pass by pointer so you are passing an address to an object rather than an almighty object that can be heavily taxing.
Was This Post Helpful? 3
  • +
  • -

#4 Lesco   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Re: Proper usage of pointers and why

Posted 10 March 2011 - 06:51 PM

Thank you have both aided me greatly on my mission to understand and learn how to use pointers correctly! :P
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

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

Re: Proper usage of pointers and why

Posted 10 March 2011 - 06:59 PM

View PostLesco, on 10 March 2011 - 06:51 PM, said:

Thank you have both aided me greatly on my mission to understand and learn how to use pointers correctly! :P


You're welcome, and I'm glad that DaneAU also posted a more abstract reason for using pointers, which is that using pointers in a language like C/C++ is not an essential requirement, but can improve the efficiency of your code hugely. My point (no pun intended) was simply to say that pointers are much more often used explicitely on a lower level, at the architectural level than in higher level languages.

It might be a good idea for you to also learn about pointer notation vs array notation, which can be a point of (unecessary) confusion. Basically, a pointer can be treated as an array and visa versa.

This post has been edited by Aphex19: 10 March 2011 - 07:01 PM

Was This Post Helpful? 1
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7163
  • View blog
  • Posts: 14,929
  • Joined: 16-October 07

Re: Proper usage of pointers and why

Posted 10 March 2011 - 07:08 PM

*
POPULAR

Pointers fundamentally do two things. They allow you to avoid copying large amounts of data when you pass a parameter. Even better, they allow functions to make changes to the data object passed.

Quick and dirty example code:
#include <stdio.h>

typedef struct {
	int x, y;
} Point;

void printPoint(Point pt) {
	// I made a copy
	printf("Point: (%d,%d)\n", pt.x, pt.y);
}

void movePoint(Point pt, int dx, int dy) {
	// I am changing a copy
	pt.x += dx;
	pt.y += dy;
}

int main() {
	Point pt = {6,7};
	printPoint(pt); // copy
	movePoint(pt, 2,3); // copy
	printPoint(pt); // copy
	
	return 0;  
}



Results:
Point: (6,7)
Point: (6,7)



So, in the above, the entire Point structure always get's copied. In the print, it's simply wasteful, copying more data around than needed. In the move, we modify a copy, so it's basically useless.

Now, with pointers:
#include <stdio.h>

typedef struct {
	int x, y;
} Point;

void printPoint(const Point *pt) {
	printf("Point: (%d,%d)\n", pt->x, pt->y);
}

void movePoint(Point *pt, int dx, int dy) {
	pt->x += dx;
	pt->y += dy;
}

int main() {
	Point pt = {6,7};
	printPoint(&pt);
	movePoint(&pt, 2,3);
	printPoint(&pt);
	
	return 0;  
}



Results:
Point: (6,7)
Point: (8,10)



It doesn't matter if Point is requires 8 bytes or 1000 bytes of storage, I'm only passing a pointer sized amount of data ( usually 4 bytes). I can also manipulate the data I pass. Both of these things are impossible without pointers.

I hate to bring it up, but this will crash horribly:
char *str = "Hello, world";
str[10] = 'A';



The reason is that the pointer str references a block of memory that's been set aside for literals. The program doesn't have rights to modify it.

This post has been edited by baavgai: 10 March 2011 - 07:09 PM

Was This Post Helpful? 6
  • +
  • -

#7 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

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

Re: Proper usage of pointers and why

Posted 10 March 2011 - 07:15 PM

Quote

I hate to bring it up, but this will crash horribly:
char *str = "Hello, world";
str[10] = 'A';



The reason is that the pointer str references a block of memory that's been set aside for literals. The program doesn't have rights to modify it.


Sorry, you're completely correct, I wasn't using my noggin', the data section is read only. A better example might have been using an integer, such as.

int *ptr = new int;
*ptr = 10;
delete ptr;

This post has been edited by Aphex19: 10 March 2011 - 07:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 Lesco   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Re: Proper usage of pointers and why

Posted 10 March 2011 - 07:30 PM

View PostAphex19, on 10 March 2011 - 07:59 PM, said:

It might be a good idea for you to also learn about pointer notation vs array notation, which can be a point of (unecessary) confusion. Basically, a pointer can be treated as an array and visa versa.


Very cool, thanks for the suggestion and the url to back it up also!

And also, thank you baavgai for the great explanations and examples!

This post has been edited by Lesco: 10 March 2011 - 07:32 PM

Was This Post Helpful? 1
  • +
  • -

#9 Gamegoofs2   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 15-April 09

Re: Proper usage of pointers and why

Posted 10 March 2011 - 08:52 PM

Pointers are something I've been trying to wrapper my head around too.

Why would we pass a variable at all? Besides the obvious wanting to manipulate it separately. Would it be better to pass the pointer as your default way of passing data?
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: Proper usage of pointers and why

Posted 10 March 2011 - 09:12 PM

I prefer passing by reference. IMO, you accomplish the same goals with less confusion.
Was This Post Helpful? 0
  • +
  • -

#11 anonymous26   User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Proper usage of pointers and why

Posted 10 March 2011 - 09:28 PM

Pointers are great for a number of reasons including:

1. Data need not be copied to be referenced, instead it is pointed to.
2. Any data structure that is representable in C/C++ can be passed into a function and returned via in/out parameters.
3. The type of data doesn't have to be known until cast to a particular type for modification, see void pointers.
4. Pointers form the primary language mechanism for dynamic memory allocation.
5. Pointers do not only represent variable data, they can also point to functions. See pointer to function.
6. Pointers can be 'resized' and reallocated on the heap at runtime.
7. Pointers permit explicit arithmetic to traverse the represented structure.
8. All pointers are the same size, for instance in 32bit code it is four bytes, as is a pointer to pointer, and a pointer to pointer to pointer ad Infinitum.
9. Pointers can point to other pointers.

The list goes on and on. I'm stopping myself now. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1