結構體中使用string

2021-06-23 06:03:13 字數 495 閱讀 9637

1. malloc只是負責申請一塊記憶體,沒有任何其他動作。

2. 直接宣告結構體或者new 乙個結構體指標,會呼叫預設建構函式。如果結構中包含類,同樣會呼叫成員類的預設建構函式。

3. 程式中的記憶體錯誤是因為使用malloc分配乙個結構體內存,但是string是乙個類。並沒有呼叫string的建構函式,所以string在malloc之後沒有正確構造,導致使用時段錯誤。

---------------------------------------

下面測試程式前兩個直接宣告結構體和new出來的,都可以正確賦值,但是malloc的那塊,賦值就段錯誤。

#include #include #include #include #include using namespace std;

class a

};typedef struct tagtest

test;

int main()

C語言中使用結構體

1 先定義結構體 struct stu char name 20 long number float score 4 再定義指向結構體型別變數的指標變數 struct stu p1,p2 或者 struct stu p1,p2 定義指標變數p 1 p 2,分別指向結構體型別變數。2 在定義型別的同時...

問題解決 在結構體中使用set儲存結構體資料

宣告 至於為什麼會使用這麼奇葩的東西 為了部落的榮耀 在結構體中使用stl的set,比使用vector等要複雜一點,感覺是因為set的儲存用到了樹,所以要寫 上示例 include include using namespace std struct aa int main int argc,cha...

Java中使用結構體時遇到的問題

在程式設計中,需要使用到乙個點的結構體,這個結構體比較簡單,如下所示 public static class point這時需要乙個型別為point的陣列,當宣告了乙個長度為4的陣列,並初始化後,效果是這樣的 public class test struct public static void m...