Hi i have a doubt. if i allocate some memory like below in one application and send the pointer to another application, and if i want to know the size of the chunk how do i do it?? { char* ch=malloc(9); } i need to get the size of the allocated chunk via malloc printf("%d",sizeof(ch)); //wont work--will give 4 as its a pointer printf("%d",sizeof(*ch));//wont work--will give 1 as i am reffering to the first byte
This won't work for sending information to another application, because malloc allocates memory on your application's private heap, not in an area that is shared between applications. What you will need to do is look up shared memory or the equivalent for your operating system and maybe find out what other forms of interprocess communications are possible. If you don't know how much memory you allocated then perhaps it would be a good idea to extend the data structure to include enough information for the recipient to be able to work it out.
yea, i got your point but let me make my self a little more clear,if i have one application say(filebrowser) which uses an extension(basicly a virtual table in C) both will share the same heap space(or process space as the extension is always created in respect to the parent application filebrowser in this case)...now say this extension has a feature via which it can save a supplied buffer into any where in the filesystem with the desired filename. This buffer is basically created by the application via calloc/realloc ,and the pointer is supplied to the extension, now in the extension i want to know the size(number of bytes) of the memory chunk it is pointing to. i Hope i made myself a little more clear.
Ok, i will give you a little more insight into what i actually need..if i allocate some memory using malloc() i free it using free() ,now free gets the info regarding the amount of memory to be freed from the data stored just before the start of the pointer returned by malloc.. Like dlmalloc used in Linux we have [Prev_size][Size][Data] representing the chunk structure of the memory with the various information, the block [Size] actually holds the Size allocated for the current chunk...I was wondering if i can actually use the size data to get the size of the block allocated...Here is my problem,i am not able to do it..not getting the correct output.
Why do you want to get the size of the block allocated? Why not just store the value you gave to malloc?
Thats because, i have developed one such extension...which will be used by BlueTooth Application, MMS Application, EMail application,Music Player,Album and many many more, all these guys will just give me the (void*)buffer..and i have to do my Job. if i can calculate the size at runtime i have a way to optimise Entire Performance(sorry i can,t tell about the algorithm).
Since you're talking about optimisation, does that mean you have already implemented the desired functionality and are now trying to improve its performance, based on code profiling and on the discovery that knowledge of the buffer size would be helpful in opening the bottleneck?
OK, so what, in general terms without giving away the inner workings of your top secret algorithm, do you do with the buffer? You mentioned saving it to the filesystem; is that correct? Do you do other processing of that buffer?
Yup there are many things it will do. if file is to be stored in a local system we make a check to see if required space is there,all sorts of conflicts,write-permissions are there and then asynchronously we start saving the file/file(s)/folder/folder(s)... if the file has to be stored in another device i check for connectivity(via another extension) and do the above things again.. and yes in both cases its strictly asynchronous, and if at any point i get a Low_Ram,Low_Battery notification, it will do a Roll Back of the operation.
> if file is to be stored in a local system we make a check to see if required space is there OK, so how do you currently do that without knowing the size of the buffer? How do you know how much space to check for?
Yes, sorry for the late reply...we start the saving process, if space is finished, a notification is fired..we we dont actually make a check before the process..
OK, let me put it another way. If there is enough disk space, how do you know when to stop writing? How do you know when you've reached the end of the buffer?
Yes..here let me tell you we have implemented our Own malloc which actually returns a pointer to a structure one of which has the size info
What OS is it currently working on, and what is the new OS? Why can't you implement your own malloc on the new OS, as you have done with the existing OS?
<<Why can't you implement your own malloc on the new OS, as you have done with the existing OS? >> Already Done that, i was trying to see if i can have a work around to find the size info...
It's advisable not to, because if the RTL implementation of malloc changes then your application will start behaving very strangely. Also if the main application passes you a pointer that has not been allocated with malloc (maybe "new", or if the buffer is created on the stack, or via other memory management performed by the application) then the assumption that the size info is at &(ptr-N) will be invalid.