{"id":333,"date":"2023-09-16T10:32:10","date_gmt":"2023-09-16T02:32:10","guid":{"rendered":""},"modified":"2023-09-18T11:50:03","modified_gmt":"2023-09-18T03:50:03","slug":"c%e4%b8%ad%e6%95%b0%e7%bb%84%e5%88%9d%e5%a7%8b%e5%8c%96%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/mushiming.com\/333.html","title":{"rendered":"C++\u4e2d\u6570\u7ec4\u521d\u59cb\u5316\u65b9\u6cd5"},"content":{"rendered":"
\u5b9a\u4e49\uff1a<\/p>\n
int *pia = new int[10]; \/\/ array of 10 uninitialized ints<\/code><\/pre>\n\u6b64 new \u8868\u8fbe\u5f0f\u5206\u914d\u4e86\u4e00\u4e2a\u542b\u6709 10 \u4e2a int \u578b\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u5e76\u8fd4\u56de\u6307\u5411\u8be5\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u6307\u9488\uff0c\u6b64\u8fd4\u56de\u503c\u521d\u59cb\u5316\u4e86\u6307\u9488 pia\u3002<\/p>\n
\u5728\u81ea\u7531\u5b58\u50a8\u533a\u4e2d\u521b\u5efa\u7684\u6570\u7ec4\u5bf9\u8c61\u662f\u6ca1\u6709\u540d\u5b57\u7684\uff0c\u53ea\u80fd\u901a\u8fc7\u5176\u5730\u5740\u95f4\u63a5\u5730\u8bbf\u95ee\u5806\u4e2d\u7684\u5bf9\u8c61\u3002<\/p>\n
\u6ce8\u610f\uff1aC++\u4f7f\u7528new\u548cdelete\u5728\u5806\uff08\u81ea\u7531\u5b58\u50a8\u533a\uff09\u4e0a\u5206\u914d\u548c\u91ca\u653e\u52a8\u6001\u6570\u7ec4\u3002<\/strong><\/p>\n<\/p>\n\u52a8\u6001\u6570\u7ec4\u521d\u59cb\u5316\uff1a<\/strong><\/p>\n1. \u5143\u7d20\u53ea\u80fd\u521d\u59cb\u5316\u4e3a\u5143\u7d20\u7c7b\u578b\u7684\u9ed8\u8ba4\u503c\uff0c\u800c\u4e0d\u80fd\u50cf\u6570\u7ec4\u53d8\u91cf\u4e00\u6837\uff0c\u7528\u521d\u59cb\u5316\u5217\u8868\u4e3a\u6570\u7ec4\u5143\u7d20\u63d0\u4f9b\u5404\u4e0d\u76f8\u540c\u7684\u521d\u503c\u3002<\/p>\n
2. \u5bf9\u4e8e\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u5fc5\u987b\u4f7f\u7528()\u6765\u663e\u793a\u6307\u5b9a\u7a0b\u5e8f\u6267\u884c\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u5426\u5219\u7a0b\u5e8f\u4e0d\u6267\u884c\u521d\u59cb\u5316\u64cd\u4f5c\uff1a<\/p>\n
<\/pre>\n\n- \n
int *pia = new int[10]; \/\/ \u6bcf\u4e2a\u5143\u7d20\u90fd\u6ca1\u6709\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
int *pia2 = new int[10] (); \/\/ \u6bcf\u4e2a\u5143\u7d20\u521d\u59cb\u5316\u4e3a0<\/code><\/p>\n<\/li>\n<\/ol>\n3.\u7c7b\u7c7b\u578b\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u5219\u65e0\u8bba\u662f\u5426\u4f7f\u7528\uff08\uff09<\/strong>\uff0c\u90fd\u4f1a\u81ea\u52a8\u8c03\u7528\u5176\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\uff1a<\/p>\n<\/pre>\n\n- \n
string *psa = new string[10]; \/\/ \u6bcf\u4e2a\u5143\u7d20\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
string *psa = new string[10](); \/\/ \u6bcf\u4e2a\u5143\u7d20\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n<\/ol>\n\u52a8\u6001\u5206\u914d\u7a7a\u6570\u7ec4\uff1a<\/strong><\/p>\nchar *cp = new char[0];<\/code><\/pre>\n\u4e4b\u540e\uff0c\u53ef\u4ee5\u52a8\u6001\u6539\u53d8cp\u7684\u7ef4\u6570\u3002<\/p>\n<\/p>\n
\u52a8\u6001\u91ca\u653e\uff1a<\/strong><\/p>\ndelete [] pia;<\/code><\/pre>\n\u5178\u578b\u4f7f\u7528\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n
<\/pre>\n\n- \n
const char *pc = \"a very long literal string\"; \/\/ \u5904\u7406C\u98ce\u683c\u5b57\u7b26\u4e32\u65f6\u4f7f\u7528const\u6307\u9488<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
const size_t len = strlen(pc) +1; \/\/ size_t\u7528\u4e8e\u6570\u7ec4\u7684\u5927\u5c0f\u548c\u4e0b\u6807<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
for (size_t ix = 0; ix != 1000000; ++ix) {
\n <\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
char *pc2 = new char[len]; \/\/ pc2\u6307\u5411\u7684\u5b58\u50a8\u7a7a\u95f4\u7684\u5185\u5bb9\u4f1a\u52a8\u6001\u6539\u53d8\uff0c\u56e0\u6b64\u4e0d\u4f7f\u7528const<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
strncpy (pc2, pc, len); \/\/ \u4f7f\u7528strncpy\u6bd4\u4f7f\u7528strcpy\u5b89\u5168<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
\/\/ do something;<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
delete [] pc2;<\/code><\/p>\n<\/li>\n- <\/li>\n
- \n
}<\/code><\/p>\n<\/li>\n<\/ol>\n<\/p>\n
\u53c2\u8003\uff1aC++ Primer \u5b66\u4e60\u7b14\u8bb0\uff1a\u52a8\u6001\u6570\u7ec4<\/p>\n<\/p>\n
\u4e00\u3001\u6570\u7ec4\u5b9a\u4e49\u548c\u521d\u59cb\u5316<\/h3>\n
<\/p>\n
<\/pre>\n\n- \n
1: \u4e00\u7ef4\u6570\u7ec4\u521d\u59cb\u5316\uff1a<\/code><\/p>\n<\/li>\n- \n
2: \u6807\u51c6\u65b9\u5f0f\u4e00\uff1a int value[100]; \/\/ value[i]\u7684\u503c\u4e0d\u5b9a\uff0c\u6ca1\u6709\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n- \n
3: \u6807\u51c6\u65b9\u5f0f\u4e8c\uff1a int value[100] = {1,2}; \/\/ value[0]\u548cvalue[1]\u7684\u503c\u5206\u522b\u4e3a1\u548c2\uff0c\u800c\u6ca1\u6709\u5b9a\u4e49\u7684value[i>1]<\/code><\/p>\n<\/li>\n- \n
4: \/\/ \u5219\u521d\u59cb\u5316\u4e3a0<\/code><\/p>\n<\/li>\n- \n
5: \u6307\u9488\u65b9\u5f0f\uff1a int* value = new int[n]; \/\/ \u672a\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n- \n
6: delete []value; \/\/ \u4e00\u5b9a\u4e0d\u80fd\u5fd8\u4e86\u5220\u9664\u6570\u7ec4\u7a7a\u95f4<\/code><\/p>\n<\/li>\n- \n
7: <\/code><\/p>\n<\/li>\n- \n
8: \u4e8c\u7ef4\u6570\u7ec4\u521d\u59cb\u5316\uff1a<\/code><\/p>\n<\/li>\n- \n
9: \u6807\u51c6\u65b9\u5f0f\u4e00\uff1a int value[9][9]; \/\/ value[i][j]\u7684\u503c\u4e0d\u5b9a\uff0c\u6ca1\u6709\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n- \n
10: \u6807\u51c6\u65b9\u5f0f\u4e8c\uff1a int value[9][9] = {
\n {1,1},{2}}\uff1b \/\/value[0][0,1]\u548cvalue[1][0]\u7684\u503c\u521d\u59cb\u5316\uff0c\u5176\u4ed6\u521d\u59cb\u5316\u4e3a0<\/code><\/p>\n<\/li>\n- \n
11: \u6307\u9488\u65b9\u5f0f\u4e00\uff1a int (*value)[n] = new int[m][n];<\/code><\/p>\n<\/li>\n- \n
12: delete []value; \/\/ n\u5fc5\u987b\u4e3a\u5e38\u91cf\uff0c\u8c03\u7528\u76f4\u89c2\u3002\u672a\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n- \n
13: \u6307\u9488\u65b9\u5f0f\u4e8c\uff1a int** value = new int* [m];<\/code><\/p>\n<\/li>\n- \n
14: for(i) value[i] = new int[n];<\/code><\/p>\n<\/li>\n- \n
15: for(i) delete []value[i];<\/code><\/p>\n<\/li>\n- \n
16: delete []value; \/\/ \u591a\u6b21\u6790\u6784\uff0c\u5b58\u50a8\u9ebb\u70e6\uff0c\u672a\u521d\u59cb\u5316<\/code><\/p>\n<\/li>\n- \n
17: \u6307\u9488\u65b9\u5f0f\u4e09\uff1a int * value = new int[3][4]; \/\/ \u6570\u7ec4\u7684\u5b58\u50a8\u662f\u6309\u884c\u5b58\u50a8\u7684<\/code><\/p>\n<\/li>\n- \n
18: delete []value; \/\/ \u4e00\u5b9a\u8981\u8fdb\u884c\u5185\u5b58\u91ca\u653e\uff0c\u5426\u5219\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732<\/code><\/p>\n<\/li>\n- \n
19: <\/code><\/p>\n<\/li>\n- \n
20: \u591a\u7ef4\u6570\u7ec4\u521d\u59cb\u5316\uff1a<\/code><\/p>\n<\/li>\n- \n
21: \u6307\u9488\u65b9\u5f0f\uff1a int * value = new int[m][3][4]; \/\/ \u53ea\u6709\u7b2c\u4e00\u7ef4\u53ef\u4ee5\u662f\u53d8\u91cf\uff0c\u5176\u4ed6\u51e0\u7ef4\u5fc5\u987b\u90fd\u662f\u5e38\u91cf\uff0c\u5426\u5219\u4f1a\u62a5\u9519<\/code><\/p>\n<\/li>\n- \n
22: delete []value; \/\/ \u4e00\u5b9a\u8981\u8fdb\u884c\u5185\u5b58\u91ca\u653e\uff0c\u5426\u5219\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732<\/code><\/p>\n<\/li>\n<\/ol>\n<\/p>\n
\u6570\u7ec4\u521d\u59cb\u5316\u7684\u5927\u62ec\u53f7\u540e\u9762\u8981\u52a0\u201c;\u201d\u6765\u8868\u793a\u7ed3\u675f\u3002<\/p>\n
\u6570\u7ec4\u8bbf\u95ee\uff1a<\/p>\n
\u6307\u9488\u5f62\u5f0f\uff1a\u5982\u4e8c\u7ef4\u6570\u7ec4value[i][j]\u7684\u8bbf\u95ee\uff1a<\/p>\n
*(value[i] + j) \u6216<\/p>\n
(*(value + i))[j]<\/p>\n
\u4e8c\u3001\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/h3>\n
<\/p>\n
<\/pre>\n\n- \n
1: \u4e00\u7ef4\u6570\u7ec4\u53c2\u6570\u4f20\u9012\uff1a<\/code><\/p>\n<\/li>\n- \n
2: void Func(int *value);<\/code><\/p>\n<\/li>\n- \n
3: \u6216\u8005\u662f<\/code><\/p>\n<\/li>\n- \n
4: void Func(int value[]);<\/code><\/p>\n<\/li>\n- \n
5: <\/code><\/p>\n<\/li>\n- \n
6: \u4e8c\u7ef4\u6570\u7ec4\u4f20\u9012\uff1a<\/code><\/p>\n<\/li>\n- \n
7: \u5b9a\u4e49\u662f int **value;\u7684\u4f20\u9012<\/code><\/p>\n<\/li>\n- \n
8: void Func(int **value);<\/code><\/p>\n<\/li>\n- \n
9: \u5b9a\u4e49\u662f int (*value)[n] = new int[m][n];\u7684\u4f20\u9012<\/code><\/p>\n<\/li>\n- \n
10: void func(int (*value)[n]); \/\/ sizeof(p)=4,sizeof(*value)=sizeof(int)*n;<\/code><\/p>\n<\/li>\n<\/ol>\n<\/p>\n
\u4e09\u3001\u6570\u7ec4\u4e0e\u6307\u9488\u5173\u7cfb<\/h3>\n
1\u3001\u6570\u7ec4\u540d\u7684\u5185\u6db5\u5728\u4e8e\u5176\u6307\u4ee3\u5b9e\u4f53\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u5c31\u662f\u6570\u7ec4\uff1b<\/p>\n
2\u3001\u6570\u7ec4\u540d\u7684\u5916\u5ef6\u5728\u4e8e\u5176\u53ef\u4ee5\u8f6c\u6362\u4e3a\u6307\u5411\u5176\u6307\u4ee3\u5b9e\u4f53\u7684\u6307\u9488\uff0c\u800c\u4e14\u662f\u4e00\u4e2a\u6307\u9488\u5e38\u91cf\uff1b<\/p>\n
3\u3001\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\u5219\u662f\u53e6\u5916\u4e00\u79cd\u53d8\u91cf\u7c7b\u578b\uff0c\uff08\u5728win32\u5e73\u53f0\u4e0b\uff0c\u957f\u5ea6\u4e3a4\uff09\uff0c\u4ec5\u4ec5\u610f\u5473\u7740\u6570\u7ec4\u5b58\u653e\u5730\u5740\u3002<\/p>\n
4\u3001\u6570\u7ec4\u540d\u4f5c\u4e3a\u51fd\u6570\u5f62\u53c2\u65f6\uff0c\u5728\u51fd\u6570\u4f53\u5185\uff0c\u5176\u5931\u53bb\u4e86\u672c\u8eab\u7684\u5185\u6db5\uff0c\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u800c\u4e14\u5728\u5176\u5931\u53bb\u5176\u5185\u6db5\u7684\u540c\u65f6\uff0c\u5b83\u8fd8\u5931\u53bb\u4e86\u5176\u5e38\u91cf\u7279\u6027\uff0c\u53ef\u4ee5\u4f5c\u81ea\u589e\u3001\u81ea\u51cf\u7b49\u64cd\u4f5c\uff0c\u53ef\u4ee5\u88ab\u4fee\u6539\u3002<\/p>\n
\u56db\u3001\u6570\u7ec4\u7684\u5b58\u50a8\u683c\u5f0f<\/h3>\n
\u591a\u7ef4\u6570\u7ec4\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u65f6\u662f\u6309\u7167\u6700\u4f4e\u7ef4\u8fde\u7eed\u7684\u683c\u5f0f\u5b58\u50a8\u7684\uff0c\u5982\u4e8c\u7ef4\u6570\u7ec4{undefined{1,2}\uff0c{3,4}}\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u8fd9\u6837\u987a\u5e8f\u7684\u201c1,3,2,4\u201d\uff0c\u8fd9\u8ddfmatlab\u662f\u6709\u533a\u522b\u7684\uff0cmatlab\u662f\u6309\u5217\u8fdb\u884c\u5b58\u50a8\u7684\u3002\u5728\u4f7f\u7528\u6307\u9488\u8fdb\u884c\u7d22\u5f15\u65f6\u5f88\u6709\u7528\u3002<\/p>\n
\u4e94\u3001\u5b57\u7b26\u6570\u7ec4<\/h3>\n
char\u7c7b\u578b\u7684\u6570\u7ec4\u88ab\u79f0\u4f5c\u5b57\u7b26\u6570\u7ec4\uff0c\u901a\u5e38\u7528\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u3002\u5b57\u7b26\u4e32\u662f\u9644\u52a0\u6709\u7279\u6b8a\u5b57\u7b26\uff08\u4e32\u5c3e\u6807\u5fd7\uff09\u7684\u5b57\u7b26\u5e8f\u5217\u3002\u4e32\u7ec8\u6b62\u5b57\u7b26\u8868\u660e\u5b57\u7b26\u4e32\u5df2\u7ecf\u7ed3\u675f\uff0c\u8be5\u5b57\u7b26\u7531\u8f6c\u4e49\u5e8f\u5217\u2018\\0\u2019\u5b9a\u4e49\uff0c\u6709\u65f6\u88ab\u79f0\u4e3a\u7a7a\u5b57\u7b26\uff0c\u5360\u7528\u4e00\u4e2a\u5b57\u8282\uff0c\u5176\u4e2d8\u4f4d\u5168\u4e3a0\u3002\u8fd9\u79cd\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\u901a\u5e38\u88ab\u79f0\u4e3aC\u578b\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u4ee5\u8fd9\u6837\u7684\u65b9\u5f0f\u5b9a\u4e49\u5b57\u7b26\u4e32\u662f\u5728C\u8bed\u8a00\u4e2d\u63a8\u51fa\u7684\uff0c\u5728C++\u4e00\u822c\u4f7f\u7528string\uff0c\u800cMFC\u4e2d\u5219\u5b9a\u4e49\u4e86CString\u7c7b\u3002<\/p>\n
\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528\u4e00\u4e2a\u5b57\u8282\uff0c\u7b97\u4e0a\u6700\u540e\u7684\u7a7a\u5b57\u7b26\uff0c\u5b57\u7b26\u4e32\u9700\u8981\u7684\u5b57\u8282\u6570\u8981\u6bd4\u5305\u542b\u7684\u5b57\u8282\u6570\u591a\u4e00\u4e2a\u3002\u5982\uff1a<\/p>\n
char movie_star[15] = \u201cMarilyn Monroe\u201d;<\/p>\n
\u8fd9\u91cc\u5b57\u7b26\u4e32\u662f14\u4e2a\u5b57\u7b26\uff0c\u4f46\u662f\u8981\u5b9a\u4e4915\u4e2a\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002\u4e5f\u53ef\u4ee5\u4e0d\u6307\u5b9a\u5b57\u7b26\u6570\u7ec4\u7684\u4e2a\u6570\u3002\u5982\uff1a<\/p>\n
char movie_star[] = \u201cMarilyn Monroe\u201d;<\/p>\n
\u516d\u3001\u5185\u5b58\u6cc4\u9732<\/h3>\n
\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6307\u9488\uff0c\u7136\u540e\u7ed9\u5b83\u8d4b\u4e88\u4e86\u4e00\u4e2a\u5730\u5740\u503c\uff0c\u7136\u540e\u53c8\u4e0d\u518d\u4f7f\u7528\uff0c\u4f46\u662f\u6ca1\u6709delete\uff0c\u90a3\u4e48\u5f53\u7ed9\u6307\u9488\u8d4b\u4e88\u5176\u4ed6\u7684\u5730\u5740\u503c\u65f6\uff0c\u539f\u6765\u7684\u5185\u5b58\u5c06\u65e0\u6cd5\u91ca\u653e\uff0c\u8fd9\u5c31\u53eb\u505a\u5185\u5b58\u6cc4\u9732\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"C++\u4e2d\u6570\u7ec4\u521d\u59cb\u5316\u65b9\u6cd5\u5b9a\u4e49\uff1aint*pia=newint[10];\/\/arrayof10uninitializedints\u6b64new\u8868\u8fbe\u5f0f\u5206\u914d\u4e86\u4e00\u4e2a\u542b\u670910\u4e2aint\u578b\u5143\u7d20\u7684\u6570\u7ec4...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/333"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}