Found 7347 Articles for C++

What is Memory Leak in C/C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

The memory leak occurs, when a piece of memory which was previously allocated by the programmer. Then it is not deallocated properly by programmer. That memory is no longer in use by the program. So that place is reserved for no reason. That’s why this is called the memory leak.For the memory leak, some block of memory may have wasted. If the system has enough memory, in that case also this may slow down the performance.Examplevoid my_func() {    int *data = new int;    *data = 50; }Here the problem is *data pointer is never deleted, so memory is ... Read More

Parameter Passing Techniques in C/C++

Samual Sam
Updated on 30-Jul-2019 22:30:26

6K+ Views

In C we can pass parameters in two different ways. These are call by value, and call by address, In C++, we can get another technique. This is called Call by reference. Let us see the effect of these, and how they work.First we will see call by value. In this technique, the parameters are copied to the function arguments. So if some modifications are done, that will update the copied value, not the actual value.Example#include using namespace std; void my_swap(int x, int y) {    int temp;    temp = x;    x = y;    y = ... Read More

Using range in switch case in C/C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

6K+ Views

In C or C++, we have used the switch-case statement. In the switch statement we pass some value, and using different cases, we can check the value. Here we will see that we can use ranges in the case statement.The syntax of using range in Case is like below −case low … highAfter writing case, we have to put lower value, then one space, then three dots, then another space, and the higher value.In the following program, we will see what will be the output for the range based case statement.Example#include main() {    int data[10] = { 5, ... Read More

Difference between “int main()” and “int main(void)” in C/C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

3K+ Views

Sometimes we see that there are two types of main function definition. The int main() and int main(void). So is there any difference?In C++, there is no difference. In C also both are correct. But the second one is technically better. It specifies that the function is not taking any argument. In C if some function is not specified with the arguments, then it can be called using no argument, or any number of arguments. Please check these two codes. (Remember these are in C not C++)Example#include void my_function() {    //some task } main(void) {    my_function(10, "Hello", "World"); ... Read More

What is long long in C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

6K+ Views

In some cases we use long long in C or C++. Here we will see what is basically long long is? The long long takes twice as much memory as long. In different systems, the allocated memory space differs. On Linux environment the long takes 64-bit (8-bytes) of space, and the long long takes 128-bits (16-bytes) of space. This is used when we want to deal with some large value of integers.We can test the size of different types using this simple program.Example#include using namespace std; main() {    int a;    long b;    long long c;    cout

Bool to int conversion in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

10K+ Views

Here we will see how to convert bool to int equivalent in C++. Bool is a datatype in C++, and we can use true or false keyword for it. If we want to convert bool to int, we can use typecasting. Always true value will be 1, and false value will be 0.Example#include using namespace std; main() {    bool my_bool;    my_bool = true;    cout

How to check if an input is an integer using C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to check whether a given input is integer string or a normal string. The integer string will hold all characters that are in range 0 – 9. The solution is very simple, we will simply go through each characters one by one, and check whether it is numeric or not. If it is numeric, then point to the next, otherwise return false value.Example#include using namespace std; bool isNumeric(string str) {    for (int i = 0; i < str.length(); i++)       if (isdigit(str[i]) == false)       return false; //when one ... Read More

How to create a high resolution timer with C++ and Linux?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

2K+ Views

To create high resolution timer we can use the chrono library. This library has high resolution clock. This can count in nanoseconds.In this program we will see the execution time in nanoseconds. We will take the time value at first, then another time value at the last, then find the difference to get elapsed time. Here we are using blank loop to pause the effect for sometimes.Example#include #include typedef std::chrono::high_resolution_clock Clock; main(){    auto start_time = Clock::now();    for(int i = 0; i

How can I create directory tree using C++ in Linux?

Samual Sam
Updated on 30-Jul-2019 22:30:26

1K+ Views

In this section we will see how to create a directory tree using C++ code in Linux. In Linux terminal we can put some command like “mkdir –p /dir/dir1/dir2” Here –p is used to mark as parent (recursively create inner directories).In C++ code we can use some libraries of Linux system. Then we can use Linux terminal commands as string argument of the system() function. We can create directory tree like this.Example#include #include #include #include using namespace std; int main() {    int status;    status = system("mkdir -p TP/My_Folder/test"); // Creating a directory    if ... Read More

How can I clear console using C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

11K+ Views

We can clear the console using C++ code. To do this we have to execute some system commands. In Linux systems, the POSIX is used. We can call system() function to execute system command. For clearing the console in linux, we can use “clear” command. This will be passed inside the system() function.Let us see the code to get the better idea.Example#include using namespace std; int main () {    cout

Advertisements