Allow me to clarify some things.
A "buffer" is a block of memory. IE, if you want to be able to have 100 characters... then you'll need a buffer that is at least 100 characters large.
A "pointer" is not a buffer. A pointer is merely a variable that holds an address. That address
may or may not be the address of a buffer, depending on where/how you got the address.
That said...
Using the new keyword (
char* x = new char;
) does 2 things:
1) creates a
single unnamed char variable. We'll call this variable 'foo'
2) returns a pointer to that unnamed char and assigns it to our 'x' pointer.
Therefore... at the end of this.. 'x' will point to 'foo'. But 'foo' is only large enough to hold a single character since that's all you allocated.
On the other hand...
Using the new[] keyword (
char* x = new char[100];
) does the same 2 things, slightly differently:
1) Creates an unnamed buffer of 100 chars.
2) Returns a pointer to the first char in that buffer and assigns it to 'x'.
So this time... foo is a proper buffer capable of holding no more than 100 chars. And 'x' will properly point to that buffer.
Keep in mind that anything you allocate with new needs to be unallocated with delete. Likewise anything allocated with new[] needs to be unallocated with delete[].
And really... you should
avoid dynamic allocation unless you absolutely need it. You are doing a lot of unnecessary allocation in your original post which leads me to believe you have some horrible misunderstanding about how C++ works.
For academic purposes... you could rewrite your original code to use dynamically allocated memory like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
char* buffer = new char[100]; // buffer large enough to hold 100 characters
char* p = buffer; // 'p' point to the first character in that buffer
// 'p' will change to point to other chars in the buffer...
// whereas we want 'buffer' to always point to the first char...
//
// p effectively points to the 'next' character in the buffer
while(filestream.read(p,1)) // <- read a character into the next spot in the buffer
{
++p; // and increment 'p' to point to the next character
}
// at this point... 'buffer' will contain the contents of the file
// (but note that if the file was larger than 100 characters, you'll have memory corruption
// which is BAD BAD BAD)
// since the buffer 'buffer' points to was dynamically allocated. We need to delete[] it
// when we're done with it to avoid memory leaks
delete[] buffer;
|
Of course.... this is largely unnecessary. There is no value in reading 1 character at a time. read() can read any number of characters all at once. So instead... let's have a look at this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// same business as before, except let's get rid of 'p' because we don't need it
char* buffer = new char[100];
filestream.read(buffer,100); // <- just read 100 characters into 'buffer' directly
// That's it. No loop or pointer math required.
// But note we still can only read 100 characters (the max size of the buffer)
// and we STILL need to delete[] because we used new[].
// so when you're done with this string data, don't forget to delete it:
delete[] buffer;
|
But EVEN THIS can be simplified. If we always want a buffer that's 100 characters long... we
don't need to dynamically allocate it. Therefore we can save the new[]/delete[] headaches and just make the buffer directly:
1 2 3 4 5 6
|
// don't use new[]. Just make the buffer directly:
char buffer[100];
filestream.read(buffer,100); // <- just read 100 characters into 'buffer'
// that's it! No need to delete[] anything because nothing was new[]'d
|
Though this would be a lot easier if you could use strings. Any reason why you have to use char buffers?