Solved

is it possible?

Posted on 2000-03-20
29
249 Views
Last Modified: 2010-04-15
is it possible to know how much memory  allocated to a pointer?
0
Comment
Question by:ytgprasad
  • 12
  • 10
  • 5
  • +2
29 Comments
 
LVL 10

Expert Comment

by:rbr
Comment Utility
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.
0
 

Author Comment

by:ytgprasad
Comment Utility
to rbr:
  you said on some systems it is possible to possible. Can you throw some light how to know, how much memory allocated?
0
 

Author Comment

by:ytgprasad
Comment Utility
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?
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
Check
http://dmalloc.com/texi/dmalloc_1.html
if you want to use debug alloc functions.
0
 

Author Comment

by:ytgprasad
Comment Utility
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
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
As I said. It depends on the enviroment. Sometimes the length is stored in front of the pointer sometimes a global table is used.
0
 

Author Comment

by:ytgprasad
Comment Utility
you mean to say, it is possible to know the memory size allocated by debugging the alloc functions
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
Yes debugging the alloc functions will be a good starting point.
0
 

Author Comment

by:ytgprasad
Comment Utility
do you also mean to say in the course of program execution I will be able to know the memory allocated to a pointer
0
 

Expert Comment

by:nisa
Comment Utility
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.



0
 

Author Comment

by:ytgprasad
Comment Utility
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
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
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.
0
 

Author Comment

by:ytgprasad
Comment Utility
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.
0
 

Expert Comment

by:nisa
Comment Utility
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.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:rbr
Comment Utility
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.
0
 
LVL 10

Accepted Solution

by:
rbr earned 10 total points
Comment Utility
So
 2 functions


char *unknow_function1 (void)
{
     static char array [100];
     return (array);
}

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.
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
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.
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
There should be some extra points for this tricky function.
0
 

Expert Comment

by:balakrishnan_t
Comment Utility
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

0
 

Author Comment

by:ytgprasad
Comment Utility
Adjusted points from 5 to 10
0
 

Author Comment

by:ytgprasad
Comment Utility
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.
0
 

Expert Comment

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

Regards.

Nisa.
0
 

Expert Comment

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

Regards.

Nisa.
0
 
LVL 2

Expert Comment

by:yairy
Comment Utility
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
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
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.
0
 

Author Comment

by:ytgprasad
Comment Utility
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".
0
 

Author Comment

by:ytgprasad
Comment Utility
answer for this question is very simple for answer goto
 
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=10318080

0
 

Expert Comment

by:nisa
Comment Utility
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
0
 

Author Comment

by:ytgprasad
Comment Utility
to nisa:
you are correct.
what cases do you think this will not be handled. Can you put your comments in that question
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now