Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Query regarding size (http://www.go4expert.com/forums/query-regarding-size-t19828/)

micsom_micsom 20Oct2009 17:20

Query regarding size
 
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

xpi0t0s 21Oct2009 03:32

Re: Query regarding size
 
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.

micsom_micsom 21Oct2009 11:42

Re: Query regarding size
 
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.

micsom_micsom 21Oct2009 12:55

Re: Query regarding size
 
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.

xpi0t0s 21Oct2009 13:21

Re: Query regarding size
 
Why do you want to get the size of the block allocated?
Why not just store the value you gave to malloc?

micsom_micsom 21Oct2009 13:52

Re: Query regarding size
 
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).

xpi0t0s 21Oct2009 14:23

Re: Query regarding size
 
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?

micsom_micsom 21Oct2009 14:25

Re: Query regarding size
 
Bingo...Absolutely Correct..please help me here

xpi0t0s 21Oct2009 14:28

Re: Query regarding size
 
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?

micsom_micsom 21Oct2009 14:35

Re: Query regarding size
 
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.


All times are GMT +5.5. The time now is 01:33.