Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to know the buffer size and increase buffer size in c++

Reply
Thread Tools

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

 
 
David Rubin
Guest
Posts: n/a
 
      06-21-2004
"John Harrison" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-21-2004
* David Rubin:
> "John Harrison" <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-21-2004

"David Rubin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> "John Harrison" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: How to increase buffer size of a file ? Fredrik Lundh Python 0 10-16-2006 03:58 PM
How to increase buffer size of a file ? durumdara Python 0 10-16-2006 02:41 PM
How do I increase the size of the buffer in java.nio.Pipe? John Java 0 02-21-2006 03:21 PM
increase response buffer size Cam Acosta ASP .Net 2 11-10-2003 06:45 PM



Advertisments