जावा पृष्ठभूमि से आ रहा है, मैं अभी भी सी ++ में स्मृति आवंटित करने के बारे में थोड़ा उलझन में हूं। मुझे पूरा यकीन है कि पहले दो कथन सही हैं:सी ++ ढेर और ढेर पर स्मृति आवंटित?
void method() {
Foo foo; // allocates foo on the stack, and the memory is freed
// when the method exits
}
void method2() {
Foo *foo = new Foo(); // allocates foo on the heap
delete foo; // frees the memory used by foo
}
लेकिन इस तरह कुछ के बारे में क्या?
void method3() {
Foo foo = *new Foo(); // allocates foo on the heap, and then copies it to the stack?
// when the method exits, the stack memory is freed, but the heap memory isn't?
}
मैं method3()
अंदर एक वैश्विक सरणी foo
जोड़ा कहो। यदि विधि समाप्त होने के बाद मैंने foo
के डेटा सदस्यों में से किसी एक तक पहुंचने का प्रयास किया, तो क्या यह काम करेगा? और method3()
स्मृति रिसाव के लिए प्रवण है?
अग्रिम धन्यवाद।
'फू फू(); वास्तव में कुछ भी आवंटित नहीं करता है। यह एक समारोह घोषित करता है। – chris