No there aren't. Every system is free to organize and keep track of allocated blocks
of memory and none of that is supposed to be exposed to the programmer, nor
defined in the Standards for those languages.
kind regards,
Jos
I will xplain my understanding.
1. In one word the answer is "yes" on unix. That is you, as a application writer should be able to retrive the size of the memory allocated from the pointer. But there is more.
I will tell you how all this happens in linux, I have some intution on windows but not sure.
When you make a call to malloc, it allocates a block of memory from heap to your program. And it will keep some house keeping information ie the size of the block allocated to this pointer. Generally this information will be kept at a constant offset from the returned pointer.
Thus the answer is totally dependent on how your C/C++ runtime is implemented. If you know the offset you should be able to retrieve the information. But be very careful if you do anything fancy with the location then you will end up crashing.
Indepth, when a process is created OS (read kernel) will give it some memory as heap (how much is a result of negotiation between OS and the programmer)
so if you malloc a small amount of memory, then your process size neither grows nor shrinks. If you alloc large memory, then your C/C++ runtime will internally request the OS to increase the heap size. Programmer stays opaque to all this. You can also voluntary shrink your size but that generally does not happen. The system calls of importance on linux/unix is brk() and sbrk() calls.
Windows story (I am not pretty sure about it):
In windows malloc is just like a wrapper function that talks to OS (read kernel32.dll) every time it is called. So the house keeping is not kept by C/C++ runtime n windows but by the kernel32.dll. I have not got any idea if you be able to retrieve the information even if you knew how it is done on linux you might be restricted.
On other OS say some embedded OS.. It will be there own story. So you should not at all bank on it if you are writing portable code, rather you should keep your info in your structures.