D
deech
Hi all,
I am reading characters from input using 'cin' into a character
buffer. I want to do it dynamically and I wrote some code to do so,
and it seems to work, but I am concerned that I am doing something
that is undefined. Here is the code :
int main () {
char * buffer = new char;
char * iterator = buffer;
int buffer_length = 0;
while ((*iterator = cin.get()) != EOF) {
iterator++;
buffer_length++;
}
*iterator = '\0';
cout << "Buffer Contents : " << buffer << endl;
cout << "Buffer length : " << strlen(buffer) << endl;
}
All the examples I've seen ask you to predetermine the number of
characters that are going to be entered, and store them in a fixed-
size char array. But this seems to work, and is more flexible. K&R
tells me that modifying the contents of a pointer leads to undefined
behavior.
Is this a good way of doing what I want?
thanks ...
-deech
I am reading characters from input using 'cin' into a character
buffer. I want to do it dynamically and I wrote some code to do so,
and it seems to work, but I am concerned that I am doing something
that is undefined. Here is the code :
int main () {
char * buffer = new char;
char * iterator = buffer;
int buffer_length = 0;
while ((*iterator = cin.get()) != EOF) {
iterator++;
buffer_length++;
}
*iterator = '\0';
cout << "Buffer Contents : " << buffer << endl;
cout << "Buffer length : " << strlen(buffer) << endl;
}
All the examples I've seen ask you to predetermine the number of
characters that are going to be entered, and store them in a fixed-
size char array. But this seems to work, and is more flexible. K&R
tells me that modifying the contents of a pointer leads to undefined
behavior.
Is this a good way of doing what I want?
thanks ...
-deech