Link to home
Start Free TrialLog in
Avatar of ytgprasad
ytgprasad

asked on

is it possible?

is it possible to know how much memory  allocated to a pointer?
Avatar of rbr
rbr

Not general. You can't say how a OS allocates memory. Very system specific. On some systems yes, some no. You would have to hack the alloc function.
Avatar of ytgprasad

ASKER

to rbr:
  you said on some systems it is possible to possible. Can you throw some light how to know, how much memory allocated?
please read last comment as:
to rbr:
  you said on some systems it is possible to know how much memory allocated. Can you throw some light how to know, how much memory allocated?
Check
http://dmalloc.com/texi/dmalloc_1.html
if you want to use debug alloc functions.
to rbr: I do not want to debug these functions, what I want is to know is , on the fly (in program execution) how much memory is allocated to a pointer
As I said. It depends on the enviroment. Sometimes the length is stored in front of the pointer sometimes a global table is used.
you mean to say, it is possible to know the memory size allocated by debugging the alloc functions
Yes debugging the alloc functions will be a good starting point.
do you also mean to say in the course of program execution I will be able to know the memory allocated to a pointer
Hi,
  I'm i little bit confuse here...
Anyway, you know right away of how much memory allocated to a pointer since you are the one who asked the "memory manager" to allocate memory space for that pointer to point to..example:

int *somePointer;


somePointer = (int*)malloc(sizeof(int));

so the size is "sizeof(int)" if in you system int is 4 bytes then the size would be.. 4 :).

OK.. some other means to know the size is by writing you own code that check the size of allocated space that is stored somewhere above the start address of allocated space during malloc operation(but then u you have to understand how malloc implement this!). This method is used by free() to know how much memory should be deallocated.
FYI, that is why when u try to free the object that is not allocated by malloc(ie array) then free may take anything that reside above the address and try to deallocate that much!!.

Best regards,

Nisa.



to nisa:

what about the cases for a pointer returned by a funciton whose code you do not know.
I mean how do I know, how much memeory allocted to a pointer  returned by a function whose code I am not aware off. In other words how do I know how much memory the pointer is  currently holding.
Why that far, take for example the library functions which return pointers, how do I know how much memory they are holding?
I think now I am clear
In this case it will be impossible to see how much memory is behind the pointer. It is only possible of memory is allocated by alloc functions.
to rbr:
I do not really feel it is impossible, since the free or delete free the memory allocated to them. it means Those functions know how much they have to free.
Hi,
 Now is clearer ...

My previous suggestion is rather complicated but it is possible. However to find the size of the memory block pointed to by a pointer is relatively easy (I think...:)) .. take this as an example:

//function in whatever dll

void myFunction(someType *somePtr)
{
  //unknown code.
}

from the function prototype u know that the pointer point to some particular type ie someType .. so the size is "sizeof(someType)". Unless dll buddies wanna be really tricky by passing pointer "casted" from some other type of pointer.. such as

 someOtherPtr = (someOtherType*)sizeof(someOtherType);
 somePtr = (someType*)someOtherPtr;
 
If they are not "documented" then the only way to know the size is by implementing my previous comment.

another issue is if a return type is "void*" (as always are) then it is your responsibility to cast to a particular pointer that is relevant to your program. If it is an unknown function(ie undocumented)and you have know idea what this void* is all about and no size return by that function, then there are two ways:

 1. Implement my previous suggestion(from previous comment).
    -OR-
 2. give a phone call to "unknown function writer" :)...



Best Regards,

Nisa.

To ytgprasad:
Free can only free memory which is allocated by any alloc function. Free can't free memory for example of an array


char array[100];

char *parray=array;

free (array);

will not work.

So alloc and free uses an internal mechanism to know how much memory is used. But this mechanism depends on the enviroment.

To nisa:

You will know the size of one element but you don't know if an array is allocated in your example.
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And more tricky.

char *unknow_function3 (void)
{
char *parray;
parray=calloc (sizeof(char),101);
return (parray+1);

}

in this case also free (unknow_function3());
will give an error.
There should be some extra points for this tricky function.
It is possible. u can very well use the sizeof operator to find the size of the pointer.

The memory is allocated to the pointer on the basis of the memory model u choose to compile the program. Also u can specify before the pointer selection as far ptr or near ptr...

near ptr gets 2 bytes allocated

far ptr gets 4 bytes allocated

Adjusted points from 5 to 10
to rbr:

you said

>char *unknow_function2 (void)
>{
>     char *parray;
>     parray=calloc (sizeof(char),100);
>     return (parray);
>}

>Both functions returns a pointer and
>you can't see outside the function how
>the pointer has been created. In the
>first case it would be impossible to
>see how much memory is used in the
>second it could be possible if you
>hack the alloc functions.

can you tell me how you can hack alloc functions. Though you have provided me some time back some info. I am not able to get any idea in hacking. Could you please help me out in that.
Hi,
  rbr has got a point ...:)
  And hacking to alloc worth more than 10 points! Different system may have implemented it differently.

Regards.

Nisa.
Hi,
  rbr has got a point ...:)
  And hacking to alloc worth more than 10 points! Different system may have implemented it differently.

Regards.

Nisa.
I think a pratical solution would be
to allocate immidetly after
the allocation a dummy allocation.

The allocation size might be the GAP between them (IF the allocation is sequantial...)

a=malloc....
b=malloc(char)

gap=&b-&a;

/////
And I realy think you should give
rbr some more points.... :-)
/////

Yair
To yairy. I don't think that there will be one system where your code will work. Nobody can guarentee that 2 sequental alloc will allocate sequential memory. And you don't know if and how the alloc functions allocated memory for itself.

To ytgprasad: I won't hack teh alloc functions for you for 2 reasons.

1.) I don't know anything about your system.
2.) I gave you a link to debug alloc functions, where you can easily check the allocations.

And as nisa said 10 points would be a little bit few points for this task.
But post this in a seperate question since this have very little to do with this one.
to rbr:
 I am closing the discussion here. As you requested posting a new question worth 100 points with title "hack alloc fns to know the size of pointers".
answer for this question is very simple for answer goto
 
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=10318080 

Hi,

   But I don't think it handles all the cases. You might want to handle all possible cases since you are dealing with unknown functions, aren't you?.


Regards,

Nisa
to nisa:
you are correct.
what cases do you think this will not be handled. Can you put your comments in that question