0

I'm quite new to c++ and I was wondering what this was doing in this code here:

class MyString
{
private:
    char* buffer;

public:
    MyString(const char* initString){
        buffer = NULL;

        cout << "Default constructor: creating new MyString" << endl;

        if(initString != NULL)
        {
            buffer = new char [strlen(initString)+1];
            strcpy(buffer, initString);

            cout << "buffer points to 0x" << hex;
            cout << (unsigned int*)buffer << endl;//<---- here the (unsigned int*)
        }
    }
};

int main(){

    MyString sayHello("Yo guys");

    return 0;
}

why do I have to force re-assign a new pointer poiting to an unsigned int for the buffer with (unsigned int*) knowing its already been assigned a pointer of chars, I don't understand, is it because the strcpy transforms the type or it is something else?. Thanks in advance.

  • 1
    If you try to print `buffer` you'll get a the contents of the string printed because `<<` has an overload that bows to what you want to get the vast majority of times when you print a `char *`: the pointed-at string. This code wants to show you the address pointed at by the `char *`, so it's forcing the `char *` to look like a different type for which `<<` prints the address. Me, I'd have cast to a `void *` because I think it advertises the intent a little better. – user4581301 Feb 05 '20 at 22:57
  • Oh ok thank you @user4581301 , would it be possible for you to post that as an answer so I can accept it? –  Feb 05 '20 at 23:00
  • I will if I can't find the duplicate I'm looking for. – user4581301 Feb 05 '20 at 23:00
  • Using `string` type would make your life easier. – anastaciu Feb 05 '20 at 23:00
  • yes I know @anastaciu, but I'm just trying to see how string actually works. –  Feb 05 '20 at 23:01
  • @SimpleDev [cout << with char* argument prints string, not pointer value](https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value) approaches the problem from the other direction but seems to cover the right points. Would you consider it a good-enough answer? – user4581301 Feb 05 '20 at 23:03
  • 1
    Note: `string` has gotten a bit more complicated lately. Now it's not only a pointer to a `char` array, but it also contains a `char` array so that it doesn't have to allocate a buffer if the string is short (wastes a bit of memory but has HUGE performance advantages) and a bunch of extra book keeping to track the size of the string and possibly the size of the buffer. – user4581301 Feb 05 '20 at 23:05
  • yes @user4581301 but before that why can't I just display the buffer's pointed address using &buffer(address of) simply? –  Feb 05 '20 at 23:07
  • 1
    `buffer` is already a pointer. It is an address. `&buffer` would be the address of the address, `char **` a pointer-to a pointer-to a `char`. In this case you wouldn't get the address you got from `new char [strlen(initString)+1]`, you'd get the address of `buffer`, and that's sitting on the stack (assuming a the near certainly of a stack-based computer). – user4581301 Feb 05 '20 at 23:10
  • Thank you for your answer @user4581301, so essentially what (unsigned int*) does is that it transforms the pointedTo address into a unsigned int 4 byte address and display's it which prints the address of buffer? –  Feb 05 '20 at 23:20
  • 1
    Yes, with caveats. Transform is a dangerous word. It more like, "Compiler, do not use `buffer` as a pointer to a `char`. Behave as though it is a pointer to `int`. I, the almighty Giver Of Data, say that it is acceptable for you to to this technically incorrect thing." Nothing was transformed. The `char *` is being looked at as if it's a `int *`, and the behavior of `<<`ing an `int *` is to print the address. – user4581301 Feb 06 '20 at 00:10
  • 1
    A note: If you try to use that `char *` as a `int *` and look at the `char` array as an `int` array you [violate strict aliasing](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) and [Undefined Behaviour](https://en.wikipedia.org/wiki/Undefined_behavior) results. Maybe it works, maybe it don't work. I'm not 100% certain that just looking at the pointer as a different type is OK, but I can't think of a system where the code will not do what the writer expects. This is partly why I'd use `void*` instead of `int*`. You can use a `void*` to point at anything. – user4581301 Feb 06 '20 at 00:14
  • ok @user4581301 thank you really much, could you put everything you said in the comments as an answer if it isn't duplicate? –  Feb 06 '20 at 11:53

0 Answers0