Files
gpd-925-en/CPlusPlus/Pointers/main.cpp
T
2026-09-29 22:09:02 +02:00

113 lines
3.9 KiB
C++

#include <iostream>
#include <memory>
#include "helpers.h"
using namespace std;
int main()
{
// general
int* pNumber = nullptr; // always initialize pointers
pNumber = new int(0); // create a new integer on the heap and assign its address to pNumber
cout << "pointer address: " << &pNumber << endl;
cout << "pointer size: " << sizeof(pNumber) << endl;
cout << "integer address: " << pNumber << endl;
cout << "integer size: " << sizeof(*pNumber) << endl;
*pNumber = 100;
cout << "integer value: " << *pNumber << endl;
// BAD BAD - memory leak, because we override the old address with a
// new one without saving or deleting the old data
// pNumber = new int(0);
// here are 100s lines of code
if (pNumber != nullptr)
{
delete pNumber; // will delete the integer on the heap
pNumber = nullptr;
}
safeDelete(pNumber);
// pointer as references outside local scope
int number = 10;
changeInteger(number);
number = changeIntegerByReturn(number);
changeIntegerByPointer(&number);
changeIntegerByPointer(pNumber);
changeIntegerByReference(number);
// smart pointer
// unique_ptr - only onw owner exist for the pointer
// shared_ptr - can have multiple owners
// weak_ptr - is a loose connection to a shared pointer
unique_ptr<int> pNumberUnique = make_unique<int>(0);
*pNumberUnique = 100;
// unique_ptr<int> pNumberUnique2 = pNumberUnique; // compiler error - you can't create a copy of a unique pointer
unique_ptr<int> pNumberUnique2 = make_unique<int>(*pNumberUnique);
shared_ptr<int> pNumberShared = make_shared<int>(0);
shared_ptr<int> pNumberShared2 = pNumberShared;
*pNumberShared2 = 100;
pNumberShared.reset();
weak_ptr<int> pNumberWeak = pNumberShared2;
// *pNumberWeak = 1000; // compiler error - weak pointer can't be dereferenced
if (!pNumberWeak.expired())
{
shared_ptr<int> tmpNumberShared = pNumberWeak.lock();
*tmpNumberShared = 1000;
}
pNumberShared2.reset();
shared_ptr<int> pNumberShared3 = pNumberWeak.lock();
if (pNumberShared3 != nullptr)
{
*pNumberShared3 = 10000;
}
// pointer-pointer
createInteger(pNumber); // BAD BAD - memory leak created
createIntegerWithPointerPointer(&pNumber);
safeDelete(pNumber);
// const examples
const int constNumber = 10; // a constant value has to be directly initialized
// constNumber = 20; // a const value can't be changed
const int* constNumberPointer;
// *constNumberPointer = 20; // a const value can't be changed
constNumberPointer = new int(20); // the pointer isn't constant
int const* numberConstPointer;
// *numberConstPointer = 20; // a const value can't be changed
numberConstPointer = new int(20); // the pointer isn't constant
int* const numberPointerConst = new int(10); // a constant pointer has to be directly initialized
*numberPointerConst = 20; // the value can be changed, because it is not constant
// numberPointerConst = new int(20); // a const pointer can't be changed
// const int const * constNumberConstPointer; // not working at all, because of duplicated const expression
const int* const constNumberPointerConst = new int(10); // a constant pointer has to be directly initialized
// *constNumberPointerConst = 20; // a const value can't be changed
// constNumberPointerConst = new int(20); // a const pointer can't be changed
int const* const numberConstPointerConst = new int(10); // a constant pointer has to be directly initialized
// *numberConstPointerConst = 20; // a const value can't be changed
// numberConstPointerConst = new int(20); // a const pointer can't be changed
// const int const * const constNumberConstPointerConst; // not working at all, because of duplicated const expression
return 0;
}