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

Thursday, April 11, 2024

send XML data


If you want to send XML data to another service internally, you can use a variety of communication methods depending on your system architecture and the technologies you are working with. Here are some common approaches:

  1. HTTP/HTTPS Requests:

    • Description: You can send XML data to another service internally using HTTP or HTTPS requests.
    • How: Use a library in your programming language of choice to make HTTP requests. Include the XML data as the request body.
    • Example: In Python, you might use the requests library to send a POST request with an XML payload.
      python
      import requests url = "http://internal-service/api/endpoint" headers = {"Content-Type": "application/xml"} xml_data = """ <book> <title>The Great Gatsby</title> <author>F. Scott Fitzgerald</author> <year>1925</year> <genre>Fiction</genre> </book> """ response = requests.post(url, data=xml_data, headers=headers) print(response.status_code) print(response.text)
  2. Message Queues:

    • Description: You can use a message queue to send XML data as messages to another service.
    • How: Send XML data as messages to the queue, and the other service can consume the messages from the queue.
    • Example: In Python, you might use the pika library to interact with RabbitMQ and send XML data as messages.
      python
      import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.queue_declare(queue='book_queue') xml_data = """ <book> <title>The Great Gatsby</title> <author>F. Scott Fitzgerald</author> <year>1925</year> <genre>Fiction</genre> </book> """ channel.basic_publish(exchange='', routing_key='book_queue', body=xml_data) connection.close()
  3. Remote Procedure Call (RPC):

    • Description: You can use RPC mechanisms to send XML data to another service internally.
    • How: Use an RPC library or framework to make a call to the remote service, passing XML data as an argument.
    • Example: In gRPC, you might define a service method that accepts XML data as input.
  4. Inter-Process Communication (IPC):

    • Description: You can use IPC mechanisms to send XML data between different processes.
    • How: Use methods such as sockets, shared memory, or named pipes to communicate and pass XML data.
    • Example: In Python, you might use a socket connection to send XML data to another process.

 

Tuesday, March 19, 2024

Why does C++ allow unsafe code?


That is, why does C++ support operations that can be used to violate the rules of static (compile-time) type safety?
  • to access hardware directly (e.g. to treat an integer as a pointer to (address of) a device register)
  • to achieve optimal run-time and space performance (e.g. unchecked access to elements of an array and unchecked access to an object through a pointer)
  • to be compatible with C
That said, it is a good idea to avoid unsafe code like the plague whenever you don't actually need one of those three features:
  • don't use casts
  • keep arrays out of interfaces (hide them in the innards of high-performance functions and classes where they are needed and write the rest of the program using proper strings, vectors, etc.)
  • avoid void* (keep them inside low-level functions and data structures if you really need them and present type safe interfaces, usually templates, to your users)
  • avoid unions
  • if you have any doubts about the validity of a pointer, use a smart pointer instead,
  • don't use "naked" news and deletes (use containers, resource handles, etc., instead)
  • don't use ...-style variadic functions ("printf style")
  • Avoid macros excpt for include guards
Almost all C++ code can follow these simple rules. Please don't be confused by the fact that you cannot follow these rules if you write C code or C-style code in C++.

For an ambitious project to make C++ easier to use and safer without damaging its efficiency or flexibility, see the Core C++ Guidelines.


Monday, March 11, 2024

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...