-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Exercise 10.5 #227
Comments
@CyberLuc I couldn't agree more. 👍 |
I admire @CyberLuc 's strong skill at such low level, hoping @CyberLuc make more contribution for us. The example given above is great as well. As for this issue, can we change the current answer like below?
|
Thank you for your praise! 😄 And yeah, the updated answer is good! 👍 |
I compiled the following program with #include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::endl;
using std::vector;
int main()
{
// just for explanation, don't use this way.
vector<const char*> roster1 = { "C++", "Primer", "Plus" };
vector<const char*> roster2 = { "C++", "Primer", "Plus" };
cout << "Two const char arrays are "
<< (equal(roster1.cbegin(), roster1.cend(), roster2.cbegin()) ? "equal" : "not equal") << endl;
cout << "Address of roster1: ";
for(const auto &cs : roster1)
cout << static_cast<void*>(cs) << " ";
cout << endl;
cout << "Address of roster2: ";
for(const auto &cs : roster2)
cout << static_cast<void*>(cs) << " ";
cout << endl;
return 0;
} With
With
Obviously, this depends on compiler. It accords with @CyberLuc's result. Thank you! |
@chihyang hi, I tried to test your code, but encountered compile error as below.
It's same to g++ compiler. How did you get your output, or did I do somethig error? Thanks. |
@Zjianglin Hi, I usually used the following commands when I learned the primer (supposing the directory g++ -std=c++11 -Wall -o <output> <input>
clang++ -std=c++11 -stdlib=libstdc++ -Wall -o <output> <input>
cl -EHsc -W4 <input> -Fo./obj/ -Fe./obj/ And the compiler versions I used are as follows: g++: (tdm64-1) 5.1.0
clang++: clang version 3.7.0 (tags/RELEASE_370/final)
cl: 18.00.40629(Visual Studio 2013) On my machine all of the commands above worked well. Hope the info above can help you. |
@chihyang Thanks for your reply. I guess the problem maybe lies in the compiler version. After changing Another question, I saw you converted the
output(Based on
|
Hi, @Zjianglin. got your reply.
All of the snippets here are used to prove that STL algorithm
Thus the snippets try to output the real value a const char* cstr = "c style string";
cout << cstr << endl; Because
Here we want
The link above has given a very detailed explanation. I guess you confuse the address a pointer holds and the address the pointer resides. Here we are only interested in the former. The latter doesn't matter to us. This is a little messy. Hope my reply doesn't increase your confusions if it doesn't ease them. |
Hi, @chihyang . Thanks for your detailed explanation. I think I did not realize Thank you again! |
thanks !! |
Thanks a lot !! Vehry helpful. |
Here is the problem for this conclusion. I disagree the compiler compares address rather than strings. Simply change the code to For the original example, it outputs 1 because compiler assumes the first vector is as long as the second one(it doesn't check the length for you), which means only when the 2nd one is shorter, you can get a right answer. |
I learned a few things from this. thanks a lot ! |
Hi, I'm here again :D
And the answer given is:
But I think it is actually not the same as
std::string
.Code given:
As we know, c-style strings cannot be compared using
==
and<
and>
, 'casue these relational operators compare addresses in pointers rather than strings represented by char pointers.And the
"Mooophy", "pezy", "Queequeg"
inroster1
and"Mooophy", "pezy", "Queequeg"
inroster2
are all literals, and compiler optimized them to be stored in the same addresses. Thus yielded1
.On my pc, I got
0x401ec6
and0x401ec6
.Then look at this:
Which yields
false
.So the answer of this exercise depends. But I think the first situation is just a coincidence and should be avoided;
NOTE: If we define
a
andb
as pointer array, then the result istrue
again.So literals of same contents pointed to by pointers share the same memories, but literals in arrays are stored individually.
The text was updated successfully, but these errors were encountered: