问题描述
在下面的代码中,我成功地对其进行了编译,但是我不明白为什么对于某些数值会导致程序崩溃,而对于其他数值却不会.有人可以解释用编译器使用的char *添加long int的行为吗?
#include< iostream>int main(){长整型数= 255;std :: cout<<值1:"<<std :: flush<<("+"号)<<std :: flush<<std :: endl;number = 15155;std :: cout<<"值2:"<<std :: flush<<("+"号)<<std :: flush<<std :: endl;返回0;}
测试结果:
值1:>值2:分段错误
注意:我不是在寻找如何添加带数字的字符串的解决方案.
在C ++中,""
是 const char [1]
数组,其中衰减到指向数组第一个元素的 const char *
指针(在这种情况下,字符串文字的'\ 0'
nul终止符)./p>
向指针添加整数将执行指针算术,这会将指针中的内存地址提前指定数量的指针类型声明为的元素(在这种情况下,为字符
).
因此,在您的示例中, ...<<("+"号)<<...
等同于 ...<<&" [数字]<<...
,或更笼统地说:
const char * ptr =&" [[0];ptr = reinterpret_cast< const char *>(reinterpret_cast< const uintptr_t>(ptr)+(数字* sizeof(字符)));...<<ptr<<...
这意味着当 number
为非0的任何值时,您将超出数组的范围,因此您的代码具有未定义的行为,而当> operator<<
尝试取消引用您赋予它的无效指针.
与许多脚本语言不同,(" +数字)
不是在C ++中将整数转换为字符串的正确方法.您需要改用显式转换函数,例如 std ::to_string()
,例如:
#include< iostream>#include< string>int main(){长整型数= 255;std :: cout<<值1:"<<std :: flush<<std :: to_string(number)<<std :: flush<<std :: endl;数= 15155;std :: cout<<"值2:"<<std :: flush<<std :: to_string(number)<<std :: flush<<std :: endl;返回0;}
或者,您可以简单地 std :: ostream ::运算符<<
为您处理该转换,例如:
#include< iostream>int main(){长整型数= 255;std :: cout<<值1:"<<std :: flush<<数字<<std :: flush<<std :: endl;数= 15155;std :: cout<<"值2:"<<std :: flush<<数字<<std :: flush<<std :: endl;返回0;}
In the code below i successfully compile it but i can't understand why for certain values of number the program crash and for other values it's not. Could someone explain the behavior of adding a long int with a char* that the compiler use?
#include <iostream>
int main()
{
long int number=255;
std::cout<< "Value 1 : " << std::flush << ("" + number) << std::flush << std::endl;
number=15155;
std::cout<< "Value 2 : " << std::flush << ("" + number) << std::flush << std::endl;
return 0;
}
Test results:
Value 1 : >
Value 2 : Segmentation fault
Note: I'm not looking for a solution on how to add a string with a number.
In C++, ""
is a const char[1]
array, which decays into a const char*
pointer to the first element of the array (in this case, the string literal's '\0'
nul terminator).
Adding an integer to a pointer performs pointer arithmetic, which will advance the memory address in the pointer by the specified number of elements of the type the pointer is declared as (in this case, char
).
So, in your example, ... << ("" + number) << ...
is equivalent to ... << &""[number] << ...
, or more generically:
const char *ptr = &""[0];
ptr = reinterpret_cast<const char*>(
reinterpret_cast<const uintptr_t>(ptr)
+ (number * sizeof(char))
);
... << ptr << ...
Which means you are going out of bounds of the array when number
is any value other than 0, thus your code has undefined behavior and anything could happen when operator<<
tries to dereference the invalid pointer you give it.
Unlike in many scripting languages, ("" + number)
is not the correct way to convert an integer to a string in C++. You need to use an explicit conversion function instead, such as std::to_string()
, eg:
#include <iostream>
#include <string>
int main()
{
long int number = 255;
std::cout << "Value 1 : " << std::flush << std::to_string(number) << std::flush << std::endl;
number = 15155;
std::cout << "Value 2 : " << std::flush << std::to_string(number) << std::flush << std::endl;
return 0;
}
Or, you can simply let std::ostream::operator<<
handle that conversion for you, eg:
#include <iostream>
int main()
{
long int number = 255;
std::cout<< "Value 1 : " << std::flush << number << std::flush << std::endl;
number = 15155;
std::cout<< "Value 2 : " << std::flush << number << std::flush << std::endl;
return 0;
}
这篇关于“"的行为是什么?+数字,为什么用c ++编译呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!