Found 7197 Articles for C++

C++ Program to Implement Sparse Array

Tapas Kumar Ghosh
Updated on 01-Jul-2025 15:30:33

3K+ Views

What is Sparse Array?Sparse array is used to determines the dimension of an array in which most of the elements are zero. It can be used for matrix calculation. Characteristics of Sparse Array Following are list of points that defines the characteristics of a sparse array: Most of the elements are zero or null values which means it is unused. Only non-zero elements and their indexes are stored. It is used to save the memory while comparing to normal array calculation. Example The Sparse array of ... Read More

C++ Program to Find the maximum subarray sum using Binary Search approach

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

330 Views

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the ... Read More

C++ Program to Solve Travelling Salesman Problem for Unweighted Graph

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

4K+ Views

Travelling Salesman Problem use to calculate the shortest route to cover all the cities and return back to the origin city. This method is use to find the shortest path to cover all the nodes of a graph.This is the program to find shortest route of a unweighted graph.AlgorithmBegin    Define a variable vr = 4 universally.    Declare an integer function TSP to implement Travelling salesman Problem.    Declare a graph grph[][] as a 2D matrix and variable p to the integer datatype.    Pass them as a parameter.    Declare variable ver to the vector datatype.    for ... Read More

Proper stack and heap usage in C++?

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

704 Views

The stack − All variables declared inside the function will take up memory from the stack. So, any local variable inside a function lives on the stack.The heap − This is unused memory of the program and can be used to allocate the memory dynamically when program runs. So If we want something to live longer than the function that declared it, we must allocate it on the heap.Exampleint main() {    int a; //get memory allocated on stack.    int *ptr=new int[7]; //memory for 7 integers allocated on heap. }The main issue in heap memory is fragmentation whereas memory ... Read More

Why do we pass a Pointer by Reference in C++?

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

824 Views

If we need to modify a pointer rather than the object that the pointer is pointing to, we pass a pointer by reference.Here is an example of how to pass a pointer by reference −Example Live Demo#include using namespace std; void Decrement( int*& d ) {    --d; } int main( void ) {    int a = 26;    int* ptr = &a; // pointer to pass    // print before decrement    cout

Why do we check for a NULL pointer before deleting in C/C++?

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

519 Views

It is basically pointless to check for a NULL pointer before deleting. Deleting a pointer will do nothing if the pointer set to NULL. It might be the reason to check for the NULL pointer that deleting a pointer which is already set to NULL may indicate bugs in the program.

Why are NULL pointers defined differently in C and C++?

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

217 Views

In C++, a null pointer can be defined by as a null pointer constant is an integer constant expression with the value 0, like −int*p = 0;But in c, a null pointer can be defined by as a null pointer constant is an integer constant expression with the value 0 or such an expression cast to void*, like −Int *p = 0;;Orint*p = (void*) 0;In C++11 a keyword “nullptr” is used to represent nullpointer.int* ptr = nullptr;In CExample Live Demo#include int main() {    int *p= NULL; //initialize the pointer as null.    printf("The value of pointer is %u", p); ... Read More

What is difference between a pointer and reference parameter in C++?

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

6K+ Views

PointersPointer variables are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer=variable name;ReferencesWhen a parameter is declared as reference, it becomes an alternative name for an existing parameter.SyntaxType &newname=existing name;InitializationType &pointer; Pointer=variable name;The main differences between pointers and reference parameters are −References are used to refer an existing variable in another name whereas pointers are used to store address of variable.References cannot have a null value assigned but pointer can.A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.A reference must be initialized on declaration while it is not ... Read More

Scope Resolution Operator vs this pointer in C++

Tapas Kumar Ghosh
Updated on 16-Jun-2025 17:00:00

496 Views

Both scope resolution operator and 'this' pointer are used for different purposes in the C++ programs. The scope resolution operator is used to access static or class members whereas this pointer is used to access object members when there is a local variable with the same name. Scope Resolution Operator in C++ The concept of scope resolution operator (::) is used in Object-Oriented Programming (OOPs) and namespace standard that can used to access something belongs to a specific scope, such as a class or namespace. Syntax It's syntax is as follows: :: Example In this program, we use ... Read More

RAII and smart pointers in C++

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

2K+ Views

RAII in C++RAII (Resource Acquisition Is Initialization) is C++ technique which control the life cycle of resource. It is a class variant and is tied to object life time.It encapsulates several resources into class where resource allocation is done by constructor during object creation and resource deallocation is done by destructor during object destruction.Resource is guaranteed to be held till the object is alive.Examplevoid file_write {    Static mutex m; //mutex to protect file access    lock_guard lock(m); //lock mutex before accessing file    ofstream file("a.txt");    if (!file.is_open()) //if file is not open    throw runtime_error("unable to open file"); ... Read More

Advertisements