-4

Take this question when i read a program (quote:c++ primer plus) 4.22 and make some change in it

#include <iostream>
#include <cstring>

using namespace std;

char * getName();

int main(int argc, const char * argv[]) {
// insert code here...

char *name;

name = getName();
string sb = name;
cout << name << " at " << (int *)name << "\n";
cout << *name << "   " << sb << endl;
cout << &sb << endl;
delete [] name;

name = getName();
sb = name;
cout << name << " at " << (int *) name << "\n";
cout << *name << "   " << sb << endl;
cout << &sb << endl;
delete [] name;

cout << sb << endl;
cout << &sb << endl;

return 0;
}

char *getName()
{
char temp[80];
cout << "Enter last name: ";
cin >> temp;
char *pn = new char[strlen(temp) + 1];
strcpy(pn, temp);
return pn;
}

and there have no memory leak. i check sb's address is different from name. i think may it work like this std::string

first step : malloc (char *, sizeof(char *) * sizeof(name) ) then : snprintf( string , sizeof (string) , "%s", name) final : string get a new address

if i use a statement: string = string + " am i a pointer"; it still work. i guess it is really a pointer! just realloc it

hereby, i think string is kind of char* or char array, i try to *string then the compiler tell me something wrong with it! i can't use like this, so i hope someone can explain what type of string is in compiler and how it work! why string is not a pointer? or it's actually a pointer but it have special method to use? tip:All work on mac os

  • 5
    `string` is a class. When you need a pointer use member function `c_str()`. When you change a string old pointer is possibly no longer valid, but a new call to `c_str()` will give you a valid one. – Dialecticus Dec 24 '15 at 13:47
  • 4
    This has nothing to do with the compiler. – Mad Physicist Dec 24 '15 at 13:48
  • `std::string` is a class: [cppreference](http://en.cppreference.com/w/cpp/string/basic_string) – melak47 Dec 24 '15 at 13:48
  • @Zeta there is one `string sb` near the top, there is then a LOT of undefined behavior that follows – Mgetz Dec 24 '15 at 13:50
  • @Mgetz: Yeah, found it and deleted my comment. `using namespace std;` always makes me miss `std::*` classes. – Zeta Dec 24 '15 at 13:50
  • @Zeta I'm going to leave mine only because I point out the UB that follows – Mgetz Dec 24 '15 at 13:51
  • Its called *object orientation*. Instead of you writing code to allocate memory for the string and reallocating it when the string changes the string knows how to manage these operations for itself. That way you don't have to write the allocation/reallocation code every time you use a string, its all written once inside the string class definition. – Galik Dec 24 '15 at 13:57
  • Thank for all response , i get it – user5713594 Dec 24 '15 at 13:59
  • 2
    @user5713594 I would suggest getting a different C++ book perhaps one off the [C++ book list](http://stackoverflow.com/q/388242/332733) – Mgetz Dec 24 '15 at 14:10

1 Answers1

2

i hope someone can explain what type of string is

It is a class.

why string is not a pointer? or it's actually a pointer but it have special method to use?

It's not a pointer. A pointer couldn't store the necessary data to fulfill the requirements defined by the standard.

i think string is kind of char* or char array

It's not. However, it quite probably does have a char pointer to an array as a data member.

eerorika
  • 232,697
  • 12
  • 197
  • 326