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)
  • 相关阅读:
    你云我云•兄弟夜谈会 第三季 企业IT架构
    你云我云•兄弟夜谈会 第二季 5G
    C++ 中的不定参数与格式化字符串 # ## vsprintf
    Python多进程池 multiprocessing Pool
    protobuf语法指南
    linux 监控工具netdata
    C++并发编程 条件变量 condition_variable,线程安全队列示例
    C++ 并发编程,std::unique_lock与std::lock_guard区别示例
    linux设置预留端口号,防止监听端口被占用 ip_local_reserved_ports
    goto语句引起的crosses initialization of XXX
  • 原文地址:https://www.cnblogs.com/vigorz/p/10499217.html
Copyright © 2011-2022 走看看