-2

This is although a code specific question but the output is quite bizarre.

I am aware of STL string etc. I was fooling around when I noticed something strange, and could not find a reason for it. :(

See the Two Codes below and the output.

[Code #1] (https://ideone.com/ydB8sQ)

#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>

using namespace std;


class str
{

private:
    vector<char> A;

public:

    str(const char *S) {

        int sz = sizeof(S);

        cerr << sz << endl;

        for (int i = 0; i < sz; ++i) {
            cout << S[i];
            //A.push_back(S[i]);   //!-- Comment --!//
        }
    }
};

int main(int argc, char const *argv[])
{
    str A("");

    return 0;
}

In this, An Empty String is passed and is printed. The Vector A does nothing but is relevant to this problem. In the first version, A is untouched, and the code prints garbage value. (see ideone O/P)

In this second version ( see A.push_back is now uncommented )

[Code #2] (https://ideone.com/PPHGZy)

#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>

using namespace std;


class str
{

private:
    vector<char> A;

public:

    str(const char *S) {

        int sz = sizeof(S);

        cerr << sz << endl;

        for (int i = 0; i < sz; ++i) {
            cout << S[i];
            A.push_back(S[i]);
        }
    }
};

int main(int argc, char const *argv[])
{
    str A("G");

    return 0;
}

The Output is :

Gvector

This is across GCC / MinGW x64. This one never prints garbage value but always contains the word 'vector'.

  • Where is the char* in the function pointing to?
  • Why would 'vector' be there anyways?
  • Also, the size of char * is 8.

EDIT : This does not happen if it isn't wrapped around a 'class'.

The word 'vector' appears always. I supposed it was random garbage value but then how come ideone still has the same word in its memory?

Recoder
  • 818
  • 8
  • 10
  • 3
    What do you think `int sz = sizeof(S);` is doing? Hint: it is not giving you the length of a string. –  Jul 10 '18 at 14:16
  • `sizeof(S)` tells you how much memory the pointer `S` occupies. It has nothing to do with the number of characters in the string. Use `strlen(S)` instead. – Pete Becker Jul 10 '18 at 14:16
  • relaterd/dupe: https://stackoverflow.com/questions/9937181/sizeof-vs-strlen – NathanOliver Jul 10 '18 at 14:17
  • It is char *. I am not using string – Recoder Jul 10 '18 at 14:18
  • A char * is often a pointer to the first character in a null-terminated string, as it is in this case. –  Jul 10 '18 at 14:19

1 Answers1

1

The main problem in your code is in line int sz = sizeof(S);. sizeof(S) is always equal to sizeof(char *) which seems to be 8 on your system. sizeof gives you number of bytes for variable itself. If you want to know number of bytes in string to which your char pointer points, you should use strlen function instead.

You get that vector string in output randomly, as you are accessing memory which is not in allocated space. Accessing such memory is undefined behavior, so you get your undefined result.

V. Kravchenko
  • 1,859
  • 10
  • 12
  • The vector word doesn't seem so random. It is there always. If it was random then sometimes it will print garbage value too. – Recoder Jul 10 '18 at 14:21
  • 1
    @Recoder That's the nature of *Undefined Behavior*: sometimes, it can do what you expect, even if that's not what is logical or consistent. But it's not behavior you can depend on or should ever implement in a sane environment. – Xirema Jul 10 '18 at 14:22
  • @Xirema Then why does in First version, where A is untouched, it prints garbage value. I was expecting garbage value in both cases. – Recoder Jul 10 '18 at 14:25
  • 1
    @Recoder It just happens so that string `"vector"` is stored in your programs memory just after your `"G"` string literal. Probably, there is such string in your programs binary file after that `"G"`. If you add something to your program, or build it with different configuration or settings, that may be a different string or your program may even crash. And may be you'll just get `G` as output. That's why such behavior is undefined. – V. Kravchenko Jul 10 '18 at 14:26
  • 3
    @Recoder -- the most straightforward answer to "why does it do X" is just "because it does X". Don't look for consistency or logic in undefined behavior. It is simply unpredictable. Except when it isn't. – Pete Becker Jul 10 '18 at 14:27
  • @V.Kravchenko Yeah! Adding `char *X = "random"` after the str A("G"); line produces the output `Grandom`. – Recoder Jul 10 '18 at 14:33
  • But in the original program, there isn't any vector string. I hope GCC does not create a string "vector" which I am accessing arbitrarily. – Recoder Jul 10 '18 at 14:35
  • @Recoder probably, gcc inserts that string for debug info purposes. I believe there is some flag for turning that off. – V. Kravchenko Jul 10 '18 at 14:37