编程小贴士

给你的编程提供小点子


delete 和 delete [] 的区别

  当调用delete的时候,系统会自动调用已分配的对象的析构函数。当我们用new [] 分配的对象是基本数据类型时,用delete和delete [] 没有区别。但是,当分配的对象是自定义对象时,二者不能通用。一般来说使用new分配的对象,用delete来释放。用new[] 分配的内存用delete [] 来逐个释放。


delete与delete[]需要注意的地方


1:首先来说一个大家容易忽略的问题: 定义:int *p=new int;


      这个大家一看就知道,在内存中分配了一个int类型的空间,没错。但是我想说的是,操作系统在堆在分配了一个int类型空间给p指向的空间,但是p本身 的值是在栈上,我觉的明白这个很重要。比如,你需要处理一个海量数据,这个数据需要用二维数组来表示,你如果这样定义int *ptr[MaxNum],然后再循环为每个ptr[0~MaxNum-1]分配空间,这个时候容易隐含一个错误,我们知道内存中栈的大小大约就2M左 右,而堆很大,几乎没有限制,当你的MaxNum很大的时候,就会导致内存溢出,因为ptr这个值的本身是在栈上的,而栈的大小就2M左右,而你又有这么 多个地址要存放,所以会出错。解决办法有二个:一:用一维数据代替二维数组;二:定义一个二维指针; 然后再动态分配。


2:delete与delete[]执行遇到的问题:


①   int  *p=new int[100];       int  num[100];       p=num;       delete []p;


     大家能看出这段代码有什么问题吗?如果你还没看出,那么你对指针及内存的动态分配与释放还需要再学习。


      错误是发生在delete []p,为什么呢?按理说用new[]申请,用delete[]释放,应该没有问题啊。但是错误发生的原因是因为delete[]p释放的是数组 num[100]的空间,而我们申请的空间根本就没有释放,为什么会出现这种情况呢?因为此时的指针已指向了num数组的首地址,而num[100]的空 间会由系统自动释放,而我们现在强行释放,所以会发生错误。


②int *p=new int[3];    *p=1;


    p++;//p的指向改变了,指向了下一空间    *p=2;    delete []p;


大家能发现这段代码有什么问题吗?


      错误还是发生在delete[]p,c/c++规定,当删除一个指针时,这个指针应指向其首地址,而上面的代码中p值已经发生了变化,所以会发生错误, 如何避免呢?可以备份一份;如 int * pbak=p;在释放的时候,用delete[]pbak即可。


③  int* p = new int[10];    int *pp=p;      delete []p;   delete []pp;


这段代码哪里又发生了错误呢?


      我们要知道,p向操作系统申请了10个int类型的空间,而pp只是指向这个空间,操作系统并没有为其再分配10个int类型的空间,所以当你用 delete[]p释放这个空间后,再用delete[]pp释放就会发生错误。其实不管用哪个释放,只要释放一次就行了。


④ int a=100;      itn *p=&a;      delete p;


      看到了这里,如果你还不能看出这段代码的错误,那你前面的白看了,说明你还是没有真正懂得啊! 错误还是发生在delete p,什么原因?因为p并没有通过new获得内存空间,只是指向某个变量,而delete p是强行释放a的空间,肯定发生错误啦

72 Responses to “ delete 和 delete [] 的区别 ”

  1. i love pron说道:

    4Iv08g This unique blog is without a doubt interesting and besides informative. I have picked a lot of interesting tips out of it. I ad love to return every once in a while. Cheers!

  2. ceti说道:

    Wow, superb weblog format! How lengthy have you been blogging for? you made running a blog glance easy. The overall glance of your website is fantastic, let alone the content material!

  3. Guess Romania说道:

    I’а†ve recently started a website, the information you offer on this web site has helped me tremendously. Thank you for all of your time & work.

  4. It as amazing for me to have a web page, which is good in support of my knowledge. thanks admin

  5. The Venus Factor说道:

    Wow, great article.Much thanks again. Cool.

  6. sex说道:

    Thank you ever so for you blog.Really looking forward to read more.

  7. Wow, marvelous blog layout! How long have you ever been running a blog for? you made running a blog glance easy. The whole look of your site is fantastic, as well as the content material!

  8. I truly appreciate this article.Much thanks again. Much obliged.

  9. pretty beneficial stuff, overall I believe this is really worth a bookmark, thanks

  10. I truly appreciate this article post.Really thank you! Will read on

  11. Miami Escorts说道:

    This is really attention-grabbing, You are an overly skilled blogger.

  12. Some genuinely fantastic information, Gladiola I found this.

  13. The Lost Ways说道:

    Thank you ever so for you post.Really looking forward to read more. Really Great.

  14. Say, you got a nice article post.Really looking forward to read more. Really Cool.

  15. amsterdam escort说道:

    Looking forward to reading more. Great blog.Really thank you!

  16. escorts sydney说道:

    Very interesting subject, thank you for posting.

  17. Normally I don at read article on blogs, but I wish to say that this write-up very pressured me to take a look at and do so! Your writing taste has been amazed me. Thanks, quite nice article.

  18. escorts houston说道:

    JAPAN JERSEY ??????30????????????????5??????????????? | ????????

  19. Gurgaon说道:

    Nice post. I learn something new and challenging on blogs I stumbleupon every day. It will always be exciting to read articles from other writers and practice something from other websites.

  20. Nice blog here! Also your site lots up fast! What web host are you the use of? Can I am getting your associate link in your host? I want my site loaded up as quickly as yours lol

  21. 먹튀 검증说道:

    Say, you got a nice article.Much thanks again. Really Great.

  22. Some really prime blog posts on this internet site , saved to favorites.

  23. buy music说道:

    your website, how can i subscribe for a blog website? The

  24. Major thanks for the article.Thanks Again. Cool.

  25. buy mp3说道:

    I will make sure to bookmark your blog and will often

  26. It as hard to find educated people in this particular subject, however, you seem like you know what you are talking about! Thanks

  27. That is a really good tip particularly to those new to the blogosphere. Brief but very accurate information Many thanks for sharing this one. A must read post!

  28. Very nice info and right to the point. I am not sure if this is actually the best place to ask but do you people have any thoughts on where to hire some professional writers? Thx

  29. Looking around While I was browsing yesterday I saw a great article concerning

  30. buy mp3说道:

    site theme. a tones way for your client to communicate. Nice task.

  31. purchase music说道:

    I value the blog article.Really looking forward to read more. Much obliged.

  32. generic levitra说道:

    J aapprecie cette photo mais j aen ai auparavant vu de semblable de meilleures qualifications;

  33. If you are ready to watch funny videos on the internet then I suggest you to go to see this web page, it contains actually so comical not only movies but also other material.

  34. this topic to be really something that I think I would never understand.

  35. 토토 사이트说道:

    I was looking through some of your content on this site and I conceive this internet site is real informative ! Keep putting up.

  36. 바다 이야기说道:

    Your style is really unique compared to other people I ave read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just bookmark this blog.

  37. Right away I am going to do my breakfast, after having my breakfast coming yet again to read additional news.

  38. Regina SEO说道:

    This is a topic that as close to my heart Many thanks! Where are your contact details though?

  39. very nice post, very nice post, i surely really like this site, keep on it

  40. 먹튀 폴리스说道:

    Thanks for the post.Really thank you! Really Great.

  41. I really liked your article.Much thanks again. Much obliged.

  42. You have a number of truly of the essence in a row printed at this point. Excellent job and keep reorganization superb stuff.

  43. Really informative article post.Thanks Again. Awesome.

  44. There is definately a great deal to know about this topic. I love all the points you have made.

  45. Very good blog.Much thanks again. Awesome.

  46. This very blog is definitely entertaining additionally amusing. I have discovered a lot of helpful tips out of this source. I ad love to visit it every once in a while. Thanks!

  47. 다잡아说道:

    There is noticeably a bundle to identify about this. I consider you made certain good points in features also.

  48. 바다이야기说道:

    This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your magnificent post. Also, I have shared your web site in my social networks!

  49. 토토 사이트说道:

    me know if this okay with you. Thanks a lot!

  50. 먹튀 검증说道:

    It as nearly impossible to find experienced people in this particular topic, however, you sound like you know what you are talking about! Thanks

  51. Well I really enjoyed reading it. This post procured by you is very constructive for correct planning.

  52. Yes, you are correct friend, on a regular basis updating weblog is really essential in favor of SEO. Nice discussion keeps it up.

  53. Wow, great blog article.Really thank you! Really Great.

  54. Moreover, The contents are masterpiece. you have performed a wonderful activity in this subject!

  55. I truly appreciate this article.Thanks Again. Cool.

  56. 토토 사이트说道:

    I simply could not depart your website before suggesting that I extremely enjoyed the usual information an individual provide to your visitors? Is gonna be again continuously to check out new posts.

  57. Major thankies for the article post.Really looking forward to read more. Fantastic.

  58. Venus Factor说道:

    Very neat article post.Much thanks again.

  59. Keep up the superb piece of work, I read few articles on this site and I believe that your website is very interesting and has lots of good information.

  60. 토토 사이트说道:

    Usually I do not read post on blogs, however I wish to say that this write-up very compelled me to take a look at and do it! Your writing style has been surprised me. Thank you, very great post.

  61. 바다 이야기说道:

    What as up everybody, here every person is sharing these kinds of experience, therefore it as pleasant to read this webpage, and I used to visit this web site daily.

  62. 다잡아说道:

    Richard Goozh What are the most popular blog sites in the Philippines where I can submit my blogs for others to read?

  63. that should outweigh Owens touchdowns. I think all of it

  64. I really liked your article.Much thanks again. Keep writing.

  65. I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are amazing! Thanks!

  66. What as up everyone, it as my first visit at this web page, and piece of writing is really fruitful designed for me, keep up posting these content.

  67. There is perceptibly a lot to identify about this. I consider you made some good points in features also.

  68. 먹튀사이트说道:

    Major thanks for the article post.Thanks Again.

  69. Muchos Gracias for your blog post.Much thanks again. Awesome.

  70. You produce a strong financially viable decision whenever you decide to purchase a motor vehicle with a

  71. porn说道:

    Loving the info on this site, you have done great job on the content.

  72. Modular Kitchens have changed the very idea of kitchen nowadays since it has provided household females with a comfortable yet a classy place in which they may invest their quality time and space.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>