Programming Articles - Page 3017 of 3366

delete() and free() in C++

Tapas Kumar Ghosh
Updated on 03-Jun-2025 11:19:24

1K+ Views

In C++, both delete and free() are used to deallocate the dynamically created memory. In this article, we will learn about the delete operator and free() function with the help of examples. C++ delete Operator The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator. Syntax Here is the syntax of delete operator in C++ language: delete pointer_variable; Here is the syntax to delete the block of allocated memory: delete[ ] pointer_variable; Example Following is the example of delete operator to see its usage in memory ... Read More

fgets() and gets() in C

Samual Sam
Updated on 26-Jun-2020 07:46:12

2K+ Views

fgets()The function fgets() is used to read the string till the new line character. It checks array bound and it is safe too.Here is the syntax of fgets() in C language, char *fgets(char *string, int value, FILE *stream)Here, string − This is a pointer to the array of char.value − The number of characters to be read.stream − This is a pointer to a file object.Here is an example of fgets() in C language, Example Live Demo#include #define FUNC 8 int main() {    char b[FUNC];    fgets(b, FUNC, stdin);    printf("The string is: %s", b);    return 0; }OutputThe ... Read More

size_t data type in C

Samual Sam
Updated on 26-Jun-2020 07:47:14

9K+ Views

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative. The return type of strcspn, strlen functions is size_t.Here is the syntax of size_t in C language, const size_t var_name;Here, var_name − This the name of variable.Here is an example of size_t in C language, Example Live Demo#include #include #include int main(void) {    const size_t x = 150;    int a[x];    for (size_t i = 0;i < x; ++i)    a[i] = ... Read More

The Best C++ Code Formatter/Beautifier?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

766 Views

There are so many C++ code formatter or beautifier tools which beautifies your code or format with proper indentation. The C++ code formatter/ beautifier are listed as follows − C++ Code Formatter/Beautifier Description Astyle This is a source code formatter. It can be used for C++, java and other languages. It’s latest version is 2.03 and it released in April 2013. Clang-Format It is a command line tool along with clang compiler. It is open- source tool and programmed in C++, python. The latest version is 3.3. Universal Indent GUI It ... Read More

When to use i++ or ++i in C++?

Samual Sam
Updated on 26-Jun-2020 07:36:30

1K+ Views

Increment operators are used to increase the value by one while decrement works opposite. Decrement operator decrease the value by one.Pre-increment (++i) − Before assigning the value to a variable, the value is incremented by one.Post-increment (i++) − After assigning the value to a variable, the value is incremented.Here is the syntax of i++ and ++i in C++ language,++variable_name; // Pre-increment variable_name++; // Post-incrementHere,variable_name −Name of the variable given by user.Here is an example of pre and post increment in C++ language,Example Live Demo#include using namespace std; int main() {    int i = 5;    cout

How Java objects are stored in memory?

Arjun Thakur
Updated on 26-Jun-2020 07:37:24

4K+ Views

A stack and a heap are used for memory allocation in Java. However, the stack is used for primitive data types, temporary variables, object addresses etc. The heap is used for storing objects in memory.Stacks and heaps in Java are explained in more detail as follows −Stack in JavaStacks are used to store temporary variables, primitive data types etc. A block in the stack exists for a variable only as long as the variable exists. After that, the block data is erased and it can be used for storing another variable.

Find max and min values in array of primitives using Java

Ankith Reddy
Updated on 26-Jun-2020 07:37:59

709 Views

This example shows how to search the minimum and maximum element in an array by using Collection.max() and Collection.min() methods of Collection class.Example Live Demoimport java.util.Arrays; import java.util.Collections; public class Main {    public static void main(String[] args) {       Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};       int min = (int) Collections.min(Arrays.asList(numbers));       int max = (int) Collections.max(Arrays.asList(numbers));       System.out.println("Min number: " + min);       System.out.println("Max number: " + max);    } }OutputThe above code sample will produce the following result.Min number: 1 Max number: 9Another ... Read More

Find frequency of each word in a string in Java

Chandu yadav
Updated on 26-Jun-2020 07:38:47

2K+ Views

In order to get frequency of each in a string in Java we will take help of hash map collection of Java.First convert string to a character array so that it become easy to access each character of string.Now compare for each character that whether it is present in hash map or not in case it is not present than simply add it to hash map as key and assign one as its value.And if character is present than find its value which is count of occurrence of this character in the string (initial we put as 1 when it ... Read More

Fibonacci of large number in java

George John
Updated on 26-Jun-2020 07:39:23

784 Views

Fibonacci numbers of Fibonacci series grows exponentially and can be very large for large numbers like 500 or 1000. To handle such number, long data type is not sufficient. BigInteger can handle large number easily. BigInteger is useful in scenarios where calculations results in data which is out of limit for available primitive data types. See the example below of getting Fibonacci number of 100 and 1000.Example Live Demoimport java.math.BigInteger; public class Tester {    public static void main(String args[]) {       System.out.println("Fibonacci of 100: ");       System.out.println(fibonacci(100));       System.out.println("Fibonacci of 1000: ");     ... Read More

Does JVM creates object of Main class in Java?

Ankith Reddy
Updated on 26-Jun-2020 07:40:55

539 Views

As we know that Java needs main() method to be static in the public class to make it executable. The prime reason for this requirement is to make JVM enable to call the main() method without creating an object. That simply means that JVM does not create the object of the Main class which contains the main() method. To justify the same, we can make the Main class containing the main method as abstract and program still runs.Following example showcases the same. Here we have made the main class abstract.Exampleabstract public class Tester {    public static void main(String args[]) ... Read More

Advertisements