Tuesday, July 14, 2009

How can pointers in C++ be usefull in C++?

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

How can pointers in C++ be usefull in C++?
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....
Reply: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...


No comments:

Post a Comment