R
Raja
How to know the buffer size and increase buffer size in c++.
Raja said:How to know the buffer size and increase buffer size in c++.
Raja said:How to know the buffer size
and increase buffer size in c++.
Raja said:How to know the buffer size and increase buffer size in c++.
Julie said: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.
Raja said:How to know the buffer size and increase buffer size in c++.
Julie said: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.
AngleWyrm said: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");
}
Mats Weber said:"AngleWyrm" <[email protected]> wrote in messageIn 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 ?
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.
[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 Rubin said:[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
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.