Skip to content

Commit

Permalink
Merge pull request pezy#37 from pezy/master
Browse files Browse the repository at this point in the history
fixed some bugs.
  • Loading branch information
Mooophy committed Nov 13, 2014
2 parents 58d5ff6 + 6c0372e commit 8d4a4e9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
35 changes: 34 additions & 1 deletion ch03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,40 @@ while (*cp) {
}
```

Print all the elements of the array.
Print all the elements of the array.

-----
**WARNING!!!!**
When we use a string, the compiler put it in the section `.rodata`, the code uses C-style character string without adding a '\0' in the end of `ca`.
So, when we code like this:

```cpp
const char ca[] = {'h', 'e', 'l', 'l', 'o'};
const char s[] = "world";
const char *cp = ca;
while (*cp) {
cout << *cp;
++cp;
}
```

The code will print "helloworld" when you run it.
because the character list in the `.rodata` like this:

h e l l o w o r l d \0
`While(*cp)` judge wether *cp is 0 or not. when *cp is not 0, it will print the character until 0.
When you change the code like this:

const char ca[] = {'h', 'e', 'l', 'l', 'o', '\0'};
the character list in the `.rodata`:

h e l l o \0 w o r l d \0
The program will run correctly. So when using C-style character string, be careful!!

----
see `.rodata`, you can use this command:

hexdump -C a.out

##Exercise 3.38
>In this section, we noted that it was not only illegal but meaningless to try to add two pointers.
Expand Down
4 changes: 2 additions & 2 deletions ch04/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ and potential pitfalls in the use of the language by the programmer.
Do you consider that an acceptable trade-off? Why or why not?

Yes, I think it necessary to hold the trade-off.
Because the speed always the biggest advantage of C++. Sometimes, we need
Because the speed is always the biggest advantage of C++. Sometimes, we need
the compiler's features for efficient work. But if you are not a expert. I
have to advice you do not touch the undefined behaviors.

Expand Down Expand Up @@ -130,7 +130,7 @@ dval = ival = pi = 0;
// can not assign to 'int' from type 'int *'
// correct it:
dval = ival = 0;
pi = &ival;
pi = 0;
```

##Exercise 4.16
Expand Down
4 changes: 3 additions & 1 deletion ch05/ex5_24.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <iostream>
#include <stdexcept>

using namespace std;

int main(void)
Expand All @@ -12,4 +14,4 @@ int main(void)
cout << static_cast<double>(a) / b << endl;

return 0;
}
}

0 comments on commit 8d4a4e9

Please sign in to comment.