列表初始化的char数组是否仍以null终止? [英] Are list-initialized char arrays still null-terminated?

查看:113
本文介绍了列表初始化的char数组是否仍以null终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我研究Lippman C ++ Primer(第5版,C ++ 11)时,遇到了以下代码:

As I worked through the Lippman C++ Primer (5th ed, C++11), I came across this code:

char ca[] = {'C', '+', '+'};  //not null terminated
cout << strlen(ca) << endl;  //disaster: ca isn't null terminated

在ca上调用库strlen函数(该函数不是以空值结尾)会导致未定义的行为. Lippman等人说:此调用的最可能的效果是,strlen会一直浏览ca后面的内存,直到遇到空字符为止."

Calling the library strlen function on ca, which is not null-terminated, results in undefined behavior. Lippman et al say that "the most likely effect of this call is that strlen will keep looking through the memory that follows ca until it encounters a null character."

稍后的练习将询问以下代码的作用:

A later exercise asks what the following code does:

const char ca[] = {'h','e','l','l','o'};
const char *cp = ca;
while (*cp) {
   cout << *cp << endl;
   ++cp;
}

我的分析:ca是一个不以null结尾的char数组. cp(指向char的指针)最初保留ca [0]的地址. while循环的条件取消引用指针cp,在上下文中将所得的char值转换为bool,并且仅在转换结果为"true"时才执行循环块.由于任何非空char都将转换为bool值'true',因此将执行循环块,将指针增加char的大小.循环然后遍历内存,打印每个字符,直到到达空字符为止.由于ca不是以空值结尾的,所以循环可能会继续经过ca [4]的地址,将后面的内存地址的内容解释为char并将它们的值写入cout,直到碰巧遇到了一些发生的位代表空字符(全0).这种行为类似于Lippman等人在前面的示例中提出的strlen(ca)行为.

My analysis: ca is a char array that is not null-terminated. cp, a pointer to char, initially holds the address of ca[0]. The condition of the while loop dereferences pointer cp, contextually converts the resulting char value to bool, and executes the loop block only if the conversion results in 'true.' Since any non-null char converts to a bool value of 'true,' the loop block executes, incrementing the pointer by the size of a char. The loop then steps through memory, printing each char until a null character is reached. Since ca is not null-terminated, the loop may continue well past the address of ca[4], interpreting the contents of later memory addresses as chars and writing their values to cout, until it happens to come across a chunk of bits that happen to represent the null character (all 0's). This behavior would be similar to what Lippman et al suggested that strlen(ca) does in the earlier example.

但是,当我实际执行代码(再次使用g ++ -std = c ++ 11进行编译)时,程序会始终打印:

However, when I actually execute the code (again compiling with g++ -std=c++11), the program consistently prints:

'h'
'e'
'l'
'l'
'o'

并终止.为什么?

推荐答案

最可能的解释:在现代台式机/服务器操作系统(例如Windows和linux)上,内存在被映射到程序的地址空间之前被清零.因此,只要程序不将相邻的内存位置用于其他内容,它就看起来像是一个以null终止的字符串. 在您的情况下,相邻字节可能只是填充,因为大多数变量至少对齐了4个字节.

Most likely explanation: On modern desktop/server operating systems like windows and linux, memory is zeroed out before it is mapped into the address space of a program. So as long as the program doesn't use the adjacent memory locations for something else, it will look like a null terminated string. In your case, the adjacent bytes are probably just padding, as most variables are at least 4-Byte aligned.

就语言而言,这只是未定义行为的一种可能实现.

As far as the language is concerned this is just one possible realization of undefined behavior.

这篇关于列表初始化的char数组是否仍以null终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆