Hi! I would like to ask if there is a function (or some way) to return the memory allocated for a pointer. Example: char *s = new char[69]; cout << DesiredFunction(s); //I would like this to print 69 Thank you in advance.
If he knew the size would be constant, then yes, that would work. But I'm assuming that at the time of the printf, he doesn't know the size.
If you're dealing with the size of a particular kind of object (say a blob), why not create a class that encapsulates the memory along with its size). myBlob.GetData() and myBlob.GetSize() If the object type is to be arbitrary, then create a C++ template around it that implements these two methods.
If you are willing to change allocation paradigms you could use a vector class or something similar which will keep track of the current size of your collection.
I am not 100% sure I understand your question are you asking how you can return the memory to the the system If so then you shoud use the operator delete for this. As you allocated an array of chars you should use the array form of delete. For example:
I would like to ask if there is a function (or some way) to return the memory allocated for a pointer. Example: char *s = new char[69]; cout << DesiredFunction(s); //I would like this to print 69
There is no safe way to do this with a pointer allocated with new. The _msize function will return the size, in bytes, of a block of memory allocated on the heap by a call to calloc, malloc or realloc. While the new operator eventually calls malloc, this is neither documented nor supported.
"But I'm assuming that at the time of the printf, he doesn't know the size." <- Correct (except if declare a second #define in the file printf is, which i dont want)!! " why not create a class that encapsulates the memory along with its size" <- I just did that. Seems to be a better way of doing that. A bit complicated though for a simple thing like this, but anyway thx a lot for helping!
Returning the memory allocated for a pointer
mamo
If he knew the size would be constant, then yes, that would work. But I'm assuming that at the time of the printf, he doesn't know the size.
If you're dealing with the size of a particular kind of object (say a blob), why not create a class that encapsulates the memory along with its size). myBlob.GetData() and myBlob.GetSize() If the object type is to be arbitrary, then create a C++ template around it that implements these two methods.
Brian
Alsin
Walter Poupore - MSFT
printf ("%d",_msize(s));
Seems to be working... Thanks a lot.
Beshr Kayali
I am not 100% sure I understand your question are you asking how you can return the memory to the the system If so then you shoud use the operator delete for this. As you allocated an array of chars you should use the array form of delete. For example:
delete [] s;
ahmedilyas
Beckwith
then why not do
char s[SIZE];
printf("%u",SIZE);
and avoid unsupported function calls
Ananda Ganesh
" why not create a class that encapsulates the memory along with its size" <- I just did that. Seems to be a better way of doing that. A bit complicated though for a simple thing like this, but anyway thx a lot for helping!