English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i'm reading about pointers in C++ and they dont really seam to be worthwhile or am i missing something?

2006-12-06 10:33:52 · 3 answers · asked by willy 5 in Computers & Internet Programming & Design

sorry about the qestion it shoulda read - "How can pointers in C++ be useful"

2006-12-06 10:35:08 · update #1

wow... i definitly thought i would get a response by now... guess it must not be that important, lol

2006-12-06 10:49:59 · update #2

yeah i figured it was important i just put that so someone would finally post lol

2006-12-06 10:57:41 · update #3

3 answers

Pointers provide a lot of power within a C++ (or C) application. You can allocate memory in one spot and have it persist for as long as you need it, as long as you delete them memory allocation before the application exits. This persistence of memory provides for a lot of flexibility because the memory can be accessed anywhere from inside the application as long as you provide the pointer.

There is a lot you can do with pointers once you fully understand what you're dealing with. Just like the Force, though, pointers have both a light and dark side, and the dark side of pointers can lead you to crashing applications, crashing the system, or worse....

2006-12-06 13:07:04 · answer #1 · answered by ballarke 3 · 0 0

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++.

2016-05-23 02:00:33 · answer #2 · answered by Anonymous · 0 0

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...

2006-12-06 10:56:10 · answer #3 · answered by evolver 6 · 0 0

fedest.com, questions and answers