Found 34484 Articles for Programming

When can I use a forward declaration in C/C++?

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

712 Views

In C++ the forward declaration lets the code following the declaration know that there is are classes with the name Person. This satisfies the compiler when it sees these names used. Later the linker will find the definition of the classes.Example CodeClass Person; void myFunc(Person p1) { // ... } Class Person { // Class definition here };So in this case when compiler encounters myFunc, it'll know that it is going to encounter this class somewhere down in the code. This can be used in cases where code using the class is placed/included before the code containing the class definition.

C++ Program to Implement Segmented Sieve to Generate Prime Numbers Between Given Range

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

769 Views

This is C++ program to implement Segmented Sieve to Generate Prime Numbers Between Given Range. Segmented Sieve first uses Simple Sieve to find primes smaller than or equal to √(n). The idea of this algorithm is to divide the range [0 ... n-1] in different segments and compute primes in all segments one by one.AlgorithmBegin    Create function to find all primes smaller than limit    using simple sieve of eratosthenes.    Finds all prime numbers in given range using    segmented sieve    A) Compute all primes smaller or equal to square root of high using simple sieve   ... Read More

C++ Program to Implement Sieve of Atkin to Generate Prime Numbers Between Given Range

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

375 Views

This is C++ program to implement Sieve of Atkin to Generate Prime Numbers Between Given Range. The Sieve of Atkin is a modern algorithm for finding all prime numbers up to a specified integer.AlgorithmBegin    Create a results list, filled with 2, 3, and 5.    Initialize the sieve array with false values    Mark siev[n] is true if one of the following is true:    a) n = (4*x*x) + (y*y) has odd number of solutions       n % 12 = 1 or n % 12 = 5.    b) n = (3*x*x) + (y*y) has odd ... Read More

Why isn't sizeof for a struct equal to the sum of sizeof of each member in C/C++?

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

724 Views

The size of a struct type element taken by sizeof() is not always equal to the size of each individual member. Sometimes the compilers add some padding to avoid alignment issues. So the size may change. The padding is added when a structure member is followed by a member with a larger size or at the end of the structure. Different compiler has different types of alignment constraints. In C standard, the total alignment structure depends on the implementation.Case 1In this case the double z is 8-byte long, which is larger than x (4-byte). So another 4-byte padding is added. ... Read More

What are copy elision and return value optimization in C++?

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

205 Views

The Copy Elision is also known as the Copy Omission. This is one of the compiler optimization technique. It avoids the unnecessary copying of objects. Almost any current compiler uses this Copy Elision technique.Let us see how it works by the help of one example code.Example Code#include using namespace std; class MyClass { public: MyClass(const char* str = "\0") { //default constructor cout

How can I profile C++ code running on Linux?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

341 Views

In Linux platform there are many great profiling tools for profiling C++ programs. Valgrind is one of them. It is widely used. It is a programming tool for memory debugging, memory leak detection, and profiling. We can use Valgrind by passing the binary to it and setting the tool to callgrind. First generate the binary by compiling the program$ g++ -o abc.cpp abcNow use valgrind to profile it$ valgrind --tool=callgrind ./abcThis will generate a file called callgrind.out.x. You can read this file using a tool called kcachegrind.If you're using gcc, you can use the inbuilt profiling tool, gprof. You can ... Read More

Why is iostream::eof inside a loop condition considered wrong?

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

140 Views

The iostream::eof in a loop is considered as wrong because we haven’t reached the EOF. So it does not mean that the next read will succeed.When we want to read a file using file streams in C++. And when we use a loop to write in a file, if we check the end of file using stream.eof(), we are actually checking whether the file has reached end or not.Example Code#include #include using namespace std; int main() { ifstream myFile("myfile.txt"); string x; while(!myFile.eof()) { myFile >> ... Read More

C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

2K+ Views

This is C++ program to implement Sieve of Eratosthenes to Generate Prime Numbers Between Given Range. In this method, an integer array with all elements initializes to zero.It follows where each non-prime element’s index is marked as 1 inside the nested loops. The prime numbers are those whose index is marked as 0.AlgorithmBegin    Declare an array of size n and initialize it to zero    Declare length, i, j    Read length    For i = 2 to n-1 do       For j = i*i to n-1 do          Arr[j-1]=1       Done ... Read More

C Program to display hostname and IP address

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

3K+ Views

In this section we will see how to see the Host name and IP address of the local system in an easier way. We will write a C program to find the host name and IP.Some of the following functions are used. These functions have different task. Let us see the functions and their tasks.FunctionDescriptiongethostname()It finds the standard host name for the local computer.gethostbyname()It finds the host information corresponding to a host name from host databaseiten_ntoa()It converts an IPv4 Internet network address into an ASCII string into dotted decimal format.Example Code#include #include #include #include #include ... Read More

C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

252 Views

Wheel Sieve method is used to find prime number between a given range. Wheel factorization is a graphical method for manually performing a preliminary to the Sieve of Eratosthenes that separates prime numbers from composites.In this method, Prime numbers in the innermost circle have their Multiples in similar positions as themselves in the other circles, forming spokes of primes and their multiples. Multiple of these prime numbers in the innermost circle form spokes of composite numbers in the outer circles.AlgorithmBegin    Define max number    gen_sieve_primes()    Declare c    Assign c = 2    For p = 2 to ... Read More

Advertisements