473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

How to know the buffer size and increase buffer size in c++.
Jul 22 '05 #1
12 7821

"Raja" <sh*******@yahoo.com> wrote in message
news:b2*************************@posting.google.co m...
How to know the buffer size and increase buffer size in c++.


Buffer size of what?

john
Jul 22 '05 #2

"Raja" <sh*******@yahoo.com> wrote in message
news:b2*************************@posting.google.co m...
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
Jul 22 '05 #3
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.
Jul 22 '05 #4
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
Jul 22 '05 #5
Raja wrote:
How to know the buffer size and increase buffer size in c++.


You could use std::vector which can resize itself.
See also realloc().
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #6
"Julie" <ju***@nospam.com> wrote in message
news:40**************@nospam.com...
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");
}
Jul 22 '05 #7
"AngleWyrm" <no***************@hotmail.com> 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 ?
Jul 22 '05 #8

"Mats Weber" <ma***@bluewin.ch> wrote in message
news:9c**************************@posting.google.c om...
"AngleWyrm" <no***************@hotmail.com> 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
Jul 22 '05 #9
>
dummy is a reference to an array T of size N.


an array of T of size N

john
Jul 22 '05 #10
"John Harrison" <jo*************@hotmail.com> 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
Jul 22 '05 #11
* David Rubin:
"John Harrison" <jo*************@hotmail.com> 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?
Jul 22 '05 #12

"David Rubin" <da********@warpmail.net> wrote in message
news:82*************************@posting.google.co m...
"John Harrison" <jo*************@hotmail.com> 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
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cam Acosta | last post by:
How I can increase the response buffer size in Internet Information Server 6 (windows 2003)? Thanks, Camilo Acosta
2
by: Kush | last post by:
Hi. I am kind of new to DB2 and to this newsgroup so please bear with me.. My question is: IBM installation creates 250 4k bufferpools by default. I want to increase this number to 1000, no...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
0
by: durumdara | last post by:
Hello ! How to increase buffer size of a file ? I want to use more buffer, but I don't want to replace every file object with my class. It have a contant in a module ? Thanks for your...
3
by: cmhada | last post by:
I have some code that continually reads packets off of a udp port where the incoming packets are 26 bytes in length. CODE: byte recBytes = udpClient.Receive(ref ipEndPoint); I have left the...
64
by: Philip Potter | last post by:
Hello clc, I have a buffer in a program which I write to. The buffer has write-only, unsigned-char-at-a-time access, and the amount of space required isn't known a priori. Therefore I want the...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
3
by: James Harris | last post by:
Do you remember a previous discussion on this newsgroup as at http://groups.google.com/group/comp.lang.c/browse_frm/thread/cb502ce8414cea06/bb738c4f91113b4e It concerned code to manage a...
2
by: Ron Hinds | last post by:
I'm getting this in an ASP application on IIS6/W2K3. The page in question is trying to return a XML file approximately 45MB in size. Changing this is not an option. Worked fine on IIS5/W2K. I tried...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.