Saturday, July 18, 2026

lvalue vs rvalue

 

1. What is an lvalue?

An lvalue is an object that:

  • Has an identifiable memory location.
  • Has a name (in most cases).
  • Can appear on the left-hand side of an assignment.

Example:

int x = 10;

Here:

  • x is an lvalue.

2. What is an rvalue?

An rvalue is a temporary value that:

  • Usually does not have a persistent identity.
  • Is typically not assigned to directly.
  • Is often created as the result of an expression.

Examples:

20
x + y
std::string("Hello")

These are rvalues.


3. Why doesn't this compile?

int x = 10;
int&& r = x;   // Error

Because:

  • x is an lvalue.
  • An rvalue reference (&&) can only bind to an rvalue.

If you want to bind x to an rvalue reference, you must explicitly say you're willing to treat it as an rvalue:

int&& r = std::move(x);

std::move(x) converts x into an rvalue.


4. Why are rvalue references useful?

They enable move semantics.

Instead of copying expensive resources (like dynamically allocated memory), C++ can transfer ownership of those resources, improving performance.


Interview Tip

A senior interviewer may ask:

Is std::move() actually moving the object?

The best answer is:

No. std::move() simply casts an lvalue to an rvalue reference. The actual move happens when a move constructor or move assignment operator is invoked.

Wednesday, July 9, 2025

Vi Editor

VI EDITOR

Move

move in the editor: h right j up k  down l left

move top shift +g

gg down the line of the file

:8 go to line 8

search

/string - search

n keyword to search next

capital N to search previous

? for backward

delete operation

d+w - delete word

Undo/Redo

u undo / cntrl +r redo

Copy Operation


word

yw -copy

p - paste

:10 jump to 10th line

:2 jump to 2nd line

set line number

set number 

to remove line number

set nonu

to make permanent line numbers

e ~/vimrc

vim sample.txt +8

vim sample.txt +/search text

vim +1,2d,wq sample.txt  - d for delete

delete 1 to 2 lines and save

vim +4d,wq sample.txt  - d for delete

it will delete 4th line

vim -d r1.txt r2.txt

In the "Diff mode" video, here are the key takeaways:

  • Using Diff Mode: You can compare two versions of a file using Vim's built-in diff mode with the -d switch (e.g., vim -d file1.txt file2.txt).
  • Navigating Differences: Move between the windows showing differences using Control + W + W.
  • Copying Changes: Use D + O to copy a line from one file to another and D + P to put a line from one file to another.
  • Vertical Split: You can set Vim to show differences in a vertical split by using :vert diffsplit file2.txt and making it persistent by adding set diffopt+=vertical to your .vimrc file.



Wednesday, January 22, 2025

Difference Between Reference And Pointers In C++

 This is a good video to refer to learn Difference Between Reference And Pointers In C++

Difference Between Reference And Pointers In C++


Pointer vs Reference (Interview Answer)

Pointer: A pointer is a variable that stores the memory address of another variable. It can be nullptr, can be reassigned to point to different objects, and supports pointer arithmetic.

Reference: A reference is an alias (another name) for an existing variable. It must be initialized when declared, cannot be nullptr in normal C++, and cannot be reseated to refer to another object. It behaves like the original variable.


Quick Comparison Table

PointerReference
Stores an addressAlias of an existing object
Can be nullptrCannot be nullptr (normal C++)
Can be reassignedCannot be reseated
Supports pointer arithmeticNo pointer arithmetic
Requires * to access valueUsed like a normal variable
Can point to dynamically allocated memoryTypically used for function parameters and return values

Example

int a = 10;
int b = 20;

int *p = &a;
int &r = a;

p = &b;   // OK: pointer now points to b

r = b;    // Does NOT make r refer to b
           // It copies b's value into a

Result:

a = 20
b = 20
r refers to a

Common Interview Questions & Answers

Q1: Can a pointer be nullptr?

Answer: Yes. A pointer can hold nullptr, indicating it does not point to any object.


Q2: Can a reference be nullptr?

Answer: No. A reference must always refer to a valid object in normal C++.


Q3: Can a pointer be reassigned?

Answer: Yes.

p = &b;

Q4: Can a reference be reassigned (reseated)?

Answer: No. Once initialized, a reference always refers to the same object.


Q5: When do you use a pointer?

Answer:

  • Dynamic memory (new/delete)
  • Optional objects (nullptr)
  • Polymorphism
  • Data structures (linked lists, trees)
  • Pointer arithmetic

Q6: When do you use a reference?

Answer:

  • Function parameters
  • Return values
  • Avoid copying large objects
  • Cleaner and safer syntax when an object is guaranteed to exist

30-Second Interview Answer

"A pointer is a variable that stores the address of another object. It can be null, reassigned, and supports pointer arithmetic. A reference is an alias for an existing object. It must be initialized, cannot be null in normal C++, and cannot be reseated. I use pointers when an object may be absent or for dynamic memory and polymorphism, while I use references for function parameters and return values when the object is guaranteed to exist."

Interview rating: If you can confidently give the 30-second answer and explain the r = b example correctly, you'd be answering this topic at the level expected of a senior C++ engineer.

Wednesday, December 4, 2024

Game Engines and Frameworks Using C++

 Game Engines and Frameworks Using C++

Several game engines and frameworks utilize C++:

  • Unreal Engine: One of the most popular game engines, which provides a great number of tools and options for creating games with very high quality. C++ programming for the Unreal Engine is offered at Coursera as a specialization for learners with prior programming experience.
  • Cocos2d-x: A cross-platform, C++-based, open-source framework for 2D game development for multiple platforms.
  • Irrlicht Engine: A free C++ 3D game engine described as small and fast.
  • raylib: A C library developed specifically for game programming and inherently really simple to use, thus should be easy for newbies.
Learning Resources
To further enhance C++ game development skills, consider the following resources:

  • Make a C++ Game – Full Course": A comprehensive YouTube tutorial guiding through the steps of creating a game in C++ from scratch.
  • Creating a Game Engine with C++ and OpenGL": A step-by-step guide on developing a basic game engine using C++ and OpenGL, covering environment setup, graphics rendering, and game logic.
Conclusion
C++ programming language is still widely used in game development to provide the necessary level of speed and flexibility for creating really good, effective, and complex games. As a result, C++ accompanies precious assets and solutions that help newcomers build attractive and optimized games without having to develop all the functions from scratch.

Wednesday, June 12, 2024

English proficiency tests IELTS, TOEFL: Reading these books can help you crack the exam

 Books can help in more ways than one. For example, they can take your vocabulary to the next level

Books can be our best friends, it's not for nothing that this saying is popular. Here are a few classics and what they can teach you when it comes to English language 

Lord of the Flies

BY William Golding

Often prescribed as curriculum in school, this book is a modern novel using descriptive and dramatic language that helps a reader visualise the story

Animal Farm

BY George Orwell

Simple language drives this novel. This story is short and effective, giving the reader an understanding on how to use language in a clever way

To Sir, With Love

BY ER Braithwaite

The story is about how a professor wins over the hearts of unruly students. The words and characterisation will surely teach you a thing or two too

The Curious Incident of the Dog in the Night-Time

BY Mark Haddon

A heart-warming tale of an autistic kid and a dog who set out to solve a murder mystery. The perspective of the world that the kid, Christopher, has is charming

The Old Man and the Sea

BY Ernest Hemingway

Written by journalist Ernest Hemingway, the book has language that is clear, straight forward and factually tight. The sentence structure is short, making it easy to follow

lvalue vs rvalue

  1. What is an lvalue? An lvalue is an object that: Has an identifiable memory location. Has a name (in most cases). Can appear on th...