fstream bufer

hello
i'm a little confused .
i know that befoer reading in binary mode from a file using the "read(char* bufer , int syze )" i should alocate inaf memory to store the number of bytes i want to read , but i discaverd that it works iven if i alocate only one byte and then read 10 or 324322 bytes .

the folowing code works in MS visual studio 2008 and dev cpp 4.9

fstream file("some_file", ios::in);
char* buf=new char;
file.read(buf,30);



is this some kind of eror or the read() function resyses the bufer ?????????????
Nope. You're just getting lucky. That should cause an access violation at some point.
I'd only imagine you would get an access violation if you tried to access an instruction of variable that was over-written by the read function. Otherwise, the function would execute without any problems; this would however leave you open to unexpected behavior (or crashes) in your application at other places in the code.

This is one of the pitfalls of C++, there are no bounds checking on dynamically allocated memory.
Topic archived. No new replies allowed.