ошибка в книге или так и задумано
От: jyuyjiyuijyu  
Дата: 19.01.14 06:00
Оценка: -1
Всем привет

тут при чтении книжки наткнулся на странный код...
помедетитировал под отладчиком над ним первый цикл изменяет '\0' в конце строки на 1
и выходит а второй предположительно может шагать по оперативной памяти
очень долго за пределами строки пока не встретит терминатора случайно оказавшегося
где то в оперативной памяти и декрементировав его остановиться


это ошибка автора книги да ?


String^ str = "Nish wrote this book for Manning Publishing";
interior_ptr<Char> ptxt = const_cast< interior_ptr<Char> >(
PtrToStringChars(str));
interior_ptr<Char> ptxtorig = ptxt;
while((*ptxt++)++);
Console::WriteLine(str);
while((*ptxtorig++)--);
Console::WriteLine(str);


You go through the string using a while-loop that increments the pointer as well as
each character until a nullptr is encountered, because the underlying buffer of a
String object is always nullptr-terminated. Next, when you use Console::Write-
Line on the String object, you can see that the string has changed to
Ojti!xspuf!uijt!cppl!gps!Nboojoh!Qvcmjtijoh
You’ve achieved encryption! (Just kidding.) Because you saved the original pointer
in ptxtorig, you can use it to convert the string back to its original form using
another while loop. The second while loop increments the pointer but decrements
each character until it reaches the end of the string (determined by the
nullptr). Now, when you do a Console::WriteLine, you get the original string:
Nish wrote this book for Manning Publishing


книжка тут page 139
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.