c - How to get memory block length after malloc? -
I thought I could assign the length of the allocated memory block to If I change the size to 2000, then it produces a length up to 2017. However, when I change to 3000, it gives 300 9. I am using GCC. What you are doing is certainly wrong, though it is almost certain that allocated blocks Just before the word is related to the shape, it may contain information in some extra flags or unused bits. Depending on the implementation, this data may also be in high bits, causing you to completely read the wrong length. Apart from this, it is also possible that small allocations (such as 1 to 32 bytes) are packaged in special small-block pages, in which there are no headers, in this case the word before the allotted block is just another block There is no meaning in relation to the part and size of the blocks are examining. Just stop this misguided and dangerous pursuit if you need to know the size of the block from the .length function in Java though, now I know that when
allocates the block to malloc () , it allocates extra bytes to keep the block-shaped integer it is located at the beginning of the integer block; This address was actually transferred to the number of the collar, which is exactly past the value of this length. The problem is, I can not access that address to retrieve the block length
#include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; Int main (zero) {four * str; Str = (four *) maulok (size (four) * 1000); Int * length; Length = str-4; / * Because on 32 bit system, one int 4 bytes long * / printf ("str length:% d \ n", * length); Free (STR); } Edit: Lastly I did the problem, this is giving 0 on my system as a length instead of size because my Ubuntu is 64 bit. I have changed str-4 to str-8, and it works now.
malloc , then you are doing something wrong.
Comments
Post a Comment