Added: C++ projects
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 4.3)
|
||||
project(Pointers)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(Pointers main.cpp
|
||||
helpers.h
|
||||
helpers.cpp)
|
||||
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by David Hackbarth on 22.09.26.
|
||||
//
|
||||
#include "helpers.h"
|
||||
|
||||
void changeInteger(int value)
|
||||
{
|
||||
value *= 10;
|
||||
}
|
||||
|
||||
int changeIntegerByReturn(int value)
|
||||
{
|
||||
return value * 10;
|
||||
}
|
||||
|
||||
bool changeIntegerByPointer(int* pValue)
|
||||
{
|
||||
if (pValue == nullptr)
|
||||
return false;
|
||||
|
||||
*pValue *= 10;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void changeIntegerByReference(int& value)
|
||||
{
|
||||
value *= 10;
|
||||
}
|
||||
|
||||
void createInteger(int* pNumber)
|
||||
{
|
||||
pNumber = new int(0);
|
||||
}
|
||||
|
||||
void createIntegerWithPointerPointer(int** ppNumber)
|
||||
{
|
||||
if (ppNumber == nullptr)
|
||||
return;
|
||||
|
||||
*ppNumber = new int(0);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by David Hackbarth on 22.09.26.
|
||||
//
|
||||
|
||||
#ifndef POINTERS_HELPERS_H
|
||||
#define POINTERS_HELPERS_H
|
||||
|
||||
void changeInteger(int value);
|
||||
int changeIntegerByReturn(int value);
|
||||
bool changeIntegerByPointer(int* pValue);
|
||||
void changeIntegerByReference(int& value);
|
||||
|
||||
void createInteger(int* pNumber);
|
||||
void createIntegerWithPointerPointer(int** ppNumber);
|
||||
|
||||
template<typename T>
|
||||
void safeDelete(T*& ptr)
|
||||
{
|
||||
if (ptr != nullptr)
|
||||
{
|
||||
delete ptr;
|
||||
ptr = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //POINTERS_HELPERS_H
|
||||
@@ -0,0 +1,112 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user