Found 7346 Articles for C++

How to call a virtual function inside constructors in C++?

V Jyothi
Updated on 24-Jun-2020 06:17:10

608 Views

Calling virtual functions from a constructor or destructor is dangerous and should be avoided whenever possible. This is because the virtual function you call is called from the Base class and not the derived class.In C++ every class builds its version of the virtual method table prior to entering its own construction. So a call to the virtual method in the constructor will call the Base class' virtual method. Or if it has no implementation at that level, it'll produce a pure virtual method call. Once the Base is fully constructed, the compiler starts building Derived class, and overrides the ... Read More

What is a segmentation fault in C/C++?

Priya Pallavi
Updated on 27-Jan-2020 12:35:13

9K+ Views

A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.Seg faults are mostly caused by pointers that are −Used to being properly initialized.Used after the memory they point to has been reallocated or freed.Used in an indexed array where the index is outside of the array bounds.

How to debug a core in C/C++?

Ankith Reddy
Updated on 24-Jun-2020 06:17:58

325 Views

A process dumps core when it is terminated by the operating system due to a fault in the program. The most typical reason this occurs is that the program accessed an invalid pointer value like NULL or some value out of its memory area. As part of that process, the operating system tries to write our information to a file to allow us to analyze what happened.This core can be used as follows to diagnose and debug our program −The core is dumped to the /proc/sys/kernel directory by default. To debug a core, the program must be compiled with the ... Read More

What does the explicit keyword mean in C++?

Nikitha N
Updated on 24-Jun-2020 06:18:59

3K+ Views

The explicit keyword in C++ is used to mark constructors to not implicitly convert types. For example, if you have a class Foo −class Foo { public:     Foo(int n); // allocates n bytes to the Foo object     Foo(const char *p); // initialize object with char *p };Now if you tryFoo mystring = 'x';The char 'x' is implicitly converted to int and then will call the Foo(int) constructor. But this is not what was intended. So to prevent such conditions and make the code less error-prone, define the constructor as explicit −Example class Foo {    public:   ... Read More

How to convert an int to string in C++?

George John
Updated on 24-Jun-2020 06:19:44

3K+ Views

You can use the itoa function from C to convert an int to string.  example#include int main() {    int a = 10;    char *intStr = itoa(a);    string str = string(intStr);    cout

How can I get the list of files in a directory using C/C++?

Chandu yadav
Updated on 27-Jan-2020 12:32:32

2K+ Views

Standard C++ doesn't provide a way to do this. You could use the system command to initialize the ls command as follows −Example#include int main () {    char command[50] = "ls -l";    system(command);    return 0; }OutputThis will give the output −-rwxrwxrwx 1 root root  9728 Feb 25 20:51 a.out -rwxrwxrwx 1 root root   131 Feb 25 20:44 hello.cpp -rwxrwxrwx 1 root root   243 Sep  7 13:09 hello.py -rwxrwxrwx 1 root root 33198 Jan  7 11:42 hello.o drwxrwxrwx 0 root root   512 Oct  1 21:40 hydeout -rwxrwxrwx 1 root root    42 Oct 21 11:29 ... Read More

What is the difference between "std::endl" and "" in C++?

Srinivas Gorla
Updated on 30-Jul-2019 22:30:22

291 Views

"" Outputs a newline (in the appropriate platform-specific representation, so it generates a "\r" on Windows), but std::endl does the same AND flushes the stream. Usually, you don't need to flush the stream immediately and it'll just cost you performance, so, for the most part, there's no reason to use std::endl.When you want to flush the stream manually because you expect your output to be made visible to the user in a timely manner(ie without delay), you should use std::endl instead of writing '' to the stream.

Is segmentation fault actual undefined behavior in C++?

Arjun Thakur
Updated on 24-Jun-2020 06:08:41

350 Views

Undefined behavior is a way to give freedom to implementors (e.g. of compilers or of OSes) and to computers to do whatever they "want", in other words, to not care about consequences.The cases in which segmentation fault occurs are transient in nature. They won't always result in a segmentation fault but can also run correctly(or at least appear to). For example, consider the following code fragment −#include int main() {     int arr[2];     arr[0] = 0;     arr[1] = 1;     arr[2] = 2; // Undefined behaviour     arr[3] = 3; // Undefined behaviour ... Read More

Sorting a vector of custom objects using C++ STL

Ankith Reddy
Updated on 12-Feb-2020 06:19:23

12K+ Views

You can sort a vector of custom objects using the C++ STL function std::sort. The sort function has an overloaded form that takes as arguments first, last, comparator. The first and last are iterators to first and last elements of the container. The comparator is a predicate function that can be used to tell how to sort the container. example#include #include #include using namespace std; struct MyStruct {    int key;    string data;    MyStruct(int key, string data) {       this -> key = key;       this -> data = data;    } }; int ... Read More

How to execute a command and get the output of command within C++ using POSIX?

Abhinanda Shri
Updated on 12-Feb-2020 06:15:52

11K+ Views

You can use the popen and pclose functions to pipe to and from processes. The popen() function opens a process by creating a pipe, forking, and invoking the shell. We can use a buffer to read the contents of stdout and keep appending it to a result string and return this string when the processes exit.example#include #include #include #include using namespace std; string exec(string command) {    char buffer[128];    string result = "";    // Open pipe to file    FILE* pipe = popen(command.c_str(), "r");    if (!pipe) {       ... Read More

Advertisements