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 functionchar *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.
c++: Does the new operator for dynamic allocation check for memory safety?的更多相关文章
- Pointers and Dynamic Allocation of Memory
METHOD 1: Consider the case where we do not know the number of elements in each row at compile time, ...
- Android 性能优化(23)*性能工具之「Heap Viewer, Memory Monitor, Allocation Tracker」Memory Profilers
Memory Profilers In this document Memory Monitor Heap Viewer Allocation Tracker You should also read ...
- lwIP Memory Management
http://lwip.wikia.com/wiki/Lwipopts.h Memory management (RAM usage) /** * MEM_LIBC_MALLOC==1: Use ma ...
- PatentTips - Systems, methods, and devices for dynamic resource monitoring and allocation in a cluster system
BACKGROUND 1. Field The embodiments of the disclosure generally relate to computer clusters, and m ...
- Pooled Allocation(池式分配)实例——Keil 内存管理
引言:说到动态申请(Dynamic Allocation)内存的好处,学过C/C++的人可能都有体会.运行时的灵活申请自然要比编码时的猜测好的多.而在内存受限情况下这种灵活性又有特别的好处--能让我们 ...
- 内存管理(memory allocation内存分配)
Memory management is the act of managing computer memory. The essential requirement of memory manage ...
- C++ operator overload -- 操作符重载
C++ operator overload -- 操作符重载 2011-12-13 14:18:29 分类: C/C++ 操作符重载有两种方式,一是以成员函数方式重载,另一种是全局函数. 先看例子 # ...
- dynamic详解
一.简介 在通过 dynamic 类型实现的操作中,该类型的作用是绕过编译时类型检查, 改为在运行时解析这些操作. dynamic 类型简化了对 COM API(例如 Office Automatio ...
- Memory Allocation with COBOL
Generally, the use of a table/array (Static Memory) is most common in COBOL modules in an applicatio ...
随机推荐
- VP-UML系统建模工具研究
一.基本信息 标题:VP-UML系统建模工具研究 时间:2014 出版源:软件工程师 领域分类:面向对象:CASE:UML:系统建模: 二.研究背景 问题定义:VP-UML系统建模的主要特点 难点:运 ...
- JDK、JRE、JVM之间的关系
JDK.JRE.JVM之间的关系 1.JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads ...
- PMP:7.项目成本管理
内容中包含 base64string 图片造成字符过多,拒绝显示
- java之Stack详细介绍
1 Stack介绍 Stack简介 Stack是栈.它的特性是:先进后出(FILO, First In Last Out). java工具包中的Stack是继承于Vector(矢量队列)的,由于Ve ...
- 设计模式总结(Java)—— 单例模式
1. 定义 为了确保一个类有且仅有一个实例,而且自行实例化并向整个系统提供这个实例. 2. 使用场景 确保某个类有且只有一个对象的场景,避免产生多个对象消耗过多的资源,或者某种类型的对象只应该有且只有 ...
- B - Red and Black 问题思考
红黑地板问题 There is a rectangular room, covered with square tiles. Each tile is colored either red or bl ...
- Testing - 软件测试知识梳理 - 比较质量保证(QA)与质量控制(QC)
QA QC QM 概念 Quality Assurance (质量保证) Quality Control (质量控制) Quality Manage (质量管理) 定义 为达到质量要求所采取的作业技术 ...
- vue 自学笔记(三) 计算属性与侦听器
一:计算属性 虽然在模板内使用表达式对属性进行处理十分便利,例如在小胡子语法里写number + 1实现对数据的简单处理,但若我们在其中加入大量的代码,使得逻辑变重,导致难以维护.例如下面的代码,并不 ...
- LeetCode--No.001 Two Sum
Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...
- enumerate使用
# enumerate读取文件import enumfor index, line in enumerate(open('C:\\Users\\CTO\\Desktop\\spider\\douban ...