C++ में References और Memory
In this page:
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
#include <iostream>
int main() {
int value = 10;
int &ref = value;
std::cout << &value << " " << &ref << std::endl; // same address
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Reference बदलना Original बदलता है
क्योंकि एक reference अपने referent का memory address share करता है, reference के जरिए value modify करना तुरंत original variable को भी बदल देता है, क्योंकि असल में सिर्फ एक ही storage involved है।
उदाहरण: Changing a Reference Changes the Original
// 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;
}
Login to try C/C++/Java/PHP code in the editor
References बनाम Copies
एक reference fundamentally एक copy से अलग है: एक copy एक अलग memory address पर एक distinct variable है जो एक duplicated value रखता है, जबकि एक reference original का address और value पूरी तरह share करता है।
उदाहरण: References vs Copies
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Function Parameters में References
एक variable को एक function में reference से pass करना function को caller का original variable सीधे modify करने देता है, क्योंकि parameter एक अलग copy receive करने की बजाय same memory address share करता है।
उदाहरण: References in Function Parameters
// 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;
}
Login to try C/C++/Java/PHP code in the editor
References को Reseat नहीं किया जा सकता
एक बार एक reference initialization पर एक variable से bind हो जाए, यह बाद में कभी किसी अलग variable को refer करने के लिए नहीं बनाया जा सकता -- बाद का कोई भी assignment referred-to value बदलता है, reference किसकी ओर point करता है वह नहीं।
उदाहरण: References Cannot Be Reseated
#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;
}
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि एक reference एक अलग copy है, जब यह same variable का एक और नाम है और इसका address share करता है।
- एक parameter में
&छोड़ देना और फिर एक copy modify करना जबकि caller का variable unchanged रहता है। - एक ऐसे object का reference रखना जो destroy हो चुका है, जैसे एक vector का element जो reallocate हो गया।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: