Skip to main content

Posts

C++ Performance Optimization - Using Better Algorithms

Performance optimization is a hot topic in C++ language. I'm going to talk about this topic from simple to advanced techniques in a series of articles. Some tips can be simple but from a performance point of view the impact can be huge.
Recent posts

C++ String to Int

Converting a string to int can be useful in any size of C++ application. There are several ways that can be used to convert a string to int in C++. Depending on your scenario you can choose the best method. However, most widely used method is std::stoi for C++11 or above for development.

C++ Exponents

According to mathematics exponent is a number indicates how many times to multiply the base number. When it comes to C++ exponents every programmer should know two std functions, std::pow and std::exp . To use these methods you should include cmath.

C++ Double to Int

In day to day programming we will need to convert a double value to an int value in different use cases. Also, I'm going to talk about how to avoid compiler warning C4244 when converting double to int.

C++ Is It Safe to Delete a void Pointer?

According to C++ Standard deleting a void* (void pointer) is undefined behavior. However usually the program will compile and run without any errors. Also the memory pointed by the pointer will be cleared. But deleting a void pointer will not call destructors.

C++ Is It Safe to Delete a NULL pointer?

Simple answer is YES . It is perfectly safe to delete a null pointer. In C++ delete operator is used to deallocate the memory block pointed by the pointer by releasing the memory allocated via new operator. For basic information on delete operator you can visit this article . 

C++ Switch vs If Else Performance

In this article I'm not going to explain the basics of switch and if else statements. But I'm going give some points based on my experience from a performance point of view. When it comes to performance my rule of thumb is always measure the execution time and decide which solution perform better rather than going with theory. Because you might not know how the complier will optimize or based on other factors it might not work as per the theory.

C++ Find Middle Element of a Linked List

This is a famous coding interview question that every programmer suppose to know. Here I am going to look at two different answers. First one is the obvious one, of course not the answer that the interviewer is looking for. Second one is the smart answer.

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 points on delete an

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.