Re: How are C++ Pointers Useful?

if you want learn STL you must know pointer and function pointer very well

Actually if you are doing it right you should almost never use a pointer with STL. Kind of the whole point is to abstract away from direct memory access via pointers.

I suppose you should understand the concept of a pointer but usually you will be using iterators not pointers.

Father more, you should not really need to know anything about function pointers. You may being using them but you probably will not have to think about them much and should not have to get into their crazy syntax.

Re: How are C++ Pointers Useful?

There are two types of memory allocation: static and dynamic. Statically allocated objects are terminated at the end of the scope:

void function()
{
int a;
// a is terminated at the end of this function.
}

Dynamically allocated objects will remain existent even outside of the scope. This can be useful. To dynamically allocate an object in C++, you use the new

void function()
{
new ClassType();
// ClassType object will still exist at the end of this function.
}

As you can see, I created an object of ClassType. It exists in memory somewhere. To use it, I would have to reference it. In the above example, I cannot reference it because when I created it, I didn't assign it to anything I can reference it with. But below, I can.

ClassType* pointer = new ClassType();

Now I can reference the object I just created through the pointer: pointer->CallMethod();

Re: How are C++ Pointers Useful?

Posted 27 July 2011 - 02:20 PM

Yeah, I guess you can say that. It exists in memory. But your pointer is still scoped. For example:

void function()
{
ClassType* pointer = new ClassType();
}

Your ClassType object will still exist, but after the function, your pointer will go out of scope. What I mean is if you try to reference the object like: pointer->CallMethod(); it won't work because pointer only existed in the scope of function.

Re: How are C++ Pointers Useful?

There are two types of memory allocation: static and dynamic. Statically allocated objects are terminated at the end of the scope:

This is actually not really true. There are a number of different ways to allocate memory in C/C++ (not counting OS specific ways).

There is: Static memory or "data segment(s)" and your program can actually have multiple ones but generally there are 2, one for static data such as global variables and static variables in functions and classes. Then there is the const segment that generally contains all constants (for which memory was allocated) - this will contain all of the string literals your program uses etc. -- Note this memory does not consume available stack space - it also can not be resized (without OS specific functions) at runtime.

Local Data or "The Stack" or "automatic allocation" -- these are variables that are scoped. These exist on the stack and are "popped off" of the stack when the current function (or scope) ends. There are your local variables and function arguments.

-- Note that you actually CAN allocate arbitrary blocks of memory on the stack like with new/malloc but it was found to be a poor practice however you will still find it in use in embedded systems that don't really have a "heap" to grab memory from.

Dynamic Memory or "the heap" OR "free store" -- memory allocated via the OS for your program's use by the new operator or malloc().

While we are at it, lets not forget about register variables (allocated as registers if available).

So there are many different ways to allocate memory. Most of them can be accessed via pointers.

pointers allow variables to transcend scope (like passing a pointer to an array in main to a function findSum which calculates the sum of the array and returns a value -- even though the array is out of findSum's scope, it can access the array via a pointer.)

Pointers let us pass small variables (the pointer is just an integer in memory) rather than large block back and forth. Imagine loading a large image into memory, you really don't want to have to keep making copies of this bitmap every time you wanted to pass it to a function.

Pointers let us access shared memory (like accessing the BIOS keyboard buffer (older DOS programming)) or buffers where we can get structured data from the OS.

Pointers let us have dynamic structures like linked lists and trees that can grow to huge sizes.

Pointers enable OOP.

Without pointers programming computers is really very hard. Even lanugages that don't give you direct access to pointers like BASIC and Java use pointers because there just is no way to do the things that they do without them.

Re: How are C++ Pointers Useful?

No, it's handle is limited to the scope in which it is declared. A global variable's scope is that in which "Global" of course.

Well... no. Its handle is just an address and while it may be stored in a local pointer, when you leave the local scope without freeing the memory it is STILL allocated even if you no long have a pointer containing its handle. But the block of memory allocated remains and can be used if you have saved the handle somehow (like storing it in some pointer).

Dynamic objects don't really have "scope". As you said their handles or pointers have scope but then memory itself kind-a does not... well I suppose that it has "process scope" meaning that (at least on windows/linux) other processes do not necessarily have access to it and since you application can have multiple processes in one application I suppose it does have some sense of scope but really no. It is "like" a global variable in that it can be accessed from anywhere in the program. It is "unlike" a global variable in that it has limited life (can be de-allocated) and the pointers used to access it are generally scoped (no reason you can't put that address in a global variable though).

Summary: It is not a global variable as its scope depends upon you the programmer and is not determined by specification in the language.

it CAN be accessed globally if you choose, or it can be limited - but it is up to you.