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.
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 ...
随机推荐
- USART of STM32
/*************************************************************************** * 文件名:USART.h * * 编写人:离 ...
- PHP字符串函数之 sscanf echo print sprintf vsprintf printf vprintf fprintf vfprintf
sscanf – 根据指定格式解析输入的字符 echo – 输出一个或多个字符串 print – 输出字符串 sprintf – 返回格式化字符串 vsprintf – 返回格式化字符串 (参数为数组 ...
- git dev 分支merge到master
code reviewer之后,需要把dev分支的代码merge到master分支.通过在azkaban的服务器上git pull,最终将代码上线. git dev 分支merge到master # ...
- [Project] MiniSearch文本检索简介
1. 预处理过程 预处理主要用来事先生成程序在运行过程中可能用到的数据,以便加速处理时间. 预处理的过程主要生成程序所需的三个文件:网页库文件,网页位置信息文件和倒排索引文件. 网页库文件 其中网页库 ...
- Java 虚拟机的对象创建
堆中存储的内容:在程序运行时,动态创建的对象. 创建对象的四种方式:new,clone(浅复制),反射,反序列化. 浅复制:只能复制当前对象本身,如果当前对象(A)引用了另外的对象(B),则引用对象( ...
- 多核CPU配合负载均衡可以这样用,为老板省点钱
负载均衡作为一个处理高并发,大流量的访问的业务场景,已经几乎是常识性的知识了. 而本文的意义在于需求:由于大流量请求,导致服务无法正常响应,在不增加购买机器成本的场景下,如何提高服务器的业务处理能力? ...
- 软件测试人员需要掌握的linux命令(一)
有些技能可以事半功倍,熟练的使用这些命令可以提高工作效率,并且结合这些命令对测试过程中遇到的问题进行一些初步的定位. 一:目录与文件操作: ls 使用权限:所有人功能 : 显示指定工作目录下之内容(列 ...
- spring boot log4j2配置
[传送门]:log4j官网配置文件详解 1. 排除 spring boot 自带的 spring-boot-starter-logging 依赖 <dependency> <gro ...
- 目标检测的图像特征提取之HOG特征
HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的梯度 ...
- 机器学习基石笔记:13 Hazard of Overfitting
泛化能力差和过拟合: 引起过拟合的原因: 1)过度VC维(模型复杂度高)------确定性噪声: 2)随机噪声: 3)有限的样本数量N. 具体实验来看模型复杂度Qf/确定性噪声.随机噪声sigma2. ...