721
edits
No edit summary |
|||
Line 26: | Line 26: | ||
It's called "stack" because the variables really stack up there, on after the other, depending of the order you instantiate them. The syntax for creating a Point2D as a stack variable is as follows: | It's called "stack" because the variables really stack up there, on after the other, depending of the order you instantiate them. The syntax for creating a Point2D as a stack variable is as follows: | ||
Point2D p1 = Point2D(1,2); | <source lang="c++" line start="1" highlight="4">Point2D p1 = Point2D(1,2);</source> | ||
If we use pointers however we can use memory space everywhere in the main memory. This space is used by other programs as well and rather loosely organized. This is why it is called "heap". | If we use pointers however we can use memory space everywhere in the main memory. This space is used by other programs as well and rather loosely organized. This is why it is called "heap". | ||
When we create a pointer to our Point2D, the syntax is like this: | When we create a pointer to our Point2D, the syntax is like this: | ||
Point2D* p2 = new Point2D(1,2); | <source lang="c++" line start="1" highlight="4">Point2D* p2 = new Point2D(1,2); | ||
... | ... | ||
delete p2; | delete p2;</source> | ||
We need to use the asterisk to denote, that we want to create a pointer and we need to use the "new" keyword to reserve the memory for our Point2D. After the "new" keyword we can initialize our Point2D directly as shown before. | We need to use the asterisk to denote, that we want to create a pointer and we need to use the "new" keyword to reserve the memory for our Point2D. After the "new" keyword we can initialize our Point2D directly as shown before. |