Re: buffer...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jase (jshelley_at_nospam.optushome.com.au)
Date: 04/08/04


Date: Fri, 9 Apr 2004 08:58:38 +1000

Okely dokely then. In the case of that function, the buffer can be anything
at all. That's why it's declared as a "const void *". The first parameter is
a pointer to something you want to write to a file, the second parameter is
the number of bytes to write. Here's a couple of examples:

The CFile object used in these examples is the variable "f".
Note that the name of an array variable with no [] is a pointer to the first
element.

1. A line of text.
char szLine[] = "This is a line of text\r\n";
f.Write(szLine, strlen(szLine));

1a. Same thing using a CString
CString str = "This is a lineof text\r\n";
f.Write((LPCTSTR)str, str.GetLength());
str.ReleaseBuffer();

2. An integer array
int array[] = {5, 10, 13};
f.Write(array, sizeof(array));

3. A class object
CSize sz(23, 50);
f.Write(&sz, sizeof(sz)); // note we had to use the & operator here, as
sz is a single object, not an array.

4. A raw integer
int val = 2345;
f.Write(&val, sizeof(val)); // or sizeof(int)

It's all about the fact that an object is ultimately just a map that gives
meaning to the bytes stored in a particular memory block. By taking a
pointer to the first byte of that block and the length of the block as
parameters, this function treats all objects the same. It doesn't know or
care what the memory represents. I recommend you take the time to consider
this, as a good understanding of pointers, objects, and the memory that they
use is essential to C++ programming.

Jase

"RBert" <drbert@microcore.net> wrote in message
news:urH7HhOHEHA.128@tk2msftngp13.phx.gbl...
> Here is a function where I saw mention of a "buffer".
>
>
> CFile::Write
> virtual void Write( const void* lpBuf, UINT nCount );
> throw( CFileException );
>
> Parameters
>
> lpBuf
>
> A pointer to the user-supplied buffer that contains the data to be written
> to the file.
>
>
>
> Thanks,
>
> RABMissouri
>
>
>
>
>
>
>
> "Jase" <jshelley@nospam.optushome.com.au> wrote in message
> news:4072a53a$0$6018$afc38c87@news.optusnet.com.au...
> > In general, a buffer is a block of memory. A function asking for a
buffer
> is
> > asking you do assign the required memory, and pass it a pointer. What
the
> > data in that block means is entirely up to that function.
> >
> > Give me a function, and I can be more specific.
> >
> > Jase
> >
> > "RBert" <drbert@microcore.net> wrote in message
> > news:uAnSsE9GEHA.2160@TK2MSFTNGP12.phx.gbl...
> > > I see that a lot of MFC class methods require buffer parameters. What
> > > exactly, is a buffer?
> > >
> > > Thanks,
> > > RABMissouri
> > >
> >
> >
> >
>
>



Relevant Pages

  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: The value of a CS education
    ... multidimensional arrays badly as a pointer to a pointer. ... demand paged virtual memory machines to their knees. ... Fast algorithms to do this array transpose efficiently have to be cache ... Its interesting to watch how the performance of many applications ...
    (sci.electronics.design)
  • Re: When to stop - how do I work out when to stop processing
    ... I know the length of the entire byte stream. ... //parse buffer received into tlv messages ... However, if doing direct pointer manipulation is still desired, ... with 'process_tlvs' being an array of pointer-to-function. ...
    (comp.lang.c)
  • Re: Library Design, f0dders nightmare.
    ... first demo mistake but I suggest to you that a sequence of blunders ... The stack is only used temporarily while creating the argv array. ... in the input buffer. ... so you are not wasting memory. ...
    (alt.lang.asm)
  • Re: HardBound and SoftBound (was "The State of Software")
    ... completely re-widen, to all of memory, and hence lose all protection. ... e.g. unless you know that malloc'ing is being done out of a common array ... I agree with Nick - SoftBound will fail to detect many common ... interpreted as a pointer across different architectures and memory models... ...
    (comp.arch)