Jump to content
  • Advertisement

Archived

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

mortex

size of a buffer

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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);

-- Gilad

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!