HackIndia Logo
HackathonsNewsSkillsGrowth
HackIndia LogoContact: +91 9910125804

General

  • Companies
  • Colleges
  • Board of Advisors
  • Brand Ambassadors
  • FAQ
  • Contact
  • Report a Bug

Hackathons

  • Upcoming Hackathons
  • Judges
  • Mentors
  • Partners
  • Participating Colleges

Resources

  • About Us
  • Branding
  • Latest News
  • Growth
  • Skills
  • Ecosystem
  • Forum
  • Ideas
  • Prompt Optimizer
  • How to earn XP

© 2026 HackIndia. All rights reserved.

Privacy PolicyTerms of ServiceCode of ConductSitemap

Build 2.139 · 09 Nov 2026, 10:59 UTC

    HackIndia
    HackathonsNewsInternshipsSkillsGrowth
    C++Working towardsC++
    0%

    0 of 10 steps done · +100 XP · +50 SHARP when it is yours

    1. 1Write C++ that does not break5 min · +10 XP · +5 SHARP
    2. 2Use references and pointers safely5 min · +10 XP · +5 SHARP
    3. 3Solve faster with STL4 min · +10 XP · +5 SHARP
    4. 4Handle strings and parsing5 min · +10 XP · +5 SHARP
    5. 5Design classes with RAII5 min · +10 XP · +5 SHARP
    6. 6Understand move semantics plainly5 min · +10 XP · +5 SHARP
    7. 7Compile, debug, and test locally4 min · +10 XP · +5 SHARP
    8. 8Ship your C++ repo5 min · +10 XP · +5 SHARP
    9. The check10 questions, 7 to pass · +10 SHARP
    10. Hand in the taskA project that uses the skill
    11. Skill and certificate+100 XP · +50 SHARP
    Lesson 2 of 8 5 min

    C++

    Use references and pointers safely

    • +10 XP for this lesson
    • +100 XP when the skill is proved
    • 4 learning it right now
    In 30 seconds
    • Choose references for required objects and pointers for optional objects.
    • Avoid dangling addresses from locals, temporaries, and vector reallocation.
    Use nullptr checks and pass-by-reference to write safer project code.
    Key point

    One rule

    A reference means: this object must exist. A pointer means: this object may be missing, shared by address, or changed to point elsewhere.

    Use the stricter option first. If a function does not need null, take a reference, not a pointer.

    Why it matters

    Many C++ bugs in student projects are not syntax bugs. They are lifetime bugs. You keep an address after the real object is gone, then the program behaves strangely.

    Your repo should show that you know when data lives, who owns it, and whether a function can receive nothing.

    Your to-do

    Choose the parameter

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Example

    Safe function inputs

    This pattern fits many hackathon projects: read a list, compute something, and optionally print extra details.

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    struct Expense {
        string label;
        int amount;
    };
    
    int totalAmount(const vector<Expense>& expenses) {
        int total = 0;
        for (const Expense& e : expenses) {
            total += e.amount;
        }
        return total;
    }
    
    void addExpense(vector<Expense>& expenses, const string& label, int amount) {
        expenses.push_back({label, amount});
    }
    
    void printFirst(const vector<Expense>* expenses) {
        if (expenses == nullptr || expenses->empty()) {
            cout << "No expenses\n";
            return;
        }
        cout << expenses->at(0).label << " " << expenses->at(0).amount << "\n";
    }
    
    int main() {
        vector<Expense> trip;
        addExpense(trip, "chai", 20);
        addExpense(trip, "metro", 60);
    
        cout << totalAmount(trip) << "\n";
        printFirst(&trip);
        printFirst(nullptr);
    }
    Common mistake

    Dangling addresses

    Do not return a reference or pointer to a local variable. The local dies when the function ends.

    Also be careful with addresses of vector elements. push_back can move the vector storage, so old pointers or references to elements may become invalid.

    int& badRef() {
        int marks = 95;
        return marks; // bad: marks dies here
    }
    
    Expense* badPtr() {
        Expense e{"idli", 40};
        return &e; // bad: e dies here
    }
    Tip

    Use AI carefully

    If you ask ChatGPT or Gemini to refactor C++ code, ask it to explain every pointer and reference it uses.

    Check three things in the answer: can this be nullptr, who owns this object, and can this address outlive the object? If the answer is unclear, prefer a reference or a normal value.

    Prompt:
    Refactor this C++ function to avoid unsafe raw pointers where possible.
    For every reference or pointer you use, explain whether it can be null and who owns the object.
    Your to-do

    Do this now

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Quick check

    0 of 2 right
    1.Your function only reads a required vector of expenses. What parameter is safest and efficient?
    2.Why is returning &localVariable from a function unsafe?

    Answer the quick check to finish this lesson.

    ← Lesson 1Skip to lesson 3 →
    C++
    Working towards
    C++ +100 XP

    0 of 8 lessons done · proved by a project that uses the skill

    See the track