[LCP]A question regarding malloc and how to check the size of an alloceted memory.
Greg Black
gjb at gbch.net
Sun Feb 23 05:17:01 UTC 2003
Kent Nyberg wrote:
| This is probably a "you should already know this, now go and read a book
| stupid!"-question, but i will give it a shot anyway since my book rely
| do not give me the answer i need.
I doubt if even the worst book is that bad, but if your book
really doesn't tell you the answer you should throw it out. The
answer is (obviously) in the standard and it will also be in the
manual for your system.
| I have allocated some memory with malloc.
| [...]
| Now, i want to check the size of "memory".
You do this by checking the return value from malloc(). If it's
NULL, then you got nothing; if it's a valid pointer, then you
got what you asked for.
| Doing an "sizeof(memory)" says it is 4 bytes.
You need to learn C. The sizeof operator always returns the
size of what it's given -- in this case, it's a pointer.
| Can some one tell me why what the normal way to check the allocated size
| is? And yes, malloc does not return NULL.
See above.
Now a question for you: why do you think you need to do this?
Greg
More information about the linuxCprogramming
mailing list