zoukankan      html  css  js  c++  java
  • c++: Does the new operator for dynamic allocation check for memory safety?

    Quesion:
    My question arises from one of my c++ exercises (from Programming Abstraction in C++, 2012 version, Exercise 12.2). Here it is:

    void strcpy(char *dst, char *src) {
        while (*dst++ = *src++);
    }

    The definition of strcpy is dangerous. The danger stems from the fact that strcpy fails to check that there is sufficient space in the character array that receives the copy, thereby increasing the chance of a buffer-overflow error. It is possible, however, to eliminate much of the danger by using dynamic allocation to create memory space for the copied string. Write a function

    char *copyCString(char *str);

    that allocates enough memory for the C-style string str and then copies the characters—along with the terminating null character—into the newly allocated memory.

    Here's my question: Is this new method really safe? Why it's safe? I mean, to be a little bit radical, what if there isn't enough space in the heap? Is the new operator able to check for space availability and fall in an elegant way if there isn't enough space? Will that cause other kind of "something-overflow"?


    Answer:

    If new fails to allocate the requested memory, it's supposed to throw a std::bad_alloc exception (but see below for more). After that, the stack will be unwound to the matching exception handler, and it'll be up to your code to figure out what to do from there.

    If you really want/need to assure against an exception being thrown, there is a nothrow version of new you can use that will return a null pointer to signal failure--but this is included almost exclusively for C compatibility, and not frequently used (or useful).

    For the type of situation cited in the question, you normally want to use std::string instead of messing with allocating space yourself at all.

    Also note that on many modern systems, the notion of new either throwing or returning a null pointer in case of failure, is really fairly foreign. In reality, Windows will normally attempt to expand the paging file to meet your request. Linux has an "OOMKiller" process that will attempt to find "bad" processes and kill them to free up memory if you run out.

    As such, even though the C++ standard (and the C standard) prescribe what should happen if allocation fails, that's rarely what happens in real life.


    想要看到更多学习笔记、考试复习资料、面试准备资料? 想要看到IBM工作时期的技术积累和国外初创公司的经验总结? 敬请关注: [CSDN](https://blog.csdn.net/u013152895) [简书](https://www.jianshu.com/u/594a3de3852d) [博客园](https://www.cnblogs.com/vigorz/) [51Testing](http://www.51testing.com/?15263728)
  • 相关阅读:
    每周进度及工作量统计——2016.10.06-2016.10.13
    SCRUM站立会议模拟
    java词频统计——web版支持
    每周进度及工作量统计——2016.9.22--2016.9.29
    简易四则运算生成程序——第一次改进后的单元测试
    java词频统计——改进后的单元测试
    第一次通读《构建之法》阅读笔记
    centOS6.5网络配置
    webpack安装
    CommonJS和AMD/CMD
  • 原文地址:https://www.cnblogs.com/vigorz/p/10499217.html
Copyright © 2011-2022 走看看