C++ Builder

What Is The mt19937 Random Generator In Modern C++?

Random numbers are one of the most important parts of today’s modern programming technologies. They are used in mathematics, physics, in many engineering fields, and in programming such as generating random data for testing, random maps in levels, random trees on a planet – the list is endless. Since C++11, mt19937 (std::mt19937) is implemented as a random number generator. In this post, we explain what mt19937 is and how we can use it. What is a random number and a random number generator in C++? A random number is a number that is randomly chosen in a given range. It is impossible to predict future values based on past or present values and they are uniformly distributed over a defined interval or set. Mersenne prime is a prime number used in mathematics that is a number of the form Mn = 2n − 1 where the n is an integer. The Mersenne Twister is a pseudorandom number generator where the period length is chosen to be a Mersenne Prime. It was developed by Makoto Matsumoto in 1997. Since C++11, the Mersenne Twister mathematical number generator is implemented as a random generator number, it is defined in the header as a std::mersenne_twister_engine that is a random number engine based on Mersenne Twister algorithm. What is the std::mt19937 random number generator in modern C++? In C we use rand(), srand() and in C++ we use std::rand(), std::srand(). While they are added to  to make modern C++ compatible, there are more useful and modern random number generators. These are std::mt19937 and std::mt19937_64. The std::mt1993 is a 32-bit Mersenne Twister by Matsumoto and Nishimura in 1998, and std::mt19937_64 is a 64-bit Mersenne Twister by Matsumoto and Nishimura in 2000. The std::mt19937 is a random number generator defined in the header in C++17 standard and beyond, producing 32-bit pseudo-random numbers by using the Mersenne Twister algorithm with a state size of 19937 bits. This is why it is called mt19937 and there is a 64-bit version called mt19937_64. Both are defined as an instantiation of the mersenne_twister_engine. Now let’s see their definitions. Since C++11, mt19937 is defined as below,   typedef mersenne_twister_engine mt19937;   Since C++11, mt19937_64 is defined as below,   typedef mersenne_twister_engine mt19937_64;   How can we use the random number generator std::mt19937 in modern C++? Simply we can generate modern random number as shown below.   std::mt19937 rnd( std::time(nullptr) );   we can use it like so:   unsigned int r = rnd();   if you want to generate a number in a range you can use modulus operator %.This is how can we generate random number between zero to n, i.e. 0 to 100.   unsigned int r = rnd()%100;   This is how can we generate random number between two numbers, i.e. 50 to 150,   unsigned int r = 50 + rnd()%100;   Is there a simple example to use std::mt19937 in modern C++? Here is a simple example to use std::mt19937.   #include #include #include   int main() { std::mt19937 rnd( std::time(nullptr) );   std::cout

Read More

How To Create A Real App That Runs In The Cloud

Hello developers. Our previous sessions in our Winter Webinars series which showed you how to create a real Android app step by step“, how to create a real iOS app (even if you do not have a mac), how to create a real windows app, create a real Mac app, are extremely popular. It’s great to get positive feedback on the webinar content too. In a previous session I covered how to create apps which work on Linux using RAD Studio 12 and Delphi. Today I went on to cover the various ways we can use RAD Studio with Delphi (or C++ Builder) to create apps which run in the cloud. Each session builds a little on the things we learned in prior webinars and adds to that knowledge. Over the next few weeks, we’ll link things up to the cloud, the web, each other, and even a robot arm. Stick around; we’re going to see that RAD Studio can do pretty much anything you can dream of – and do it without needing to be a super hardcore software developer too. If you want to register, go to: https://lp.embarcadero.com/webinar-registration In this article you can catch the replay of the presentation content and the slides. If you watch on YouTube, please hit the “like” and “subscribe” buttons to make sure you get notifications of all the videos in the Winter Webinar series. Hitting “like” and “subscribe” on YouTube will not add you to any mailing lists from Embarcadero – the only effect is for YouTube to send you a notification the next time we upload a new webinar or start a live broadcast. Where can I see the replay of the “How To Create A Real App That Runs In The Cloud” webinar? Here’s the full replay of the video.   All the video replays are also uploaded to our YouTube channel. You can also find them in the “Learn” section of the RAD Studio IDE Welcome page. The plan is, as time goes on, for me to fill that “Learn” tab with a whole series of videos which take you through every aspect of creating cross-platform and desktop apps with RAD Studio on Windows, macOS, Linux, iOS, and Android. Where can I get the slides for the “How To Create A Real App That Runs In The Cloud” step by step guide? Here are all the slides for “How To Create A Real App That Runs In The Cloud”. Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder. Design. Code. Compile. Deploy. Start Free Trial   Upgrade Today    Free Delphi Community Edition   Free C++Builder Community Edition

Read More

Learn About Useful Shared Mutexes Locking In Modern C++

A concurrency support library is designed to solve problems in modern C++ that arise with multi-thread development. This library includes built-in support for threads (std::thread), atomic operations (std::atomic), mutual exclusion (std::mutex), condition variables (std::condition_variable), and many other features. In C++14, in addition to mutex, there is a shared_mutex which is an instance of the class located in header. In this post, we explain using shared mutexes locking in Modern C++. What is a mutex (mutual exclusion) in C++? Mutual Exclusion is a property of concurrency control and in programming, the Mutual Exclusion is a data exclusion method to lock and unlock data that provides exclusive access to a resource. This is mostly needed when we use shared data in multi-thread and multi-task operations in parallel programming. In C++, we can use std::mutex to define mutex data variables to protect his shared data from being simultaneously accessed by multiple threads. Here is an example of how we can use std::mutex with its lock() and unlock() methods,   std::mutex mtx;   mtx.lock();   // do operations mtx.unlock();   What is a shared mutex in modern C++? The shared mutex comes with C++14, it is an instance of the class located in header and used with the shared_mutex class name in mutual exclusion operations of threads. The shared_mutex class is a part of the thread support library, it is a synchronization primitive for the thread operations that can be used to protect shared data when multiple threads try to access. Here is how we can define a shared mutex by using std::shared_mutex.   std::shared_mutex ;   Is there an example about shared mutexes (std::shared mutex)? Here is a simple example about std::shared_mutex with its try_lock_shared() and unlock_shared() methods that comes with C++17.   std::shared_mutex sharedmutex;   // in a thread function sharedmutex.try_lock_shared(); // do operations sharedmutex.unlock_shared();   How to use shared lock unlock mutexes methods? A shared_mutex has lock() and unlock() methods as in mutex type, In C++17, it is improved and supports the additional methods lock_shared, unlock_shared, and try_lock_shared. Simply these are: lock_shared (C++17) The lock_shared method is used to block the calling thread until the thread obtains shared ownership of the mutex. unlock_shared (C++17) The unlock_shared method is used to release shared ownership of the mutex held by the calling thread. try_lock_shared (C++17) The try_lock_shared method is used to obtain shared ownership of the mutex without blocking. Return type is can be  true if the method obtains ownership, or false if it cannot. Is there a full example of how to use shared mutexes (std::shared mutex) in C++? Let’s assume that we have a global val and we read data by a getv() and we write data by putv() functions, and we run these functions in threads. Here is a full and simple example about shared mutexes (std::shared mutex). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43   #include #include #include #include   std::shared_mutex sharedmutex;   int val=500;   void putv( int v ) {   sharedmutex.lock();   std::this_thread::sleep_for(std::chrono::microseconds(2));  //some latency simulation val = v;   sharedmutex.unlock(); }   void getv( int &v ) {   sharedmutex.lock_shared();   std::this_thread::sleep_for(std::chrono::microseconds(2));  // some latency simulation   v = val;   sharedmutex.unlock_shared(); }     int main() {   int readval;   std::thread t1( getv , std::ref( readval ) );   std::thread t2( putv , 100); […]

Read More

What Are The Amazing Containers In Modern C++?

Containers are powerful data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties. The C++ Standards library defines 4 container types. In this post, we explain containers in modern C++. What is a container in modern C++? Containers are modern data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties. A container is a holder object that stores data elements (a collection of data objects). They are implemented as a class template to define objects that can be used with modern rules of C++ (The rule of 6), they allow great flexibility in the different data types supported as elements, they can be used with int, float, double, etc. or with struct types, they can be used with other modern types of C++, lambdas and templates. Thus, the developer can create different data sets in memory, these can be static or dynamic, they are safe and optimized well. Basically, a container manages the storage space for its elements and provides properties and methods to access and operate on them, these methods and properties can be either directly or through iterators. They are mostly dynamic data structures, and they are well optimized for the memory management and performance. In C++, there are four main types of containers, Sequence Containers (vectors, arrays, …) Associative Containers (maps, sets, …) Unordered Associative Containers (unordered_set, unordered_map, …) Container Adapters (stack, queue, priority_queue) Now, let’s see each of them. What are sequence containers in modern C++? In C++, the Sequence Containers are class templates of container types of modern C++ that can be used to implement data structure types (vector, array,…) where they can be accessed sequentially. They are a kind of data types, but objects of classes and they can use methods of its classes, optimized for many modern C++ algorithms and methods. The sequence containers are; std::array : a class template for the static contiguous array (modern C array) std::vector : a class template for the dynamic contiguous array ( modern dynamic C arrays) std::deque : a class template for the double-ended queue std::list : a class template for the doubly-linked list (modern linked list) std::forward_list : a class template for the singly-linked list (modern linked list) What are sequence containers in modern C++? Associative Containers are class templates of container types that can be used to implement sorted data structures where can be quickly searched. They are sorted by keys. We can say they are about O(log n) complexity data structures. The associative containers are; std::map : a class template for the collection of key-value pairs, its keys are unique and it is sorted by keys std::set : a class template for the collection of unique keys, it is sorted by keys  multiset : a class template for the collection of keys, it is sorted by keys multimap : a class template for the collection of key-value pairs, it is sorted by keys  What are associative containers in modern C++? Unordered Associative Containers are class templates of container types that can be used to implement unsorted (hashed) data structures where they can be quickly searched. They are about O(1) amortized, O(n) worst-case complexity data structures. The unsorted associative containers are; unordered_set : a class template for […]

Read More

What Are The Differences Between Mutex And Shared Mutex In C++?

The concurrency support library in modern C++ is designed to solve read and write data securely in thread operations that allow us to develop faster multi-thread apps. This library includes built-in support for threads (std::thread), atomic operations (std::atomic), mutual exclusion (std::mutex), condition variables (std::condition_variable), and many other features. In C++14, in addition to mutex, there is a shared mutex (std::shared_mutex) which is an instance of the class located in header. In this post, we explain a frequently asked mutex question in modern C++, what are the differences between mutex and shared_mutex? What is a mutex (std::shared_mutex) in C++? Mutual Exclusion is a property of concurrency control and in programming, the Mutual Exclusion is a data exclusion method to lock and unlock data that provides exclusive access to a resource. This is mostly needed when we use shared data in multi-thread and multi-task operations in parallel programming. In C++, we can use std::mutex to define mutex data variables to protect his shared data from being simultaneously accessed by multiple threads. Here is an example of how we can use std::mutex with its lock() and unlock() methods,   std::mutex mtx;   mtx.lock();   // do operations mtx.unlock();   Here are more details and examples about std::mutex. What is a shared mutex (std::shared_mutex) in C++? The shared mutex comes with C++14, it is an instance of the class located in header and used with the shared_mutex class name in mutual exclusion operations of threads. The shared_mutex class is a part of the thread support library. It is a synchronization primitive for the thread operations that can be used to protect shared data when multiple threads try to access. Here is a simple example about std::shared_mutex with its try_lock_shared() and unlock_shared() methods that comes with C++17.   std::shared_mutex sharedmutex;   // in a thread function sharedmutex.try_lock_shared(); // do operations sharedmutex.unlock_shared();   Here are more details and a full example about shared_mutex. ———- LINK TO Learn About Useful Shared Mutexes Locking In Modern C++ —————— What are the differences between std::mutex and std::shared_mutex? While the std::mutex guarantees exclusive access to some kind of critical resource, the shared_mutex class extends this feature by a shared and exclusive level of accesses. The shared_mutex can be used in exclusive access level to prevent access of any other thread from acquiring the mutex, as in std::mutex. No matter if the other thread is trying to acquire shared or exclusive access. The shared_mutex can be used in the shared access level to allow multiple threads to acquire the mutex, but all of them are only in shared mode. In thread operations, exclusive access is not granted until all of the previously shared holders have returned the mutex. As long as an exclusive request is waiting, new shared ones are queued to be granted after the exclusive access. For more information about shared mutex feature, please see https://open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3659.html C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, […]

Read More

Three Important Posts About The Features Of C++14

Hello C++ Developers. As I write this post, the summer is over (if you live in the Northern hemisphere), and, in most countries, the new educational year has started, and we wish good luck to all students. If you are a student and want to learn C++, we have a lot of educational posts for you. This week, we continue to explore features from the C++14 standard which is available in C++ Builder. This week, we explain what is constexpr specifier and what are the relaxed constexpr restrictions in C++14. We explain variable templates in C++ and we teach how to use them in modern C++. In another post-pick, we explain what Aggregate Member Initialization is and we give very simple examples for you to try. Our educational LearnCPlusPlus.org site has a broad selection of new and unique posts with examples suitable for everyone from beginners to professionals alike. It is growing well thanks to you, and we have many new readers, thanks to your support! The site features a treasure-trove of posts that are great for learning the features of modern C++ compilers with very simple explanations and examples. RAD Studio’s C++ Builder, Delphi, and their free community editions C++ Builder CE, and Delphi CE are powerful tools for modern application development. Table of Contents Where I can I learn C++ and test these examples with a free C++ compiler? How to use modern C++ with C++ Builder? How to learn modern C++ for free using C++ Builder? Do you want to know some news about C++ Builder 12? Where I can I learn C++ and test these examples with a free C++ compiler? If you don’t know anything about C++ or the C++ Builder IDE, don’t worry, we have a lot of great, easy to understand examples on the LearnCPlusPlus.org website and they’re all completely free. Just visit this site and copy and paste any examples there into a new Console, VCL, or FMX project, depending on the type of post. We keep adding more C and C++ posts with sample code. In today’s round-up of recent posts on LearnCPlusPlus.org, we have new articles with very simple examples that can be used with: The free version of C++ Builder 11 CE Community Edition or a professional version of C++ Builder  or free BCC32C C++ Compiler and BCC32X C++ Compiler or the free Dev-C++ Read the FAQ notes on the CE license and then simply fill out the form to download C++ Builder 11 CE. How to use modern C++ with C++ Builder? In C++, the constexpr specifier is used to declare a function or variable to evaluate the value of at compile time, which speeds up code during runtime. This useful property had some restrictions in C++11, these are relaxed in C++14 and this feature is known as Relaxed Constexpr Restrictions. In the next post, we explain what are the relaxed constexpr restrictions in modern C++. The Aggregate Member Initialization is one of the features of C++. This feature is improved and modernized with C++11, C++14, and C++20. With this feature, objects can initialize an aggregate member from the braced-init list. In the next post, we explain what the aggregate member initialization is and what were the changes to it in modern C++ standards. The template is one of the great features of modern C++. They are simple and very powerful statement in […]

Read More

What Is std::quoted Quoted String In Modern C++?

Sometimes we want to preserve the string format especially when we use string in a string with /”. In C++14 and above, there is a std::quoted template that allows handling strings safely where they may contain spaces and special characters and it keeps their formatting intact. In this post, we explain std::quoted quoted strings in modern C++. What Is Quoted String In Modern C++? The std::quoted template is included in  header, and it is used to handle strings (i.e. “Let’s learn from ”LearnCPlusPlus.org!” “) safely where they may contain spaces and special characters and it keeps their formatting intact.  Here is the syntax,   std::quoted( )   Here are C++14 templates defined in where the string is used as input,   template quoted( const CharT* s, CharT delim = CharT(‘”‘), CharT escape = CharT(‘\’) );   or   template quoted(   const std::basic_string& s,   CharT delim = CharT(‘”‘), CharT escape = CharT(‘\’) );   Here is a C++14 template defined in where the string is used as output,   template quoted( std::basic_string& s, CharT delim=CharT(‘”‘), CharT escape=CharT(‘\’) );   Note that this feature is is using std::basic_string and it improved in C++17 by the  std::basic_string_view support. What Is std::quoted quoted string in modern C++? Here is an example that uses input string and outputs into a stringstream:   const std::string str  = “I say “LearnCPlusPlus!””; std::stringstream sstr; sstr std::quoted(str_out);  // output sstr to str_out   Is there a full example about std::quoted quoted string in modern C++? Here is a full example about std::quoted in modern C++. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31   #include #include #include   int main() { std::stringstream sstr; const std::string str  = “Let’s learn from “LearnCPlusPlus.org!” “; sstr

Read More

Structured Diagnostics in the New Problem Details Window

Structured Diagnostics in the New Problem Details Window Sy Brand October 11th, 20230 2 Massive compiler errors which seem impossible to navigate are the bane of many C++ developers’ lives. It’s up to tools to provide a better experience to help you comprehend diagnostics and understand how to fix the root issue. I wrote Concepts Error Messages for Humans to explore some of the design space and now, due to the hard work of many folks working on Visual Studio, we have a better experience to share with you all. You can read about some of the work which has led up to these changes in Xiang Fan’s blog post on the future of C++ diagnostics in MSVC and Visual Studio. In Visual Studio 2022 version 17.8 Preview 3, if you run a build using MSVC and an MSBuild project, entries in the Error List which have additional information available will show an icon in a new column named Details: If you click this button, a new Problem Details window will open up. By default this will be in the same place as the Error List, but if you move it around, Visual Studio will remember where you put it. This Problem Details window provides you with detailed, structured information about why a given problem occurred. If we look at this information we might think, okay, why could void pet(dog) not be called? If you click the arrow next to it, you can see why: In a similar way we can expand out other arrows to find out more information about our errors. This example is produced from code which uses C++20 Concepts and the Problem Details window gives you a way to understand the structure of Concepts errors. For those who would like to play around with this example, the code required to produce these errors is: struct dog {}; struct cat {}; void pet(dog); void pet(cat); template concept has_member_pet = requires(T t) { t.pet(); }; template concept has_default_pet = T::is_pettable; template concept pettable = has_member_pet or has_default_pet; void pet(pettable auto t); struct lizard {}; int main() { pet(lizard{}); } Make sure you compile with /std:c++20 to enable Concepts support. Output Window As part of this work we have also made the Output Window visualize any hierarchical structure in the output diagnostics. For example, here in an excerpt produced by building the previous example: 1>Source.cpp(18,6): 1>or ‘void pet(_T0)’ 1>Source.cpp(23,5): 1>the associated constraints are not satisfied 1> Source.cpp(18,10): 1> the concept ‘pettable’ evaluated to false 1> Source.cpp(16,20): 1> the concept ‘has_member_pet’ evaluated to false 1> Source.cpp(10,44): 1> ‘pet’: is not a member of ‘lizard’ 1> Source.cpp(20,8): 1> see declaration of ‘lizard’ 1> Source.cpp(16,41): 1> the concept ‘has_default_pet’ evaluated to false 1> Source.cpp(13,30): 1> ‘is_pettable’: is not a member of ‘lizard’ 1> Source.cpp(20,8): 1> see declaration of ‘lizard’ This change makes it much easier to scan large sets of diagnostics without getting lost. Code Analysis The Problem Details window is now also used for code analysis warnings which have associated Key Events. For example, consider this code which could potentially result in a use-after-move: #include void eat_string(std::string&&); void use_string(std::string); void oh_no(bool should_eat, bool should_reset) { std::string my_string{ “meow” }; bool did_reset{ false }; if (should_eat) { eat_string(std::move(my_string)); } if (should_reset) { did_reset = true; my_string = “the string is reset”; } […]

Read More

What Is Decltype (auto) In Modern C++ And How To Use It?

The auto keyword arrives with the new features of the C++11 and the standards above. In C++14, there is a new decltype that is used with auto keyword. In modern C++, the decltype(auto) type-specifier deduces return types while keeping their references and cv-qualifiers, while auto does not. In this post, we explain what decltype (auto) is in modern C++ and how to use it. What is auto in modern C++? The auto keyword is used to define variable types automatically, it is a placeholder type specifier (an auto-typed variable), or it can be used in a function declaration, or a structured binding declaration. If you want to learn more about auto keyword, here it is, What is decltype in modern C++? The decltype keyword and operator represents the type of a given entity or expression. This feature is one of the C++11 features added to compilers (including BCC32 and other CLANG compilers). In a way you are saying “I am declaring this variable to be the same type as this other variable“. Here are more details about how you can use it, How to use decltype (auto) in modern C++? In C++14, there is a new decltype feature that allows you to use with the auto keyword. In C++14 and standards above, the decltype(auto) type-specifier deduces return types while keeping their references and cv-qualifiers, while auto does not. Since C++14, here is the syntax,   type_constraint (optional) decltype ( auto )   In this syntax, the type is decltype(expr) and expr can be an initializer or a return statement. Here is a simple example how we can use it,   decltype(auto) x = i;   Are there some simple examples about decltype (auto) in modern C++? Here are some simple examples that shows difference between auto and decltype(auto), In C++14 and above, we can use decltype(auto) with const int values as below,   const int x = 4096; auto xa = x;    // xa : int decltype(auto) xb = x;  // xb : const int   In C++14 and above, we can use decltype(auto) with int& values as below,   int y = 2048; int& y0 = y;  // y_ : int auto ya = y0;  // yc2 : int decltype(auto) yb = y0;  // yb : int&   In C++14 and above, we can use decltype(auto) with int values as below,   int&& z = 1024; auto zm = std::move(z);   // zm : int decltype(auto) zm2 = std::move(z);  // zm2 : int&&   In C++11 and above, we can use auto for return types,   auto myf(const int& i) { return i; // auto return type : int }   In C++14 and above, we can use decltype(auto) for return types,   decltype(auto) myf2(const int& i) {    return i; // decltype(auto) return type : const int& }   Is there a full example about decltype (auto) in modern C++? Here is a full example that shows how you can use auto and decltype(auto) in different int types. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54   #include   auto myf(const int& i) { return i; // auto […]

Read More

Learn How to Use New And Delete Elisions In C++

The C++14 standard (and later), brings a lot of useful smart pointers and improvements on them. They help us to avoid the mistake of incorrectly freeing the memory addressed by pointers. In modern C++, we have the new and delete operators that are used to allocate and free objects from memory, and in this post, we explain how to use new and delete operators. Can we use new and delete elisions in modern C++ or is it obsolete? Allocating memory and freeing it safely is hard if you are programming a very big application. In Modern C++, there are smart pointers that help avoid the mistake of incorrectly freeing the memory addressed by pointers. Smart pointers make it easy to define pointers, they came with C++11. The most used types of C++ smart pointer are unique_ptr, auto_ptr, shared_ptr, and weak_ptr. Smart pointers are preferable to raw pointers in many different scenarios, but there is still a lot of need to use for the new and delete methods in C++14 and above. When we develop code that requires in-place construction, we need to use new and, possibly, delete operations. They are useful, in a memory pool, as an allocator, as a tagged variant, as a buffer, or as a binary message to a buffer. Sometimes we can use new and delete for some containers if we want to use raw pointers for storage. Modern C++ has a lot of modern choices for faster and safer memory operations. Generally, developers choose to use unique_ptr/make_unique and make_shared rather than raw calls to new and delete. Even though we have a lot of standard smart pointers and abilities, we still need new and/or delete operators. How can we use new and delete elisions in C++? What is the new operator in C++? The new operator in C++, denotes a request for memory allocation on the free memory. If there is sufficient memory, a new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.    = new ;   Here, the data_type could be any built-in data type, i.e. basic C / C++ types, array, or any class types i.e. class, structure, union.  Now, let’s see how we can use new operator. We can simply use auto and new to create a new pointer and space for its buffer as below,   auto *ptr = new long int;     or in old style still you can use its type in definition instead of auto as below,   long int *ptr = new long int;     one of the great feature of pointers is we don’t need to allocate them at the beginning, we can set them NULL, and we can allocate them in some steps as below,   long int *ptr = NULL; … ptr = new long int;     What is the delete operator in C++? The delete operator in C++ is used to free the dynamically allocated array pointed by pointer variable. Here is the syntax for the delete operator,   delete ;   we can use delete[] for the pointer_arrays, here is the syntax for them,   delete[] ;   here is how we can use delete to delete a pointer, and this is how we can delete pointer arrays,   int *arr […]

Read More