-1

I know there is a bug in the code, but I am interested to know exactly how C++14 processes the code to output this

3
þÿÿt

With this code

#include <iostream>

void pretty_print(int,int,int);

int main()
{
    srand(time(0));
    const int LIM = 100;
    int a = rand()%LIM;
    int b = rand()%LIM;
    int c = rand()%LIM;
    if(a+b+c<500)
    {
        pretty_print(a,b,c);
    }
    else throw new std::invalid_argument("FOOBAR");
    return 0;
}

void pretty_print(int a, int b,int c)
{
    std::string ans = "";
    int count = 0;
    if(a!=0)
    {
        count++;
        ans+=(a+" ");
    }
    if(b!=0)
    {
        count++;
        ans+=(b+" ");
    }
    if(c!=0)
    {
        count++;
        ans+=(c+" ");
    }
    std::cout << count << "\n";
    std::cout << ans << "\n";
}

P.S. I have deliberately tried to add an integer to a string without conversion in order to examine why the output is such. I want to know why adding an integer without conversion to a string results in such a behaviour.

Jagreet
  • 113
  • 1
  • 10

2 Answers2

3

I want to know why adding an integer without conversion to a string results in such a behaviour.

A string literal is an array of characters. In a value context (such as as an operand of +), an array will be converted into pointer to first element. This implicit conversion is called decaying.

Adding an integer n to a pointer (to an element of an array) will result in a pointer to nth successive sibling of the originally pointed element. For example, "abcd" + 1 will result in a pointer to the characer b. This is called pointer arithmetic.

You generate numbers up to 100, so you end up getting a pointer to up to hundredth index of the string literal. However, the length of the string literal is only 2 (the length of the array includes the null terminator in addition to the length of the string itself), so unless you happened to generate only zeroes and ones, those pointers will be outside the bounds of the string literal.

When a character pointer is passed to the compound assignment operator of std::string, that pointer will be treated as a null terminated pointer to a character string and the pointed object will be accessed.

The behaviour of accessing an array out of bounds is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326
3

In C++ a string literal like e.g. " " is really an array of constant characters. As such it will decay to a pointer to its first element.

What you're doing with a+" " is pointer arithmetic: You add the value of a to the pointer pointing to the first character of " ". In other words, a + " " is equal to &" "[a]. (It helps knowing that for any pointer or array p and index i the expression *(p + i) is exactly equal to p[i].)

Unless the values of your variables is 0 (or 1, which is the index of the terminator), then the values as indexes will be way out of bounds of the array holding " ", leading to undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621