|"So my question is... What the heck does this do?"|
You shouldn't. A program has two areas of memory where storage (aka "variables
") can reside:
1) The "stack".
The stack is where "automatic
" storage resides. Basic examples of automatic storage are as follows:
int Function( int X ) // "X" is automatic when "Function" is called
int main( )
int A; // Automatic
int *B; // Automatic
int Array; // Automatic
The stack is used for simple data-types such as "int
" and "double
". Generally speaking, the stack is where all small, simple data-types should
2) The "heap"
. The heap is where memory is allocated from during run-time. Allocating memory from the heap (with "new
") is a way to acquire memory based on the program's needs during its run-time. Typically, the heap should only be used when large chunks of memory (1KB memory blocks, for example) are required.
|"What's the difference between this?|
int *ptr; // creates a pointer to an address that can hold an int
and this ?
int *ptr = new int; // creates a pointer to an address that can hold an int"
The first code snippet creates a pointer which has automatic storage, which resides on the stack. A pointer by itself can point to any piece of storage -- regardless of placement -- that's of type "int
" (in this case). The second code snippet also creates a pointer with automatic storage, but refers to storage that resides on the heap.
In comparison, both pointers in both snippets are placed in the same area of memory and refer to the same type of storage and therefore are fundamentally identical. What each pointer points to in this case does not matter.