Question:

In the various cases that a buffer is provided to the standard library's many string functions, is it guaranteed that the buffer
will not be modified beyond the null terminator? For example:

char buffer[17] = "abcdefghijklmnop";
sscanf("123", "%16s", buffer);

Is buffer now
required to equal "123\0efghijklmnop"?

Another example:

char buffer[10];
fgets(buffer, 10, fp);

If the read line is only 3 characters long, can one be certain that the 6th character is the same as before fgets was called?

Answer:

Each individual byte in the buffer is an object. Unless some part of the function description of sscanfor fgets mentions
modifying those bytes, or even implies their values may change e.g. by stating their values become unspecified, then the general rule applies: (emphasis mine)

6.2.4 Storage durations of objects

2 [...] An object exists, has a constant address, and retains its last-stored value throughout its lifetime. [...]

It's this same principle that guarantees that

#include <stdio.h>
int a = 1;
int main() {
printf ("%d\n", a);
printf ("%d\n", a);
}

attempts to print 1 twice. Even though a is
global, printf can
access global variables, and the description of printf doesn't
mention not modifying a.

Neither the description of fgets nor
that of sscanf mentions
modifying buffers past the bytes that actually were supposed to be written (except in the case of a read error), so those bytes don't get modified.

Does the C standard guarantee buffers are not touched past their null terminator?的更多相关文章

  1. awk - Unix, Linux Command---reference

    http://www.tutorialspoint.com/unix_commands/awk.htm NAME gawk - pattern scanning and processing lang ...

  2. FTP客户端上传下载Demo实现

    1.第一次感觉MS也有这么难用的MFC类: 2.CFtpFileFind类只能实例化一个,多个实例同时查找会出错(因此下载时不能递归),采用队列存储目录再依次下载: 3.本程序支持文件夹嵌套上传下载: ...

  3. 针对android方法数64k的限制,square做出的努力。精简protobuf

    1.早期的Dalvik VM内部使用short类型变量来标识方法的id,dex限制了程序的最大方法数是65535,如果超过最大限制,无法编译,把dex.force.jumbo=true添加到proje ...

  4. 跨平台的CStdString类,实现了CString的接口

    在实际工作中,std的string功能相对于MFC的CString来说,实在是相形见绌. CStdString类实现了CString的功能,支持跨平台. // ==================== ...

  5. 初次接触:DirectDraw

    第六章 初次接触:DirectDraw 本章,你将初次接触DirectX中最重要的组件:DirectDraw.DirectDraw可能是DirectX中最强大的技术,因为其贯穿着2D图形绘制同时其帧缓 ...

  6. [算法专题] BST&AVL&RB-Tree

    BST 以下BST的定义来自于Wikipedia: Binary Search Tree, is a node-based binary tree data structure which has t ...

  7. PE Header and Export Table for Delphi

    Malware Analysis Tutorial 8: PE Header and Export Table 2. Background Information of PE HeaderAny bi ...

  8. php-5.6.26源代码 - 扩展模块的种类,扩展模块的执行埋点

    模块种类(两种) 类型一:zend的模块:(类似zend_extension=test.so) 识别方法: php.ini中以zend_extension开头的配置,如zend_extension=t ...

  9. pppd - 点对点协议守护进程

    总览 SYNOPSIS pppd [ tty_name ] [ speed ] [ options ] 描述 点对点协议 (PPP) 提供一种在点对点串列线路上传输资料流 (datagrams)的方法 ...

随机推荐

  1. 文件描述符fd、文件指针fp和vfork()

    1. fd:在形式上是一个非负整数.实际上他是一个索引值.指向kernal为每一个进程所维护的该进程打开文件的记录表. 当程序打开一个文件或者创建一个新文件的时候kernal向进程返回一个文件描述符. ...

  2. mybatis中使用常量

    mybatis的mapper文件中项要使用常量的话${@类的全称路劲@常量名称}

  3. JS跨域调用之document.domain--相同基础域名页面之间的调用

    利用document.domain 实现跨域: 前提条件:这两个域名必须属于同一个基础域名!而且所用的协议,端口都要一致,否则无法利用document.domain进行跨域 Javascript出于对 ...

  4. [CocoaPods]使用Gemfile

    RubyGems + Bundler 对于许多人来说,CocoaPods是编程项目中依赖管理的第一个介绍.CocoaPods的很多想法来自类似的项目(例如RubyGems,Bundler,npm和Gr ...

  5. Python总纲路线

    比较全面的Python学习方案: 一,Python 基础教程 二,Python 高级教程 这是系统写学习资料参考,后面会整理单个的学习应用内容. 廖雪峰Python教程传送门

  6. 一个linux内核编译时遇到的perl语法导致的编译问题解决

    在编译linux内核时,遇到了一个比较诡异的问题.具体log如下: Can't locate strict.pm in @INC (you may need to install the strict ...

  7. tomcat-四种运行模式和三种部署模式(优化)

    四中运行模式如下: 1-bio: 传统的Java I/O操作,同步且阻塞IO. 2-nio: JDK1.4开始支持,同步阻塞或同步非阻塞IO 3-aio(nio.2): JDK7开始支持,异步非阻塞I ...

  8. 京东架构师的showtime京东个性化推荐系统实战

    推荐系统核心任务是排序,从线上服务角度看,就是将数据从给定集合中数据选择出来,选出后根据一定规则策略方法进行排序. 线上服务要根据一定规则进行架构设计,架构设计是什么?每一次权衡取舍都是设计,设计需要 ...

  9. docker 容器时间和系统时间不一致

    docker cp /etc/localtime 容器名:/etc/localtime cp /etc/localtime 24fe94504424:/etc/localtime date -s 09 ...

  10. [,,].length等于几

    分别测试了谷歌.欧朋,火狐,QQ.搜狗,Edge,ie5.7.8.9.10.11 其中ie5,ie7,ie8得到的结果为3 其他均为2:如果最后一个逗号后面为空,则不识别最后一位