Skip to main content

Posts

Showing posts from April, 2020

C++ delete vs delete[]

In C++ both delete and delete[] are used deallocate storage allocated via dynamic meory allocation. However delete is used to deallocate storage of a single object and delete[] used to deallocate storage of an array. If you allocate memory using new keyword make sure to deallocate memory using delete  keyword to avoid memory leaks. If you allocate an array of memory using new[]  deallocate memory using delete[]  keyword. If you allocate memory using new[] and use delete for deallocation in C++ it is undefined behavior. As per my experience compiler will not give any error, however there will be memory leak as only the first object will be deallocated. Also, it is undefined behavior for both delete and delete[] if the value of the pointer in invalid. Both of these operations are safe if the pointer is eqaul to NULL / nullptr . So as a best practice it is good to assign nullptr once the memory is deallocated to avoid undefined behavoir. Few more important point...

C++ Access Specifiers and Inheritance

C++ has three types of access specifiers public, protected and private. The below explanations assume that there are no friendship declarations are made between classes. Public Inheritance Public and protected members listed in base class keep there access specifiers in the derived class. Means public members of base class are public in derived class and protected members of the base class are protected in the derived class. Private members of the base class cannot be access in the derived class. Protected Inheritance Public and protected members listed in base class becomes protected members of the derived class. Private members of the base class cannot be access in the derived class. Private Inheritance Public and protected members listed in base class becomes private members of the derived class. Private members of the base class cannot be access in the derived class. Also, please note if there is no access specifier defined in the base class all the members are private b...

Welcome to C++ Junkie

C++ is one of the most popular programming languages in the world. I believe if you get the basics right learning C++ and becoming an expert is easy. I have been working with C++ for more than 7 years now. My intention is to share C++ related stuff that I come across in my day to day work. To get started here is how you write the simplest program to print an output in the console.