Solved

is it possible?

Posted on 2000-03-20
29
254 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
ID: 2639136
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
ID: 2639224
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
ID: 2639226
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 10

Expert Comment

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

Author Comment

by:ytgprasad
ID: 2639405
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
ID: 2639600
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
ID: 2639607
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
ID: 2639619
Yes debugging the alloc functions will be a good starting point.
0
 

Author Comment

by:ytgprasad
ID: 2644032
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
ID: 2652352
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
ID: 2652405
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
ID: 2652537
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
ID: 2652616
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
ID: 2652718
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
 
LVL 10

Expert Comment

by:rbr
ID: 2652774
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
ID: 2652783
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
ID: 2652795
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
ID: 2652825
There should be some extra points for this tricky function.
0
 

Expert Comment

by:balakrishnan_t
ID: 2652973
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
ID: 2652983
Adjusted points from 5 to 10
0
 

Author Comment

by:ytgprasad
ID: 2652998
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
ID: 2653218
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
ID: 2653222
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
ID: 2655760
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
ID: 2660085
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
ID: 2660354
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
ID: 2662671
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
ID: 2663026
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
ID: 2663167
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

831 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