I appreciate it and all, but the problem is that my version DOES work... kind of. Let me try again...
Suppose I create something on the free store like above, and want to write to it:
1 2 3 4 5 6 7
|
char* forExample{new char[size + 1]};
while(*passedParameter){
*forExample++ = *passedParameter++;
}
*forExample++ = 0;
return forExample;
|
This won't work for me. It will only print garbage. HOWEVER...
1 2 3 4 5 6 7 8 9
|
char* forExample{new char[size + 1]};
char* forExampleCopy{forExample};
while(*passedParameter){
*forExampleCopy++ = *passedParameter++;
}
*forExampleCopy++ = 0;
return forExample;
|
This WILL work, and work correctly, and I'm not understanding why.
All I can ASSUME is that it's because of the same reason I SUSPECT calling delete[] on a pointer to free store memory also returns an error - it has been incremented, and thus isn't pointing to element 0.
I don't know. I just don't understand, and the book never REALLY gets into the dynamics behind it (Programming Principles and Practice). Take an except from above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
//Test orig against vowels
while (*orig) { //Missing star?! Replace orig with orig_copy
while (*v_pointer) {
if (*orig == *v_pointer) {
not_vowel = false;
break;
}
++v_pointer;
}
v_pointer = vowels;
if (not_vowel) *removed_copy++ = *orig;
not_vowel = true;
++orig;
}
*removed_copy++ = 0;
|
Before (when I didn't care what the size was) I used:
1 2 3
|
while(orig){
//all the above
}
|
But that failed EVERY SINGLE TIME, and you can bet I ran it while shouting profanity numerous times. But when I dereferenced it:
1 2 3
|
while(*orig){ //That star!
//all the above
}
|
No problems! I took that to mean they (pointers) DEFINITELY don't know where their end is, EVEN WHEN PASSED AS A "C-STYLE" STRING:
1 2
|
char unvowel[]{ "Just a string that needs unvoweling" };
char* diditwork{ devowel(unvowel) };
|
Sorry. I'm just getting a bit frustrated with it is all, and it ALWAYS happens when I return to C-Style strings. Maybe the point wasn't to just help me understand them, but gain appreciation for the vector and string classes? Mission THOROUGHLY accomplished, and I've long refused to use them, but I DO want to understand them...