मैं यह जानना चाहता हूं कि मेरी फेंक ऑब्जेक्ट स्मृति में संग्रहीत है। तो मैं इसके लिए एक छोटे से कार्यक्रम ने लिखा है:सी ++ में फेंक दिया गया ऑब्जेक्ट कहां है?
#include <iostream>
#define print_dist() int a;\
do { std::cout << __FUNCTION__ << "() a[" << (long)&a - (long)ptrMainStackBase << "]" << std::endl; } while (0)
#define print_distx(x) \
do { std::cout << __FUNCTION__ << "() " #x "[" << (long)&x - (long)ptrMainStackBase << "]" << std::endl; } while (0)
#define print_distxy(x, y) \
do { std::cout << __FUNCTION__ << "() " #x "(ex)[" << (long)&x - (long)y << "]" << std::endl; } while (0)
class CTest
{
public:
CTest()
{ std::cout << "CTest::CTest" << std::endl; }
// private:
CTest(const CTest&)
{ std::cout << "copy" << std::endl; }
};
const CTest *ptrException;
int *ptrMainStackBase;
void Test2()
{
print_dist();
CTest test;
print_distx(test);
std::cout << "&test=" << &test << std::endl;
throw test;
}
void Test1()
{
print_dist();
try
{
Test2();
}
catch (const CTest& test)
{
ptrException = &test;
print_dist();
print_distx(test);
print_distxy(test, ptrException);
std::cout << "&test=" << &test << std::endl;
throw test;
}
}
int main()
{
int b;
ptrMainStackBase = &b;
print_dist();
try
{
print_dist();
Test1();
}
catch (const CTest& test)
{
print_dist();
print_distx(test);
print_distxy(test, ptrException);
std::cout << "&test=" << &test << std::endl;
}
return 0;
}
और यह प्रिंट:
main() a[-4]
main() a[-8]
Test1() a[-64]
Test2() a[-104]
CTest::CTest
Test2() test[-108]
&test=0x7fffd3b21628 <- test created here on stack
copy
Test1() a[-68]
Test1() test[-140736732956164]
Test1() test(ex)[0]
&test=0xb89090 <- and copied here
copy
main() a[-12]
main() test[-140736732956020]
main() test(ex)[144]
&test=0xb89120 <- and here
ऐसा लगता है, जैसे जब मैं एक वस्तु फेंक, यह पहली एक और ढेर से दूर बहुत दूर है पर कॉपी किया है सामान्य एक क्या ये सच है? और दो "अपवाद स्टैक फ्रेम" के बीच 144 बाइट दूरी क्यों है?
मुझे विश्वास नहीं है! मुझे लगता है कि संदर्भ द्वारा एक फेंक दिया ऑब्जेक्ट पकड़ना सामान्य है। –
@ औद्योगिक-एंटीड्रिप्रेसेंट, यह है, और यह होना चाहिए - यह तथ्य इस तथ्य के कारण पूरी तरह से सुरक्षित है कि फेंक दिया गया वस्तु कॉपी की गई है। अगर इसकी प्रतिलिपि नहीं बनाई गई तो आपको कोई समस्या होगी। – eran