#### Archived

This topic is now archived and is closed to further replies.

# size of a buffer

This topic is 5645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey Is it possible to get the size of the memory allocated to a buffer? so that I can have, void foo(char*) instead of void foo(char*,size_t) I know that the buffer is always a char. Any ideas? Thanks! [edited by - mortex on June 26, 2002 3:24:12 AM]

##### Share on other sites
Well, if any character can be in the buffer I''m afraid I can''t help you.

However, say, you don''t use char 0x00 then you can use this as a terminator.

##### Share on other sites
I am not sure if I am misunderstanding your question or not so I''ll just offer a few answers:

1. If your allocated pointer is always a char, and only a single char then you have the size of the data as the size of a char type - thus the amount of memory.

2. If you allocate it (new, malloc, etc... )you need to inform the allocation routine what size the array is going to be. This ends up being the size of the datatype in bytes * the size you need. In this case, you know the size of the array and thus the size of the memory.

If this is not what you are looking for then perhaps describing your problem a little further will help readers to understand what you are trying to do, and give you additional assistance.

Good Luck -

##### Share on other sites

The buffer is binary, and could contain anything.
I recall someone once told me that it could be done with the win32-api or something, not sure, though.

##### Share on other sites
It could be of 0->n in size.

I can''t do a while(*buffer++) or something ofcourse, since the buffer could contain hundreds of \0''s.

I''m not even sure if it''s possible, but it would be cool to know.

##### Share on other sites
There is a simple way to know the size of a buffer but there is a condition:
The buffer has to be located with new
X* buffer = new X[1000];

then you can get the size of the buffer by:
int size = sizeof(buffer)/sizeof(X);

##### Share on other sites

I''ll show some code to illustrate what I want:

void foo(char *buf){  // I need the size of buf here}void main(){  char *bar = new char[1000];  foo(bar);  delete [] bar;}

Is it possible?

##### Share on other sites
quote:
There is a simple way to know the size of a buffer but there is a condition:
The buffer has to be located with new
X* buffer = new X[1000];

then you can get the size of the buffer by:
int size = sizeof(buffer)/sizeof(X);
Nonono! Don't do this, this won't work at all. size will always be zero in this case unlesssizeof(X) is smaller than sizeof(X*) (usually sizeof(X*) is 4 in 32-bit environments).

EDIT1: Removed the ugly...

[edited by - dalleboy on June 26, 2002 5:21:57 AM]

##### Share on other sites
If you use malloc you could use the _msize (non-ANSI) function to get the size of the allocated memory.
  #include <malloc.h>long* buffer = (long*) malloc(1000 * sizeof(long));size_t size = _msize(buffer);

##### Share on other sites
It would be nice if there was some mention made of the language being used.

##### Share on other sites
sabreman: since I''m using ''new'' it''s quite clear that I''m using C++.

I hope to find a solution that works with new/delete, not malloc/free.

##### Share on other sites
quote:
Original post by SabreMan
It would be nice if there was some mention made of the language being used.
The symantics look like c/c++.

##### Share on other sites
quote:
Original post by mortex
I hope to find a solution that works with new/delete, not malloc/free.
In that case the answer is: No, there are no general way of doing it for new/delete. But you could take a look at the Win32 functions GlobalSize, LocalSize and HeapSize.

##### Share on other sites

Hm, actually _msize() worked with new aswell, who would have thought
(I'm using mingw at the moment, have to check with vc7 when I'm home)

dalleboy: thanks

[edited by - mortex on June 26, 2002 5:50:28 AM]

##### Share on other sites
in binary files all regular fileformats do have a header and the data. in the header they store the size of the data.
there is no difference between files and memorychunks. if you really need to know the size of your array store it at the beginning.
or use stl.

"take a look around" - limp bizkit

##### Share on other sites
quote:
Original post by mortex
sabreman: since I'm using 'new' it's quite clear that I'm using C++.
I hope to find a solution that works with new/delete, not malloc/free.

Ahh.... since you are using C++, dump your char* for a vector, which carries its own size information.

    #include <vector>using namespace std;void foo(vector<char>& vec){  size_t size = buf.size();  char* buf = &vec[0]; // if you want a char*  char c25 = vec[25]; // can use operator[] too;  vec[10] = 'a';}void main(){  vector<char> bar(1000); // the size can be a variable  foo(bar);  // no need for a delete unless you've new-ed the vector;}

If you just want fixed-size arrays go look at boost::array (see .sig)

Or, if you're always passing C arrays and not pointers :

  template<size_t size> void foo( char buf[size] ){  // can use buf and size}int main(){    char array[1000];    char* ptr_1 = new char[100];    char* ptr_2 = array;    foo( array ); // works, size = 1000.    foo( ptr_1 ); // doesn't compile, ptr_1 is not of type char[].    foo( ptr_2 ); // doesn't compile, ptr_2 is not of type char[] either.};

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

[edited by - Fruny on June 26, 2002 6:00:54 AM]

##### Share on other sites
quote:
Original post by mortex Hm, actually _msize() worked with new aswell, who would have thought

##### Share on other sites
quote:
Original post by mortex
sabreman: since I''m using ''new'' it''s quite clear that I''m using C++.

You didn''t mention ''new'' in your original post, but never mind...
quote:

I hope to find a solution that works with new/delete, not malloc/free.

The idiomatic modern C++ way of solving your problem would be to use std::vector instead of an array. vector actually behaves more like you might reasonably expect an array to, and includes a size() method which does what you are asking for. For further details, see Marshall Cline''s comments on why Arrays are evil.

quote:
Original post by dalleboy
The symantics look like c/c++.

I gathered that, but that''s two different languages.

##### Share on other sites

e.g.
char *dat=new char[100]; // dat=0xC0001004unsigned int size=*(unsigned int*)(dat-sizeof(unsigned int));

don''t ask me how i know this, i''m probably wrong, but the idea just sounds familiar to me for some reason, and i remember getting lots of 0xXXXXXXX4 addresses returned by malloc...

ok, cancel that, i just tried the theory in VC6 and it died like the mad dog it was...

...but im _sure_ that used to work!!!...oh well....

##### Share on other sites
Hi,
Well of course the command is _msize and not sizeof. silly me, was thinking of something else when I wrote the answer. And yes c++ compiler usually (the ones I know of) write the size of the buffer they allocated in the 4 bytes before the buffer. that data is used for free and other memory functions

##### Share on other sites
quote:
Well of course the command is _msize and not sizeof.

There is no such function in Standard C or C++.
quote:

And yes c++ compiler usually (the ones I know of) write the size of the buffer they allocated in the 4 bytes before the buffer.

The Standard does not mandate that, so it is unwise to rely on it when there is a satisfactory alternative. And there is a satisfactory alternative.

##### Share on other sites
just use a friggin std::vector

cripes

##### Share on other sites
Just a suggestion... maybe BSTR would help you out. Bstrings have 2 (or is 4???) bytes before your pointer that tells how long the buffer is. There are functions for allocation, deletion, setting the buffer size, etc... Might be worth a look.

##### Share on other sites
Vorlath you''ve got to be kidding.

Option 1: Use a well documented, standard data structure (std::vector)

Option 2: Use some obscure Win32 datatype (BSTR which is actually an OLECHAR FAR* which is god knows what, I couldn''t find any typedefs for those)

This is a tough one...

SabreMan your sig should be "SabreMan... fighting C/C++ one line of code at a time," or maybe "SabreMan... USE THE GODDAMN STL"

##### Share on other sites
Hahaha. Yeah, you''re right. Unless you''re doing COM, STAY AWAY. Sorry, mental lapse on that one.

• ### Forum Statistics

• Total Topics
628634
• Total Posts
2983964

• 10
• 18
• 20
• 13
• 9