Found 7346 Articles for C++

Dangling, Void, Null and Wild Pointers in C++

George John
Updated on 30-Jul-2019 22:30:25

3K+ Views

Dangling pointerDangling pointer is a pointer pointing to a memory location that has been freed (or deleted). There are different ways where Pointer acts as dangling pointerFunction CallThe pointer pointing to local variable becomes dangling when local variable is not static.int *show(void) {    int n = 76; /* ... */ return &n; }OutputOutput of this program will be garbage address.De-allocation of memoryint main() {    float *p = (float *)malloc(sizeof(float));    //dynamic memory allocation.    free(p);    //after calling free()    p becomes a dangling pointer p = NULL;    //now p no more a dangling pointer. }Variable goes ... Read More

Compare *ptr++, *++ptr and ++*ptr in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this section, we will see what are the differences between *ptr++, *++ptr and ++*ptr in C++.Here we will see the precedence of postfix++ and prefix++ in C or C++. The precedence of prefix ++ or -- has higher priority than dereference operator ‘*’ and postfix ++ or -- has a priority higher than both prefix ++ and dereference operator ‘*’.When ptr is a pointer, then *ptr++ indicates *(ptr++) and ++*prt refers ++(*ptr)Example Code Live Demo#include using namespace std; int main() {    char arr[] = "Hello World";    char *ptr = arr;    ++*ptr;    cout

Standard Size of character ('a') in C/C++ on Linux

Anvi Jain
Updated on 30-Jul-2019 22:30:25

4K+ Views

In C++ the size of the character literal is char. In C the type of character literal is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.Example Code Live Demo#include main() {    printf("%d", sizeof('a')); }Output1Example Code Live Demo#include using namespace std; main() {    cout

How do you declare an interface in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

349 Views

An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.A class is made abstract by declaring at least one of its functions as a pure virtual function. A pure virtual function is specified by placing "= 0" in its declaration as follows −class Box {    public:       // pure virtual function       virtual double getVolume() ... Read More

Which one is better in between pass by value or pass by reference in C++?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

383 Views

In C++ we can pass arguments into a function in different ways. These different ways are −Call by ValueCall by ReferenceCall by AddressSometimes the call by address is referred to as call by reference, but they are different in C++. Incall by address, we use pointer variables to send the exact memory address, but in call by reference we pass the reference variable (alias of that variable). This feature is not present in C, there we have to pass the pointer to get that effect.In this section, we will see what are the advantages of call by reference over call ... Read More

Find out the current working directory in C/C++

Anvi Jain
Updated on 30-Jul-2019 22:30:25

6K+ Views

In this section, we will see how to get the current working directory using C or C++. We have defined some flags for the current operating system.Example Code Live Demo#ifdef WINDOWS #include #define GetCurrentDir _getcwd #else #include #define GetCurrentDir getcwd #endif #include using namespace std; std::string get_current_dir() {    char buff[FILENAME_MAX]; //create string buffer to hold path    GetCurrentDir( buff, FILENAME_MAX );    string current_working_dir(buff);    return current_working_dir; } main() {    cout

Dangling, Void, Null and Wild Pointers in C/C++

Chandu yadav
Updated on 30-Jul-2019 22:30:25

619 Views

Dangling pointerDangling pointer is a pointer pointing to a memory location that has been freed (or deleted). There are different ways where Pointer acts as dangling pointerFunction CallThe pointer pointing to local variable becomes dangling when local variable is not static.int *show(void) {    int n = 76; /* ... */ return &n; }OutputOutput of this program will be garbage address.De-allocation of memory#include #include int main() {    float *p = (float *)malloc(sizeof(float));    //dynamic memory allocation. free(p);    //after calling free() p becomes a dangling pointer p = NULL;    //now p no more a dangling pointer. ... Read More

Single quotes vs. double quotes in C or C++

Smita Kapse
Updated on 30-Jul-2019 22:30:25

7K+ Views

In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character ‘x’ and a null terminator ‘\0’. So “x” is two-character array in this case.In C++ the size of the character literal is char. In C the type of character literal is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.

C++ Program to Print the Kind of Rotation the AVL Tree is Undergoing When you Add an Element or Delete an Element

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

95 Views

AVL tree is a self-balancing Binary Search Tree where the difference between heights of left and right subtrees cannot be more than one for all nodesTree rotation is an operation that changes the structure without interfering with the order of the elements on an AVL tree. It moves one node up in the tree and one node down. It is used to change the shape of the tree, and to decrease its height by moving smaller subtrees down and larger subtrees up, resulting in improved performance of many tree operations. The direction of a rotation depends on the side which ... Read More

Why use static_cast(x) instead of (int)x in C++?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

The (int)x is C style typecasting where static_cast(x) is used in C++. This static_cast() gives compile time checking facility, but the C style casting does not support that. This static_cast() can be spotted anywhere inside a C++ code. And using this C++ cast the intensions are conveyed much better.In C like cast sometimes we can cast some type pointer to point some other type data. Like one integer pointer can also point character type data, as they are quite similar, only difference is character has 1-byte, integer has 4-bytes. In C++ the static_cast() is more strict than C like casting. ... Read More

Advertisements