Found 7197 Articles for C++

How to return local array from a C++ function?

Ankith Reddy
Updated on 26-Jun-2020 14:13:36

835 Views

A local array cannot be directly returned from a C++ function as it may not exist in memory after the function call. A way to resolve this is to use a static array in the function. As the lifetime of the static array is the whole program, it can easily be returned from a C++ function without the above problem.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int *retArray() {    static int arr[10];    for(int i = 0; i

When to use new operator in C++ and when it should not be used?

Ankith Reddy
Updated on 26-Jun-2020 13:58:34

8K+ Views

Use of the new operator signifies a request for the memory allocation on the heap. If the sufficient memory is available, it initializes the memory and returns its address to the pointer variable.The new operator should only be used if the data object should remain in memory until delete is called. Otherwise if the new operator is not used, the object is automatically destroyed when it goes out of scope. In other words, the objects using new are cleaned up manually while other objects are automatically cleaned when they go out of scope.The following is the syntax of new operator.pointer_variable ... Read More

When do function-level static variables get initialized in C/C++?

George John
Updated on 26-Jun-2020 13:59:45

840 Views

Static variables can be defined using the static keyword. They are variables that remain in memory while the program is running i.e. their lifetime is the entire program run. This is different than automatic variables as they remain in memory only when their function is running and are destroyed when the function is over.Function-level static variables are created and initialized the first time that they are used although the memory for then is allocated at program load time.A program that demonstrates function-level static variables in C is given as follows −Example Live Demo#include int func() {    static int num = ... Read More

When are static C++ class members initialized?

Arjun Thakur
Updated on 26-Jun-2020 13:41:03

552 Views

Static C++ class members can be defined using the static keyword. The static member in a class is shared by all the class objects as there is only one copy of the static class member in the memory, regardless of the number of objects of the class.The static class member is initialized to zero when the first object of the class is created if it is not initialized in any other way.A program that demonstrates static class members in C++ is given as follows.Example Live Demo#include using namespace std; class Example {    public :    static int a;   ... Read More

Why C/C++ variables doesn’t start with numbers

Ravi Ranjan
Updated on 26-May-2025 11:51:12

2K+ Views

In C/C++, a variable name can have alphabets, numbers, and the underscore( _ ) character. There are some keywords in the C/C++ language. Apart from them, everything is treated as an identifier. Identifiers are the names of variables, constants, functions, etc. Why Variables in C/C++ Can't Start with Numbers? In C and C++, variable names (also, known as identifiers) cannot start with a digit due to how the compiler processes code during compilation. First, we need to understand the phases of compilation or compiler. There are seven phases in a typical compiler: Lexical Analysis ... Read More

Calling a member function on a NULL object pointer in C++

Ravi Ranjan
Updated on 13-Jun-2025 18:53:46

1K+ Views

You can call a class member function using a NULL object pointer in C++. This is an undefined behavior and there is no guarantee about the execution of the program. The actual results depend on the compiler used. Class Member Function A class member function is a function that is defined either inside a class or outside a class using a scope resolution operator. Example Here is an example of class member functions in C++ where the print() function is an example of the Inside class function and the print2() function is ... Read More

Initialization of a normal array with one default value in C++

Ravi Ranjan
Updated on 26-May-2025 11:39:09

18K+ Views

An array is a fixed-size collection of elements of the same data type that stores data in contiguous memory locations. Initializing an array with one default value in C++ is an easy task and can be easily implemented using various approaches that we are going to understand with code examples. Initializing Array with Zeroes in C++ To initialize all elements of an array with 0, we will simply initialize the array with 0 or just use empty curly braces. Example Here is an example that uses the above mentioned methods to initialize the array with 0: #include using ... Read More

Which is the fastest algorithm to find prime numbers using C++?

Ravi Ranjan
Updated on 02-May-2025 13:17:12

3K+ Views

The fastest algorithm to find the prime numbers is the Sieve of Eratosthenes algorithm. It is one of the most efficient ways to find the prime numbers smaller than n when n is smaller than around 10 million. In this article, we have a given number as 'num'. Our task is to find all the prime numbers less than or equal to num using Sieve of Eratosthenes algorithm in C++. Example Here is an example to find prime numbers less than 10: Input: num = 10 Output: 2 3 5 7 The explanation of the above ... Read More

Calling class method through NULL class pointer in C++

Arjun Thakur
Updated on 26-Jun-2020 13:51:40

483 Views

A class method can be can be called using a NULL class pointer.Note − This is undefined behaviour and there is not guarantee about the execution of the program. The actual results depend on the compiler used.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; class Example {    public :    void func() {       cout func();    return 0; }OutputThe output of the above program is as follows.The function is called through Null class pointer.Now, let us understand the above program.The class Example contains a member function func(). This function displays ... Read More

Can main function call itself in C++?

Ravi Ranjan
Updated on 13-May-2025 19:34:44

2K+ Views

The main() function can call itself in C++. This is an example of recursion, i.e., a function calling itself. In recursion, a function calls itself over and over again with modified arguments until it reaches its base case or the termination condition, where the recursion stops. In this article, we will go through three C++ examples where the main function calls itself. Counting Numbers up to N To count numbers up to 'n', we will recursively call the main function until it reaches 'n'. Example The following example calls the main() function recursively to print the numbers from 1 ... Read More

Advertisements