Found 7347 Articles for C++

RAII and smart pointers in C++

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

1K+ 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

Pointers, smart pointers and shared pointers in C++

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

4K+ Views

PointersPointers are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer = variable name;Functionspointers are used to store address of variable.pointers can have a null value assigned.pointer can be referenced by pass by reference.a pointer has its own memory address and size on the stack.Example Live Demo#include using namespace std; int main() {    // A normal integer variable    int a = 7;    // A pointer variable that holds address of a.    int *p = &a;    // Value stored is value of variable "a"    cout

Is it safe to delete a void pointer in C/C++?

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

890 Views

Void pointer is a pointer which is not associate with any data types. It points to some data location in storage means points to the address of variables. It is also called general purpose pointer.It is not safe to delete a void pointer in C/C++ because delete needs to call the destructor of whatever object it's destroying, and it is impossible to do that if it doesn't know the type.Here is a simple example of void pointer −Example Live Demo#include int main() {    int a = 7;    float b = 7.6;    void *p;    p = &a;    printf("Integer variable is = %d",  *( (int*) p) );    p = &b;    printf("Float variable ... Read More

How to declaring pointer variables in C/C++?

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

415 Views

A pointer is used to store the address of the variables. To declare pointer variables in C/C++, an asterisk (*) used before its name.Declaration*pointer_nameIn CExample Live Demo#include int main() {    // A normal integer variable    int a = 7;    // A pointer variable that holds address of a.    int *p = &a;    // Value stored is value of variable "a"    printf("Value of Variable : %d", *p);    //it will print the address of the variable "a"    printf("Address of Variable : %p", p);    // reassign the value.    *p = 6;    printf("Value ... Read More

How to compare pointers in C/C++?

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

7K+ Views

We can compare pointers if they are pointing to the same array. Relational pointers can be used to compare two pointers. Pointers can’t be multiplied or divided.In CExample Live Demo#include int main() {    int *p2;    int *p1;    p2 = (int *)300;    p1 = (int *)200;    if(p1 > p2) {       printf("P1 is greater than p2");    } else {       printf("P2 is greater than p1");    }    return(0); }OutputP2 is greater than p1In C++Example#include using namespace std; int main() {    int *p2;    int *p1;    p2 = (int *)300;    p1 = (int *)200;    if(p1>p2) {       cout

Function pointer to member function in C++

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

12K+ Views

In C++ , function pointers when dealing with member functions of classes or structs, it is invoked using an object pointer or a this call. We can only call members of that class (or derivatives) using a pointer of that type as they are type safe.Example Live Demo#include using namespace std; class AB {    public:       int sub(int a, int b) {          return a-b;       }       int div(int a, int b) {          return a/b;       } }; //using function pointer int res1(int ... Read More

Are there benefits of passing by pointer over passing by reference in C++?

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

431 Views

A pointer can receive a null parameter whereas a reference can’t. You can only use pointer if you want to pass “no object”.Explicitly passing by pointer allow us to see the whether the object is passes by reference or value at call site.These are simple example of passing by pointer and passing by reference −Passing by pointerExample Live Demo#include using namespace std; void swap(int* a, int* b) {    int c = *a;    *a= *b;    *b = c; } int main() {    int m =7 , n = 6;    cout

Applications of Pointers in C/C++

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

1K+ Views

To access array elementsWe can access array elements by using pointers.In CExample Live Demo#include int main() {    int a[] = { 60, 70, 20, 40 };    printf("%d", *(a + 1));    return 0; }Output70In C++Example Live Demo#include using namespace std; int main() {    int a[] = { 60, 70, 20, 40 };    cout

Removing an element from C++ std::vector<> by index?

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

280 Views

Remove an element from C++ std::vector by index can be done by following way −Example Live Demo#include #include using namespace std; int main() {    vector v; //declare vector    //insert elements into vector    v.push_back(-10);    v.push_back(7);    v.push_back(6);    // Deletes the first element (v[0])    v.erase(v.begin() );    for (int i = 0; i < v.size(); i++)       cout

The best way to check if a file exists using standard C/C++

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

15K+ Views

The only way to check if a file exist is to try to open the file for reading or writing.Here is an example −In CExample#include int main() {    /* try to open file to read */    FILE *file;    if (file = fopen("a.txt", "r")) {       fclose(file);       printf("file exists");    } else {       printf("file doesn't exist");    } }Outputfile existsIn C++Example#include #include using namespace std; int main() {    /* try to open file to read */    ifstream ifile;    ifile.open("b.txt");    if(ifile) {       cout

Advertisements