Question:
why are pointers so usefull in c++?
dodgerqwerty
2009-06-18 07:17:22 UTC
i cant get my head round pointers.

i know they point to the place in the memory where the data is stored.


whats the point of it
can any one explain in simple english , why the usefull please
Six answers:
Found my brain but lost my mind
2009-06-20 01:04:42 UTC
let me see if I can give you a CLEAR reason why pointers are so useful...



I'm gonna assume you understand the basic concepts of computer memory. I'll further assume you understand about virtual memory so I don't have to make my answer complicated. fair enough?



Old-school programming (PL/1, COBOL, even BASIC) contains a concept of "working-memory". That is the memory currently loaded and in use for an executing function. In todays programming languages (like C,C++,JAVA, C#, etc) you would refer to "working-memory" as local variables (that are allocated from the "stack" and discarded after the function concludes execution).



In the U.S., a telephone is 10 digits (310-555-1234). You have the telephone numbers of 1,000 people. You want to be able to look through your list of phone numbers. You have "working-memory" for the telephone number you are currently looking at.



Each time you want to look at the next (or previous) telephone number in your list, you have to copy the telephone number into your "working-storage". The (object) code required to copy memory (from your telephone list to your working-storage) is significant (not TOO much but alot if you consider you have to redundantly call it a ca-jillion times).



An alternative to executing all that unnecessary memory copying code is to just "move the address at which you're look at". So instead of "copying" the memory, you're just changing "where" you're looking.



Let me explain in code:



typedef struct _tagTelephone

{

int NPA; // area code

int NXX; // first 3 digits

int LATA; // last 4 digits



} TELEPHONE;



TELEPHONE myTelephoneList[1000];

int lookThruNumbers(int theNumberIwantToLookAt); // between 0 - 999



// WITHOUT POINTERS



int lookThruNumbers(int theNumberIwantToLookAt)

{

TELEPHONE thisTelephone;



memcpy(&thisTelephone, &myTelephoneList[theNumberIwantToLookAt], sizeof(TELEPHONE));

printf ("NPA = %d\nNXX = %d\nLATA = %d\n", thisTelephone.NPA,thisTelephone.NXX,thisTelephone.LATA);



return 0;

}



// WITH POINTERS



int lookThruNumbers(int theNumberIwantToLookAt)

{

TELEPHONE *pthisTelephone = &myTelephoneList[theNumberIwantToLookAt];



printf ("NPA = %d\nNXX = %d\nLATA = %d\n", pthisTelephone->NPA,pthisTelephone->NXX,pthisTelephone->LATA);



return 0;

}



NOW.... Both functions will work properly. However the function using pointers doesn't have to copy any memory in oder to look at your list. We're just changing WHERE we're pointing at. Thusly, the function using pointers will execute MUCH faster than the one not (using pointers).



A SIDE-NOTE for you to consider...



Did you see the variable "TELEPHONE myTelephoneList[1000];"? THIS IS A POINTER! "myTelephoneList" points to a block of memory that is 12K (3 ints * the size of an int -- 4 bytes * 1,000 telephone numbers.. or 12K). So its my guess that if you've been working with C/C++, you've been using pointers for a while (without even knowing it).



Today's languages that utilize "references" have matured the reference mechanism so you don't have to think indirectly about pointers (or the mechanics for managing them). The underlying implementation for a reference is STILL a pointer -- its just managed for you.



So with managed pointers (or references) why do you still want to care about pointers? Here's your answer...



Always use the right tool for the right job! If you're writing a game, an operating system, a database platform, or some OTHER testicle-smashing project, you really want to make sure not ONLY your implementation is optimized but your design as well. Minimizing your executable foot-print, the amount and usage of your memory, the speed of your I/O are all factors that you may elect to remove your training wheels of having modern-day compilers "give" you code for your own protection. Removing said code will give you MANY oportunities to realize such optimizations. If you're writing a "Hello World!" app, then you can forget about pointers and let the compiler keep you fat, dumb & happy!



Hopefully this was easy enough to understand.
Bob M
2009-06-18 08:41:30 UTC
Most programming these days uses references, this is because it is the only memory management that should be used on a shared resource OS (Linux/Unix/Windows), these references are simply a numeric ID of the memory allocation returned by the OS, then you reference that location using the ID plus the offset into the memory location. Your runtime in some languages, or the OS with others, takes care of the actual memory management.



In true pointers you point directly at a physical location in memory.



Where data is small you are not likely to notice much difference between the two. But if you have a lot of complex coding for each location, then the barrier in referencing that is to protect the machine from your code is simply too slow.



You might have a bitmap image in memory 1024x800, that is 819200 bytes in 256 colour or 1638400 in 16 bit colours, 3276800 in 32 bit colours..



If your code is looking to hilight colour borders you have to basically crawl row by row looking for strong changes in colour which may not be apparent unless you take into account say 10-20 pixels, but you don't want a dot or just a dark patch, so you have to take into accound a squre area of the image.



That is a lot of processing and your customer will want it done very quicky. C++ has less barriers than C#/VB, so it is the best way to process large memory objects. In the example above you would have several pointers on each step and basically stream that data in and out of your code. I don't know another programming language that would let you do it that way, except C itself of cause.



In C#, the main one I use on Windows machines these days, you simply could not do it that way at all. There are other methods which you can use, but not nearly so efficient as C++.
Eric
2009-06-18 09:25:25 UTC
Say you have an array of 1024 integers and you want to put a value in the middle of the array you would have to move half of the items down 1 for it to fit in the middle



if each element was a node that held a pointer to the next element, and the data (int) it is an easy addition to the dynamic list...



What if you wanted to double the array but the end of the array in memory was not free. You would have to recreate the array with the proper size and copy the values over... what a time waster.



Another example lets say that you work with huge files that are completely stored in memory (think of a fragmented hard drive) you have small empty storage places everywhere, now what if you wanted to place a huge chunk of data into memory that HAD to be located at the consecutive location, it would be impossible to locate that amount of consecutive free memory.
Sarah
2016-04-10 01:39:27 UTC
You're kidding, right? Pointers are desirable (at times) because they allow you in, an OO architecture, to put a value or value array into memory exactly once, instead of making repeated copies of it into memory as you pass the value through your classes and methods. But pointers move from desirable to absolutely necessary when you try and use the operating system's APIs, or the graphical subsystem's. Most API calls in, well, just about any operating system will require you to pass/retrieve pointers. You can't expect to write much more than "Hello world!" without pointers...
sentiao
2009-06-18 07:23:08 UTC
You can send the value of the variable that the pointer points to can be changed without the need for a return value.



Also, imagine a variable which contains the address of the Next variable. You can create chains (dynamically linked lists) this way, which are used in kernel programming (to name one).
2009-06-18 07:25:29 UTC
check out the link below to learn more about C++...



http://markthispage.blogspot.com/2009/06/some-good-sites-to-learn-c-programming.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...