Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,561 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,202 people online right now. Registration is fast and FREE... Join Now!




Pointers

 
Reply to this topicStart new topic

Pointers, why do we use them?

rgfirefly24
post 3 Aug, 2008 - 07:02 AM
Post #1


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


ok, So I've read several books and articles on pointers. I understand how to use a point, what i don't understand is why. The following example is something they showed me in one of my C++ books:

CODE


#include <iostream>
using namespace std;

int main()
{
const int SIZE =8;
int set[SIZE] = { 5,10,15,20,25,30,35,40};
int *numPtr;
int count;

numPtr = set;

count << "The numbers in set are:\n";
for (count = 0; count < SIZE; count++)
{
count << *numPtr << " ";
numPtr++;
}

count << "\nThe numbers in set backward are:\n";
for (count=0;count<SIZE;count++)
{
numPtr--;
count << *numPtr << " ";
}

return 0;
}


so in the above example why would we use Pointers. Infact why use pointers at all. Wouldn't it just be easier to use the variable or array itself?

i.e
CODE


for (count=0;count<SIZE;count++)
{
cout << set[count] << " ";
}

then

for(count=SIZE; count >= 0; --count)
{
cout << set[count] << " ";
}
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 3 Aug, 2008 - 07:40 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,008



Thanked 170 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well the idea is memory management. Back in the days of limited memory and slow processors it was easier to move a pointer than move a whole chunk of memory representing an object. Keep in mind that pointers are very small variables. They only need to carry a memory address. This makes them easier to move around in memory than objects which could be KB or even MB in size.

For instance, to have an array of pointers is going to be easier to sort if we just move around addresses than to actually move around the objects. When moving an object, the processor has to fetch a new chunk of ram each time, copy over the object, and then take care of the old memory. This is an expensive operation.

Pointers also allow arithmetic and allow you to go byte by byte in memory with a fine granularity.

In todays world and the .NET framework, it handles all the memory management and makes those choices for you. Which is why current languages have pretty much done away with pointers. Since RAM is not quite that limited now, it is easier to focus on the logic behind the code than to worry about the specifics. It allows you to code at a "higher level".

However where RAM is still limited (like mobile devices) and where performance is needed, pointers are still being used. It is why languages like C++ are still so heavily used in gaming industries as well as mobile markets and environments where memory is at a premium.

smile.gif

This post has been edited by Martyr2: 3 Aug, 2008 - 07:41 AM
User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 3 Aug, 2008 - 08:08 AM
Post #3


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


ahh, thank you smile.gif My programming experience is heavily weighted in the .NET world. My first contracted job was doing a .NET application, and my background is mostly in Java, VB, and C# so no pointers needed. This fall i will be taking my advanced c++ class so i was hoping to get back into the swing of using C++ code. I knew that c++ needed more memory management than most other languages, so i guess with using pointers it makes that aspect alot easier. Thank you for explaining that in a way no book seems to care about. I will now spend alot more time learning those handy tools. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 3 Aug, 2008 - 12:42 PM
Post #4


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


Pointers are necessary in some form or another. Sometimes they are disguised as "references". Sometimes they are called pointers, but explicit pointers are forbidden (such as the case in Java, hence the NullPointerException). Since C++ is a system's programming language and descends from C, pointers are a normal data type. Plus, references in C++ are not nullable types, like they are in some high level languages, so pointers are necessary, as uninitialized references are not allowed (even to null).
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 02:55AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month