size of a buffer

Started by
23 comments, last by mortex 21 years, 10 months ago
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]
Advertisement
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.
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 -


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.
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.
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);

-- Gilad

gilad: that won''t work.

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?
quote:Original post by gilady
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]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
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);  
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
It would be nice if there was some mention made of the language being used.

This topic is closed to new replies.

Advertisement