亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 1776 | 回復(fù): 9
打印 上一主題 下一主題

[C++] 使用C++的疑惑 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2004-06-27 17:31 |只看該作者 |倒序?yàn)g覽
我的C++和數(shù)據(jù)結(jié)構(gòu)是自學(xué)的,一直以來(lái)書上怎么寫我就怎么做,到了論壇里發(fā)現(xiàn)大家貼出的源代碼有一些習(xí)慣我不理解。比如說(shuō)很多人用C++時(shí)從不用new和delete,而是一定要用malloc/free。這是為什么?我覺得malloc用起來(lái)遠(yuǎn)沒有new方便。這只是一些人的習(xí)慣還是有什么原因?另外我的數(shù)據(jù)結(jié)構(gòu)學(xué)的是——殷人昆(用面向?qū)ο蠓椒枋觯┡笥呀ㄗh我學(xué)一下嚴(yán)魏敏的,我看了一下,使用C語(yǔ)言描述的。有必要重學(xué)一遍嗎?

現(xiàn)在在家,沒地問問題。想聽聽大家的意見,謝謝

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
2 [報(bào)告]
發(fā)表于 2004-06-27 19:21 |只看該作者

使用C++的疑惑

你在哪里看的“很多人用C++時(shí)從不用new和delete,而是一定要用malloc/free”?
我很想看一下。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2004-06-28 08:12 |只看該作者

使用C++的疑惑

Most of the C++ developer *will* like to use new/delete operator for memeory management, few of them *may* use malloc/free(or other third party memory management library) for some advanced system programming, but this is rarely happened.(I assume this kind of users are really C++/system expert and intend to do that in this way).  Other than that, you can/should follow the normal way(new and delete) to do your C++ programming, especially you are new to C++.

Hope this help.

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2004-06-28 08:38 |只看該作者

使用C++的疑惑

說(shuō)有很多人可能是過(guò)了一些,不過(guò)確實(shí)有一些人這樣寫?赡苁且?yàn)槲規(guī)缀踔挥胣ew/delete,所以讀到下面這段代碼會(huì)比較敏感。

  1. Status InitList_Sq(SqList &L)
  2. {
  3. L.elem=(elemtype *)malloc(LIST_INIT_SIZE*sizeof(elemtype));
  4. if(!L.elem) exit(OVERFLOW);
  5. L.length=0;
  6. L.listsize=LIST_INIT_SIZE;
  7. return OK;
  8. }
復(fù)制代碼

這是一位仁兄的代碼,他用的是C++。(只是引用一下,沒別的意思)我是個(gè)完全自學(xué)的新手,暫時(shí)身邊也找不到明白人,所以疑問也比較多。在這里只是想確認(rèn)一下我的想法
[/quote]

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2004-06-28 08:48 |只看該作者

使用C++的疑惑

我個(gè)人的理解是:那段話的含義是針對(duì)“for memeory management”,對(duì)于內(nèi)存上的操作,高手們的確是自己開辟內(nèi)存,再自己進(jìn)行初始化的;而new在C++中還有其他的用途。比如你舉的那段代碼,可以自己進(jìn)行控制“if(!L.elem) exit(OVERFLOW); ”,而如果用new的話,要用異常來(lái)進(jìn)行判斷。
  還有就是new/delete操作是通用的c++編程規(guī)則,應(yīng)該遵守,這個(gè)觀點(diǎn)英文的最后一句也提及了。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2004-06-28 09:16 |只看該作者

使用C++的疑惑

很多人使用malloc/free是因?yàn)橛X得自己處理的是scalar,這個(gè)也無(wú)所謂。

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2004-06-28 10:21 |只看該作者

使用C++的疑惑

[quote]原帖由 "alarum"][/quote 發(fā)表:


[quote]原帖由 "alarum"][/quote 發(fā)表:



OK, let's go a little bit deeper for the "new/delete" operator. As you might know, "new" operation is actually calling "malloc" to do the underlying implementation, while it also maintains a 'header' to   keep track of the memory usage, this is good for memory management, but sometimes(in a rare situation), it's a overhead, especially when you are designing a real-time/system program( I think the code you attached here is actually for such purpose, otherwise, it's not a good C++ programe ). Other than that, "new" and "malloc" are almost the same(of course, "new" will trigger a constructor of the allocated object, whilc "malloc" will not, but this is out of this topic here).

One more time, as a regular C++ programer, you are encouraged to use "new/delete" insteadof "malloc/free" unless you had to do so.

Further more, I want to clarify sth. about data structure and C++/C. I don't think they have direct relation to each other, C++ or C are just used as a language to help to descript the data structure, but they are NOT part of the data structure, you can use any other language you are familiar with to do that also. Peasonally speaking, I like to use C to learn data structure.

Hope this help.

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2004-06-28 13:25 |只看該作者

使用C++的疑惑

you are wrong, to use operator new will not add any overhead, only operator new[] will have some overhead, however, for PODs, some compilers will optimize off these overhead, and for non-PODs, you cannot avoid calling the dtor when destroying.
You may have some single step in your IDE to check what I say

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2004-06-30 08:02 |只看該作者

使用C++的疑惑

在C++程序中,malloc()/free()的作用有限,只適用于對(duì)于基本數(shù)據(jù)和簡(jiǎn)單的結(jié)構(gòu)等類型(PODs)的內(nèi)存分配和回收,一般不適用于類。因?yàn)轭愐话阌袠?gòu)造函數(shù)或析構(gòu)函數(shù),在創(chuàng)建或者刪除類對(duì)象的時(shí)候需要自動(dòng)被執(zhí)行。所以說(shuō)樓主說(shuō)的“很多人用C++時(shí)從不用new和delete,而是一定要用malloc/free”確實(shí)是言過(guò)其實(shí),除非你在C++中不使用類。比如樓主提供的代碼中如果 elemtype 是一個(gè)類而不是一個(gè)POD的話,不管出于什么理由,不用new/delete而用malloc()/free()來(lái)分配和釋放內(nèi)存都是說(shuō)不過(guò)去的。

對(duì)于PODs的內(nèi)存分配和釋放,出于與C代碼的兼容考慮,可以使用malloc()/free()。

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2004-06-30 08:10 |只看該作者

使用C++的疑惑

不過(guò)有一個(gè)原因,使得malloc/free還有市場(chǎng):內(nèi)存分配不出的時(shí)候,malloc返回NULL,而new理論上應(yīng)該是拋出異常。在某些兄弟眼里,異常可是洪水猛獸。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP