← Back to C++ Course | Chapter 7: Pointers & References | Lesson 8 of 11

C++ में References और Memory

एक reference एक alias है जो इसके refer किए variable के same memory address share करता है, इसलिए दोनों नाम identical underlying storage access करते हैं, एक copy के उलट जो अलग memory occupy करती है।
Syntax
cpp
data_type &reference_name = variable_name;

&reference_name == &variable_name;  // same address

एक Reference Same Address Share करता है

एक reference अपनी storage वाला एक अलग variable नहीं है; यह इसके refer किए variable की same memory location के लिए एक और नाम है, जिसे उनके addresses compare करके confirm किया जा सकता है।

उदाहरण: A Reference Shares the Same Address

cpp
#include <iostream>

int main() {
	int value = 10;
	int &ref = value;
	std::cout << &value << " " << &ref << std::endl; // same address
	return 0;
}

एक Reference बदलना Original बदलता है

क्योंकि एक reference अपने referent का memory address share करता है, reference के जरिए value modify करना तुरंत original variable को भी बदल देता है, क्योंकि असल में सिर्फ एक ही storage involved है।

उदाहरण: Changing a Reference Changes the Original

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 10
	int value = 10;
	int &ref = value;
	ref = 50;
	// Print to the console with cout
	std::cout << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

References बनाम Copies

एक reference fundamentally एक copy से अलग है: एक copy एक अलग memory address पर एक distinct variable है जो एक duplicated value रखता है, जबकि एक reference original का address और value पूरी तरह share करता है।

उदाहरण: References vs Copies

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 10
	int value = 10;
	// Declare copy and set it to value
	int copy = value;
	int &ref = value;
	value = 99;
	// Print to the console with cout
	std::cout << "copy=" << copy << " ref=" << ref << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function Parameters में References

एक variable को एक function में reference से pass करना function को caller का original variable सीधे modify करने देता है, क्योंकि parameter एक अलग copy receive करने की बजाय same memory address share करता है।

उदाहरण: References in Function Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

void addTen(int &x) {
	x += 10;
}

// Program execution starts in main()
int main() {
	// Declare num and set it to 5
	int num = 5;
	addTen(num);
	// Print to the console with cout
	std::cout << num << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

References को Reseat नहीं किया जा सकता

एक बार एक reference initialization पर एक variable से bind हो जाए, यह बाद में कभी किसी अलग variable को refer करने के लिए नहीं बनाया जा सकता -- बाद का कोई भी assignment referred-to value बदलता है, reference किसकी ओर point करता है वह नहीं।

उदाहरण: References Cannot Be Reseated

cpp
#include <iostream>

int main() {
	int a = 1, b = 2;
	int &ref = a;
	ref = b; // assigns b's VALUE to a, does not rebind ref to b
	std::cout << a << " " << b << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह सोचना कि एक reference एक अलग copy है, जब यह same variable का एक और नाम है और इसका address share करता है।
  2. एक parameter में & छोड़ देना और फिर एक copy modify करना जबकि caller का variable unchanged रहता है।
  3. एक ऐसे object का reference रखना जो destroy हो चुका है, जैसे एक vector का element जो reallocate हो गया।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.