Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

segmentation fault - What's the behaviour of "" + number and why c++ compile it?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 '' 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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...