[LCP]A question regarding malloc and how to check the size of an alloceted memory.

Kent Nyberg blurkis at bonbon.net
Tue Feb 25 00:07:01 UTC 2003


lör 2003-02-22 klockan 22.16 skrev Greg Black:
> 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 no hopes at all that my book about C is the best in the world, i
bought it second hand for about 1 dollar :)

> | 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.
> 
I know this.

> | 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?
> 

Well, i had not much to do and was playing around.
Since it did not work as a expected, i wanted 
to know how it should be done.
It seems strange that i cant check how big an allocated memory is. But
hey, i can do without doing it the way i was trying to. So thanks
anyway.


> Greg
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List
-- 
Kent Nyberg <blurkis at bonbon.net>




More information about the linuxCprogramming mailing list