* (e-mail address removed):
what is the best way to have a buffer in a function, for a (real)
example say a member function
std::string read(int offset, int nbytes){
static const char buffer_size(1024);
On most systems a 'char' is not able to hold the value 1024.
You should use 'ptrdiff_t', or to be conform but impractical, 'size_t'.
static char buffer[buffer_size];
:

read(file_desc,buffer,buffer_size,nbytes,offset);
return(std::string(buffer));
You can't do that unless the data in buffer is zero-terminated.
}
over pushing it on the stack or dynamically allocating it
It depends on how much stack space you have, and whether the routine needs to be
re-entrant for threading.
For typical desktop programming you have a few MiB stack space by default, so a
buffer of 1 KiB is nothing, hence the threading issue doesn't even pop up: for
that general case make that buffer an automatic variable (i.e. on the stack).
In the concrete case above, however, it's just silly to have a buffer at all
since you proceed to immediately create a std::string. In practice (with all
known compilers) std::string allocates a contiguous buffer, and in C++0x that is
guaranteed. So assuming 'pread' returns the number of bytes read, off the cuff,
std::string result( nbytes, '\0' );
result.resize( :

read( file_desc, &result[0], nbytes, offset ) );
return result;
Cheers & hth.,
- Alf