Programming Articles - Page 3201 of 3363

How do I use arrays in C++?

Srinivas Gorla
Updated on 11-Feb-2020 10:32:26

409 Views

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. To use an array in C++, you'll need to declare it first, for example, int arr[10];This declares an array of type int of size 10. This can store 10 integers in contiguous memory. To Refer to any of its element, you need to use the array access operator and provide it the index of the element you want to access. The indexing in C++ array start from 0. So in the ... Read More

Why is it faster to process a sorted array than an unsorted array in C++?

Anvi Jain
Updated on 23-Jun-2020 13:43:58

329 Views

In C++, it is faster to process a sorted array than an unsorted array because of branch prediction. In computer architecture, a branch prediction determines whether a conditional branch (jump) in the instruction flow of a program is likely to be taken or not.Let’s take an example −if(arr[i] > 50) {    Do some operation B } else {    Do some operation A }If we run this code for 100 elements in unsorted and sorted order below things will be happened −For sorted array −1, 2, 3, 4, 5, …… 50, 51………100 A, A, A, A, A A, B ... Read More

Difference between undefined, unspecified, and implementation-defined behavior in C and C++?

Abhinanda Shri
Updated on 30-Jul-2019 22:30:21

567 Views

Undefined behavior is simply behavior that is not defined by the C++ specification. For example, if you have multiple unary increment/decrement operations in an expression like i++ + ++i, they result in behavior that is not defined. This is simply due to the fact that some language constructs are syntactically valid but you can't predict the behavior when the code is run. Another example is the expression: u = (u++);Implementation-defined behavior is behavior unspecified by the specification and left for the implementor to decide and document how the choice is made. In this case, the choice that is made must ... Read More

What should main() return in C and C++?

Tapas Kumar Ghosh
Updated on 14-Apr-2025 18:00:03

3K+ Views

In C/C++, the main() function is used in the beginning of every program. This returns the program execution status in the operating system. Syntax Following is the syntax of the main() function using C/C++: int main(void); int main(int argc, char *argv[]); Example Below, the C/C++ example shows the usage of the main() return value. C C++ #include int main() { printf("Hello, C World!"); return 0; } Key Points of C Return 0: This is standard for successful execution Non-zero: It indicates error Implicit return: C99 allows omitting return #include int main() { std::cout

What is the copy-and-swap idiom in C++?

Ankith Reddy
Updated on 23-Jun-2020 13:51:21

335 Views

The assignment consists of 2 steps, tearing an object's old state and building a new state for it. The destructor is used for the first step and a copy constructor does the second step. Implementing both of these is straightforward. But when overloading the assignment operator, it can become quite difficult to implement. The copy and swap idiom is a solution for the same.This idiom uses the copy-constructor to build a local copy of the data. It then swaps the old data with the new data using the swap function. The temporary copy is then destructed using the destructor. We ... Read More

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

Vrundesha Joshi
Updated on 11-Feb-2020 10:28:25

1K+ Views

There are many great profiling tools for profiling C++ programs on Linux. The most widely used tool is Valgrind. It is a programming tool for memory debugging, memory leak detection, and profiling. You 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 hello.cpp hello Now use valgrind to profile it: $ valgrind --tool=callgrind ./helloThis 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

The most elegant way to iterate the words of a C/C++ string

Arjun Thakur
Updated on 11-Feb-2020 10:27:10

2K+ Views

There is no one elegant way to iterate the words of a C/C++ string. The most readable way could be termed as the most elegant for some while the most performant for others. I've listed 2 methods that you can use to achieve this. First way is using a stringstream to read words seperated by spaces. This is a little limited but does the task fairly well if you provide the proper checks. example#include #include #include using namespace std; int main() {     string str("Hello from the dark side");     string tmp;         ... Read More

What is object slicing in C++ or Java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

3K+ Views

Object slicing is used to describe the situation when you assign an object of a derived class to an instance of a base class. This causes a loss of methods and member variables for the derived class object. This is termed as information being sliced away. For example, class Foo { int a; }; class Bar : public Foo { int b; }; Since Bar extends Foo, it now has 2 member variables, a and b. So if you create a variable bar of type Bar and then create ... Read More

What are the rules about using an underscore in a C++ identifier?

Arjun Thakur
Updated on 11-Feb-2020 10:22:52

2K+ Views

From MSDN docs −Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers.So you should avoid using names like −__foo, __FOO, _FOOAnd names like the following should not be used in the global namespace −_foo, _barOther than this, there are some more prefixes like LC_, SIG_, and suffixes like _t ... Read More

Can a local variable's memory be accessed outside its scope in C/C++?

Rishi Rathor
Updated on 11-Feb-2020 10:20:27

218 Views

Let us look at an example where you MIGHT be able to access a local variable's memory outside its scope.Example#include int* foo() {    int x = 3;    return &x; } int main() {    int* address = foo();    cout

Advertisements