From 58f39a6ae77952ef12bc39535cd8cc0785598a83 Mon Sep 17 00:00:00 2001 From: Butcher3Years Date: Tue, 10 Feb 2026 23:41:39 +0530 Subject: [PATCH] About Deep copying and shallow copying --- Copying--Copying Constructor/Deep.cpp | 94 +++++++++++++++++++++++++++ Copying--Copying Constructor/Log.cpp | 62 ++++++++++++++++++ Copying--Copying Constructor/main.cpp | 31 +++++++++ Smart pointers -- Types/weak.cpp | 2 +- 4 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 Copying--Copying Constructor/Deep.cpp create mode 100644 Copying--Copying Constructor/Log.cpp create mode 100644 Copying--Copying Constructor/main.cpp diff --git a/Copying--Copying Constructor/Deep.cpp b/Copying--Copying Constructor/Deep.cpp new file mode 100644 index 0000000..64fe783 --- /dev/null +++ b/Copying--Copying Constructor/Deep.cpp @@ -0,0 +1,94 @@ +//"DEEP COPYING" works through copying constrctor[is a constructor which called for second string when actually copies it ] + +#include +#include +#include + +class String +{ + private : + char* m_Buffer; + unsigned int m_size;//see size + public : + String(const char* string) + { + m_size = strlen(string); + m_Buffer = new char[m_size + 1 ]; + memcpy(m_Buffer, string, m_size); + m_Buffer[m_size] = 0; + + } + + /* String(const String& other)//taking const ref to same class [copy constrctor] and we want memory in ptr not the pointer + : m_Buffer(other.m_Buffer), m_size(other.m_size) + { + + } + this is default copy constrctor in c++ + */ + + String(const String& other) + : m_size(other.m_size) + { + std::cout << "Copied String!" << std::endl; + m_Buffer = new char[m_size + 1];//creating a new buffer + memcpy(m_Buffer, other.m_Buffer, m_size + 1);//same like before string class but a new buffer one also we know previous string have null termination + } + + + // String(const String& other) = delete; then coppy constrctor wont work same like unq ptr + + /* OR + + String(const String& other) + { + memcpy(this, &other, sizeof(String)); + } + */ + + ~String() + { + delete m_Buffer; + } + + char& operator[](unsigned int index) + { + return m_Buffer[index]; + } + + friend std::ostream& operator<<(std::ostream& stream, const String& string); +}; + + +std::ostream& operator<<(std::ostream& stream, const String& string) + { + stream << string.m_Buffer; + return stream; + } + + void PrintString(const String& string)//by attaching ref and const it wont copies again and we cant edit existing string + { + String copy = string;//power of const ref + std::cout << string << std::endl; + } + +int main() +{ + + String string = "Cherno"; + String second = string; + + second[2] = 'a'; + + // std::cout << string << std::endl; + //std::cout << second << std::endl; + + PrintString(string); + PrintString(second);//simply needlessly copying it and we actually copying it 2 more copies + + std::cin.get(); +} + +//AFTER THIS SECOND STRING CANT CHANGE FIRST ONE +//By const ref we can do copies how much we wanted and this time copied String for string SECOND + diff --git a/Copying--Copying Constructor/Log.cpp b/Copying--Copying Constructor/Log.cpp new file mode 100644 index 0000000..5543b4b --- /dev/null +++ b/Copying--Copying Constructor/Log.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + +class String//strings made of an array of characters +{ + private : + char* m_Buffer; + unsigned int m_size;//see size + public : + String(const char* string) + { + m_size = strlen(string); + m_Buffer = new char[m_size + 1 ];//with null termination character to get no extra memory adress + + memcpy(m_Buffer, string, m_size);//destination ,souce and the size in bytes and size is + 1 if we not created null termination character down there + m_Buffer[m_size] = 0;//manually created the null termination character + + /*for(int i = 0; i < m_size; i++) + m_Buffer[i] = string[i];//copies character one by one + best way is by memory copy */ + } + ~String() + { + delete m_Buffer; + } + + char& operator[](unsigned int index) + { + return m_Buffer[index]; + } + + friend std::ostream& operator<<(std::ostream& stream, const String& string); //to avoid using another method GetBuffer and use priavte member we need to make a friend +}; + + +std::ostream& operator<<(std::ostream& stream, const String& string) //overloading operator + { + stream << string.m_Buffer; + return stream; + } +int main() +{ + + String string = "Cherno"; + String second = string; + + second[2] = 'a'; + + std::cout << string << std::endl; + std::cout << second << std::endl; + + + std::cin.get(); +} + +//IN this copying compiler takes intial string class members list and copies them into a new memory adress second +//It does a shallow copy means the 2 strings have same char pointer means memory adresss is same in both strings in buffer +//At the end we want to delete the same block of adress twice so that we get error after the termination of {} +//To not to crash we need new char array to store copied string its like both pointing to same memory adress!! +//We need second block which conatain different pointer points a new memory adress! +//IK done through "DEEP COPYING" means not the shallow copying it does copy object Entirely \ No newline at end of file diff --git a/Copying--Copying Constructor/main.cpp b/Copying--Copying Constructor/main.cpp new file mode 100644 index 0000000..0a2a484 --- /dev/null +++ b/Copying--Copying Constructor/main.cpp @@ -0,0 +1,31 @@ +#include +#include + + + struct Vector2 + { + float x, y; + }; +int main() +{ + Vector2* a = new Vector2{ 2, 3 }; + Vector2* b = a; //here this new opearator affects both a and b cause those 2 ptrs points same memory address!! + + //b->x = 2; affects memory adress not pointer + + // b.x = 5;//a.x still 2 just copying a to b but still those 2 points different memory adressses + + + std::cin.get(); +} + +//By assigning one to other variable to other it always copies +//In pointers it copies the memory adress +//You cant re assign the refernces[just changing underlying thing thats pointing to ] + +/* int a = 2 ; + int b = a; + + int b = 3;//still a is 2 + */ + diff --git a/Smart pointers -- Types/weak.cpp b/Smart pointers -- Types/weak.cpp index b7e4e92..0d6c634 100644 --- a/Smart pointers -- Types/weak.cpp +++ b/Smart pointers -- Types/weak.cpp @@ -40,4 +40,4 @@ int main() //shared ptr to shared ptr ref count increases //shared ptr to weak ptr ref count remain same like you have Entities but you dont take ownership[it wont keep you alive] -//try to use unq ptr and then shared always \ No newline at end of file +//try to use unq ptr and then shared always according to overhead \ No newline at end of file