C++ Articles - Page 673 of 719

Why do we assume strncpy insecure in C/C++?

Ravi Ranjan
Updated on 16-May-2025 17:26:16

618 Views

The function strncpy() is used to copy the specified number of characters to the destination from the source. It is similar to the strcpy() function. In strncpy() function, we can specify the at most how many characters we want to copy from source to destination. In this article, we have a source string. Our task is to copy this string into a destination string using the strncpy() function and understand why it is insecure to use in C++. Syntax of strncpy() Function The syntax of the strncpy() function is as follows: char *strncpy(char *destination, char *source, size_t n); ... Read More

How to calculate combination and permutation in C++?

Ravi Ranjan
Updated on 02-May-2025 15:19:10

14K+ Views

Combination and permutation are a part of combinatorics. The permutation is known as the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time, or all at a time. Combination is the different ways of selecting elements if the elements are taken one at a time, some at a time, or all at a time. In this article, we have the value of 'n' and 'r' such that 'r' should be less than n. Our task is the find the permutation and combination using the value of 'n' ... Read More

How do I convert a double into a string in C++?

Ravi Ranjan
Updated on 11-Apr-2025 17:13:47

31K+ Views

A double datatype is used to represent a decimal or exponential value and string represents a sequence of characters. In this article, we are having a double value and our task is to convert the given double into string. Here is a list of approaches to convert double to string in C++ that we will be discussing in this article with stepwise explanation and complete example codes. Using to_string() Function Using ostringstream Using stringstream Using sprintf() Function Using ... Read More

C++ static member variables and their initialization

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

6K+ Views

The static member variables in C++ are defined using the static keyword. The static member variables in a class are shared by all the class objects as there is only one copy of them in memory, regardless of the number of objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. In this article, we will understand the static member variables and their characteristics, and go through various examples explaining the characteristics of static member variables. The characteristics of the static keyword are mentioned below: Characteristics ... Read More

Accessing array out of bounds in C/C++

Ravi Ranjan
Updated on 23-Jul-2025 18:57:52

2K+ Views

An array in C/C++ is a fixed-size sequential collection of elements of the same data type where all the elements are stored in the contiguous memory allocation. If an array is accessed out of bounds then an undefined behavior will occur in C/C++, unlike Java where an exception such as java.lang.ArrayIndexOutOfBoundsException will occur. Accessing Out of Bound Memory Accessing out-of-bounds memory in an array means we are trying to access the array index outside its valid range size (i.e., index = array size). It returns any garbage value in the output. Example In ... Read More

How to write long strings in Multi-lines C/C++?

Ravi Ranjan
Updated on 10-Apr-2025 10:24:43

15K+ Views

To write long strings in multi-lines, you can use 'newline character' or 'raw string literal'. It increases the readability of the code, makes the code look clean, and you can avoid scrolling. In this article, we have a long string and our task is to write the long string in multi-lines in C++. Approaches to Write Long Strings in Multi Lines Here is a list of approaches to write long strings in multiple lines which we will be discussing in this article with stepwise explanation and complete example codes. Using Newline Character ... Read More

Initializing array with variable vs a real number in C++

Arjun Thakur
Updated on 26-Jun-2020 14:12:22

336 Views

An array is a collection of same type of elements at contiguous memory location. The lowest address in the array corresponds to the first element while highest address corresponds to the last element. The array index starts with zero(0) and ends with the size of array minus one(array size - 1).An array can be initialized with variables as well as real numbers. A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int main() {    int a = 5;    int b = 3;    int arr[4];    arr[0] = a;    arr[1] = 8;    arr[2] = b;    arr[3] = 2;    cout

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

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

852 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

853 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

Advertisements