1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Query regarding size

Discussion in 'C' started by micsom_micsom, Oct 20, 2009.

  1. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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.
     
  4. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Why do you want to get the size of the block allocated?
    Why not just store the value you gave to malloc?
     
  6. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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).
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  8. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    Bingo...Absolutely Correct..please help me here
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  10. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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.
     
  11. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    file here refers to the buffer, the extension will give a filename as supplied by the invoking App
     
  12. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    > 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?
     
  13. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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..
     
  14. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  15. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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
     
  16. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    But if we want it to work on other OS i need the size info
     
  17. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  18. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    <<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...
     
  19. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  20. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    so what do u suggest?
     

Share This Page