How to know the buffer size and increase buffer size in c++

Discussion in 'C++' started by Raja, Jun 17, 2004.

  1. Raja

    Raja Guest

    How to know the buffer size and increase buffer size in c++.
     
    Raja, Jun 17, 2004
    #1
    1. Advertising

  2. "Raja" <> wrote in message
    news:...
    > How to know the buffer size and increase buffer size in c++.


    Buffer size of what?

    john
     
    John Harrison, Jun 17, 2004
    #2
    1. Advertising

  3. Raja

    Mike Wahler Guest

    "Raja" <> wrote in message
    news:...
    > How to know the buffer size


    Decide what size you want, and remember that.

    > and increase buffer size in c++.


    Add the desired amount to the above, and
    change it to the new size.

    Perhaps if you'd be more specific, we could
    give a more specific answer.


    -Mike
     
    Mike Wahler, Jun 17, 2004
    #3
  4. Raja

    Julie Guest

    Raja wrote:
    >
    > How to know the buffer size and increase buffer size in c++.


    There is no intrinsic support in C++ to be able to determine the size of an
    arbitrary allocated memory block (buffer?). You must keep track of the memory
    block yourself.

    To increase the size (when necessary), you simply need to allocate a new piece
    of memory that is of suitable size, copy over the contents of the previous to
    the new, update references to the previous to point to the new, and then
    release the previous.
     
    Julie, Jun 17, 2004
    #4
  5. Raja

    Pete C. Guest

    Julie wrote:
    > Raja wrote:
    >>
    >> How to know the buffer size and increase buffer size in c++.

    >
    > There is no intrinsic support in C++ to be able to determine the size
    > of an arbitrary allocated memory block (buffer?). You must keep
    > track of the memory block yourself.
    >
    > To increase the size (when necessary), you simply need to allocate a
    > new piece of memory that is of suitable size, copy over the contents
    > of the previous to the new, update references to the previous to
    > point to the new, and then release the previous.


    Of course there's also std::vector<> ...

    - Pete
     
    Pete C., Jun 17, 2004
    #5
  6. Thomas Matthews, Jun 17, 2004
    #6
  7. Raja

    AngleWyrm Guest

    "Julie" <> wrote in message
    news:...
    > Raja wrote:
    > >
    > > How to know the buffer size and increase buffer size in c++.

    >
    > There is no intrinsic support in C++ to be able to determine the size of

    an
    > arbitrary allocated memory block (buffer?). You must keep track of the

    memory
    > block yourself.


    In a similar vein about determining allocated memory, I recently ran across
    this little gem:

    //
    // A handy two-line array_size() template function
    //
    #include <cstdlib>
    #include <iostream>
    using namespace std;


    // Uses template type induction to
    // discover the size of an array
    // so that you don't have to use sizeof(array)/sizeof(element)
    template<typename T, int size>
    int array_size(T (&)[size]) { return size; };


    int main(){

    int my_array[] = { 1, 2, 3, 4, 5 };// implicit size

    cout << "array size is : " << array_size(my_array) << endl;

    system("pause");
    }
     
    AngleWyrm, Jun 17, 2004
    #7
  8. Raja

    Mats Weber Guest

    Re: Determining array size

    "AngleWyrm" <> wrote in message news:<DPjAc.64455$0y.24244@attbi_s03>...

    > In a similar vein about determining allocated memory, I recently ran across
    > this little gem:
    >
    > //
    > // A handy two-line array_size() template function
    > //
    > #include <cstdlib>
    > #include <iostream>
    > using namespace std;
    >
    >
    > // Uses template type induction to
    > // discover the size of an array
    > // so that you don't have to use sizeof(array)/sizeof(element)
    > template<typename T, int size>
    > int array_size(T (&)[size]) { return size; };
    >
    >
    > int main(){
    >
    > int my_array[] = { 1, 2, 3, 4, 5 };// implicit size
    >
    > cout << "array size is : " << array_size(my_array) << endl;
    >
    > system("pause");
    > }


    That's great, and it's nice to know that there is something better
    than sizeof. But I do not understand why it works. I looked up the
    "(&)" syntax in Stroustrup's book but couldn't find it. Could someone
    explain ?
     
    Mats Weber, Jun 19, 2004
    #8
  9. Re: Determining array size

    "Mats Weber" <> wrote in message
    news:...
    > "AngleWyrm" <> wrote in message

    news:<DPjAc.64455$0y.24244@attbi_s03>...
    >
    > > In a similar vein about determining allocated memory, I recently ran

    across
    > > this little gem:
    > >
    > > //
    > > // A handy two-line array_size() template function
    > > //
    > > #include <cstdlib>
    > > #include <iostream>
    > > using namespace std;
    > >
    > >
    > > // Uses template type induction to
    > > // discover the size of an array
    > > // so that you don't have to use sizeof(array)/sizeof(element)
    > > template<typename T, int size>
    > > int array_size(T (&)[size]) { return size; };
    > >
    > >
    > > int main(){
    > >
    > > int my_array[] = { 1, 2, 3, 4, 5 };// implicit size
    > >
    > > cout << "array size is : " << array_size(my_array) << endl;
    > >
    > > system("pause");
    > > }

    >
    > That's great, and it's nice to know that there is something better
    > than sizeof. But I do not understand why it works. I looked up the
    > "(&)" syntax in Stroustrup's book but couldn't find it. Could someone
    > explain ?


    Perhaps easier to understand like this

    template<typename T, size_t N>
    size_t array_size(T (&dummy)[N]) { return N; }

    dummy is a reference to an array T of size N. Because the parameter is
    unused C++ allows you to omit it as AngleWyrm did.

    john
     
    John Harrison, Jun 19, 2004
    #9
  10. Re: Determining array size

    >
    > dummy is a reference to an array T of size N.


    an array of T of size N

    john
     
    John Harrison, Jun 19, 2004
    #10
  11. Raja

    David Rubin Guest

    Re: Determining array size

    "John Harrison" <> wrote in message

    [snip]
    > template<typename T, size_t N>
    > size_t array_size(T (&dummy)[N]) { return N; }
    >
    > dummy is a reference to an array T of size N. Because the parameter is
    > unused C++ allows you to omit it as AngleWyrm did.


    Of course, this generates a function for every different type T and
    size N. It's more efficient to use

    #define array_size(X) (sizeof X / sizeof *X)

    which has the same semantics. You only lose some type-safety, but this
    typically not a problem.

    /david
     
    David Rubin, Jun 21, 2004
    #11
  12. Re: Determining array size

    * David Rubin:
    > "John Harrison" <> wrote in message
    >
    > [snip]
    > > template<typename T, size_t N>
    > > size_t array_size(T (&dummy)[N]) { return N; }
    > >
    > > dummy is a reference to an array T of size N. Because the parameter is
    > > unused C++ allows you to omit it as AngleWyrm did.

    >
    > Of course, this generates a function for every different type T and
    > size N.


    No; the compiler is free to inline the result and will typically do so.


    >It's more efficient to use
    >
    > #define array_size(X) (sizeof X / sizeof *X)
    >
    > which has the same semantics.


    No; the macro incorrectly accepts a pointer whereas the function
    correctly does not, the macro works on an array of local element type
    whereas the function does not, and the macro can be evaluated at compile
    time whereas a bit more template magic is needed to achieve that.


    > You only lose some type-safety, but this typically not a problem.


    No; those who think it's not a problem constitute a proper subset of
    those who by using such things create problems for others or themselves.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jun 21, 2004
    #12
  13. Re: Determining array size

    "David Rubin" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message
    >
    > [snip]
    > > template<typename T, size_t N>
    > > size_t array_size(T (&dummy)[N]) { return N; }
    > >
    > > dummy is a reference to an array T of size N. Because the parameter is
    > > unused C++ allows you to omit it as AngleWyrm did.

    >
    > Of course, this generates a function for every different type T and
    > size N. It's more efficient to use
    >
    > #define array_size(X) (sizeof X / sizeof *X)
    >
    > which has the same semantics. You only lose some type-safety, but this
    > typically not a problem.
    >
    > /david


    Not necessarily, if you make the template version an inline function.

    Your version suffers from the big problem that is will compile for pointers.
    Its quite common for an array to change to a pointer, for instance when some
    code is refactored into a different function but the array stays in the
    original function and is passed to the new function as a pointer. In that
    case your version will compile but give meaningless results. The template
    version will give a compile error.

    john
     
    John Harrison, Jun 21, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cam Acosta

    increase response buffer size

    Cam Acosta, Nov 10, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    9,924
    Cam Acosta
    Nov 10, 2003
  2. John
    Replies:
    0
    Views:
    793
  3. durumdara
    Replies:
    0
    Views:
    517
    durumdara
    Oct 16, 2006
  4. Fredrik Lundh
    Replies:
    0
    Views:
    419
    Fredrik Lundh
    Oct 16, 2006
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    262
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page