Informatics 2 - 2,3
Informatics 2 - 2,3
Wskaźniki i referencje
The Agenda
1. Memory allocation concept
2. Reference oparator
3. Dereference operator
4. Comparing reference and dereference operators
5. Declaring variables of pointer types
6. Dynamic memory allocation
7. Examples
Terms
The memory of your computer can be imagined as a succession of memory
cells, each one of the minimal size that computers manage (one byte). These
single-byte memory cells are numbered in a consecutive way, so as, within any
block of memory, every cell has the same number as the previous one plus
one.
This way, each cell can be easily located in the memory because it has a unique
address and all the memory cells follow a successive pattern.
Reference operator (&)
The address that locates a variable within memory is what we call a reference
to that variable.
Program:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int a = 10; // local variable
5. cout << "a = " << a << endl;
6. cout << "a address = " << &a << endl;
7. return 0;
8. }
OUTPUT:
a = 10
a address = 0x6efe1c
Dereference operator (*)
We have just seen that a variable which stores a reference to another variable
is called a pointer. Pointers are said to "point to" the variable whose reference
they store.
Using a pointer we can directly access the value stored in the variable which it
points to. To do this, we simply have to precede the pointer's identifier with an
asterisk (*), which acts as dereference operator and that can be literally
translated to "value pointed by".
Comparing * and & operators
Difference between the reference and dereference operators:
This type is not the type of the pointer itself! but the type of the data the
pointer points to. For example:
1. int * number;
2. char * character;
3. float * greatnumber;
Declaring variables of pointer types
Example:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int firstvalue, secondvalue;
5. int * mypionter;
6. mypointer = &firstvalue;
7. *mypointer = 10;
8. mypointer = &secondvalue;
9. *mypointer = 20;
10. cout << "firstvalue is " << firstvalue << endl;
11. cout << "secondvalue is " << secondvalue << endl;
12. return 0;
13.}
OUTPUT:
firstvalue is 10
secondvalue is 20
Dynamic memory allocation
Dynamic memory allocation in C++ allows you to allocate memory during the
runtime of a program.
This is useful when you don't know the required size of memory at compile
time or when you need to manage memory manually for data structures like
arrays, linked lists, or when dealing with large amounts of data.
C++ provides two primary operators for dynamic memory allocation: new and
delete.
To allocate memory for a single element, use the new operator followed by
the data type of the element.
int * ptr = new int; //Allocates memory for a single integer
After you're done using dynamically allocated memory, it's essential to release
it to avoid memory leaks. For a single element, use the delete operator
followed by the pointer variable.
delete ptr; // Deallocates memory for the single integer
// pointed by ptr
Dynamic memory allocation
Example
1. #include <iostream>
2. using namespace std;
3. int main() {
4. // Dynamic memory allocation for a single integer variable
5. int* ptr = new int;
6. // Check if memory allocation was successful
7. if (ptr != nullptr) {
8. // Input value for the dynamically allocated integer
9. cout << "Enter an integer value: ";
10. cin >> *ptr;
11. // Output the value stored in the dynamically allocated integer
12. cout << "Value entered: " << *ptr << endl;
13. // Don't forget to deallocate the dynamically allocated memory
14. delete ptr;
15. } else {
16. cout << "Memory allocation failed!" << endl;
17. }
18. return 0;
19. }
Problem
Write a C++ program that uses pointers to perform some basic operations on variables.