2015-06-04 79 views
1

我提前道歉,因爲我沒有特定的答案。只有一些見解,將不勝感激。下面的評論(以及代碼)概述了一些觀察,澄清和不確定性。這裏的問題真的是會員的一生。堆棧成員VS C++對象中的堆成員

class SomeClass { 
int m_int; // primitive type. hardly ever see a pointer 

// For class instances, you always* seem to see this 
SomeOtherClass* m_some_other_class_instance_1; 
// and not this 
SomeOtherClass m_some_other_class_instance_2; 

// But lately, I've noticed that for std:: templates, it doesn't seem to be this 
vector<double>* m_vector_instance_1; 
// but rather this 
vector<double> m_vector_instance_2; 
}; 

// So it got me thinking ... 


void mainThread() { 
    SomeClass* some_class_instance_1 = new SomeClass(); 
    // SomeClass instance on heap 
    // So all its members (both <xx>_1 and <xx>_2) are on heap as well 
    // Hence all its members will stay alive beyond the scope of this function (or do they?) 

    SomeClass some_class_instance_2; 
    // SomeClass instance on stack 
    // So the only piece of data relating to SomeClass that's on the heap is what's pointed to by <xx>_1 members 
    // But everything else will still stay alive within the scope of this function 

    // In conclusion, using either case above, members of a SomeClass instance stay alive for their intended period 
    // So are <xx>_1 members overkill? 

    // Ah, ha, ha, ha, stayin' alive, stayin' alive ... 
} 

在背景方面,我們假設SomeClass不知道它周圍的其他類的,不期望能夠通過任何在/出去......這樣的構造可能只是初始化其成員無論如何,編寫它的人都不知道如何使用這個類。唯一擔心的是會員活着。

我已經通過這些線程讀取,但他們都不太相關:

Why should I use a pointer rather than the object itself?

Class members and explicit stack/heap allocation

Class members that are objects - Pointers or not? C++

+0

您有問題要問? –

+0

閱讀代碼中的評論。其中一些是問題。 – Ash

+0

如何更清晰和具體的比*「那麼 _1成員過度殺傷?」* –

回答

1
int m_int; // primitive type. hardly ever see a pointer 

如果我看到int *m我的第一反應是,這是int數組。

// For class instances, you always* seem to see this 
SomeOtherClass* m_some_other_class_instance_1; 
// and not this 
SomeOtherClass m_some_other_class_instance_2; 

您可能要堆是否需要推遲裝載分配你的對象,而不必在外部類是構建。你可能做的另一個原因是多態的原因。如果SomeOtherClass在構造函數中是基類,則可以初始化一個不同的子類。 if(some_condition) m_ptr = new Child1(); else m_ptr = new Child2(); 再次,您可能想要將其封裝在unique_ptr中,以便銷燬是自動的,並且不會泄漏。

// But lately, I've noticed that for std:: templates, it doesn't seem to be this 
vector<double>* m_vector_instance_1; 
// but rather this 
vector<double> m_vector_instance_2; 

如果你堅持一個不屬於這個類的向量的指針,那麼ptr並不意外。

分配向量(或其他stl容器)的堆沒有意義,部分原因是因爲您正在使用它們來消除處理c樣式數組和處理內存的痛苦。在矢量下面,它將被分配堆。

SomeClass* some_class_instance_1 = new SomeClass(); 
    // SomeClass instance on heap 
    // So all its members (both <xx>_1 and <xx>_2) are on heap as well 
    // Hence all its members will stay alive beyond the scope of this function (or do they?) 

是的,它的所有堆棧成員都會一直存在直到它被刪除。任何堆中分配的人必須被銷燬,以及,如果你正在做手工分配的,一定要調用刪除,但最好周圍的東西包裹像unique_ptrshared_ptr1

SomeClass some_class_instance_2; 
    // SomeClass instance on stack 
    // So the only piece of data relating to SomeClass that's on the heap is what's pointed to by <xx>_1 members 
    // But everything else will still stay alive within the scope of this function 

此對象和它的成員將被摧毀當它超出範圍。雖然它的一些成員不是指針。如果他們指向一個堆分配的成員,沒有其他指針,你有內存泄漏。

// In conclusion, using either case above, members of a SomeClass instance stay alive for their intended period 
    // So are <xx>_1 members overkill? 

我想不出在上面的上下文中分配一個stl容器的有效理由。可能需要分配其他成員的堆,但不要在可以時儘可能地分配其他成員,甚至在可以時更願意使用smart_ptrs

// Ah, ha, ha, ha, stayin' alive, stayin' alive ... 

這是C++,你會死於一次緩慢而痛苦的死亡。

+0

最後,一個真正*閱讀*我的文章背後的意圖的人。謝謝。首選答案...直到有更好的答案出現;) – Ash

+1

我編輯了我的問題以提供一些上下文,因此請相應地編輯您的問題。 +1爲慢速和痛苦的死亡評論。 – Ash

+0

@Ash更新了答案。 –

0

低於你的假設其實是錯誤的

class SomeClass { 
int m_int; // primitive type. hardly ever see a pointer 

否它取決於上下文或情況你沒有看到並不意味着人們沒有使用它。

// For class instances, you always* seem to see this 
SomeOtherClass* m_some_other_class_instance_1; 
// and not this 
SomeOtherClass m_some_other_class_instance_2; 

同樣在這裏再次取決於您的需求

// But lately, I've noticed that for std:: templates, it doesn't seem to be this 
vector<double>* m_vector_instance_1; 
// but rather this 
vector<double> m_vector_instance_2; 
}; 

範圍&壽命是不同的項,它不是取決於你是否使用指針或沒有。

看到這些問題的更多細節

Benefits of pointers?

https://softwareengineering.stackexchange.com/questions/16211/what-are-use-cases-and-advantages-of-pointers

+0

準確地說,我在評論中使用了「難」和「似乎」這兩個詞。我知道我沒有看到任何代碼,只是注意到'似乎'常見。因此,在函數間傳遞成員的時間效率....是你需要成員指向堆上的數據的唯一原因? – Ash

+0

PS:在某些情況下,生命期和範圍可能非常相關。想一想'本地非靜態'變量....如我的示例所示。 – Ash