If I dynamically allocate memory to a pointer, then is it possible to see
afterwards what is the memory size the pointer is looking at.
e.g
int SIZE = 10;
int *a = malloc(SIZE*sizeof(int));
printf("Size : %d", sizeof(a));
this actually gives me 4 bytes, which essentially is the sizeof the
pointer. But I want to know the actual memory size. Any idea how can I do
that.
--
Jamil Anwar Zaman
|
|
0
|
|
|
|
Reply
|
shantu (2)
|
8/9/2003 4:43:49 PM |
|
On Sun, 10 Aug 2003 02:43:49 +1000
Jamil Anwar Zaman <shantu@spin.net.au> wrote:
> If I dynamically allocate memory to a pointer, then is it possible to see
> afterwards what is the memory size the pointer is looking at.
> e.g
>
> int SIZE = 10;
> int *a = malloc(SIZE*sizeof(int));
> printf("Size : %d", sizeof(a));
>
> this actually gives me 4 bytes, which essentially is the sizeof the
> pointer. But I want to know the actual memory size. Any idea how can I do
> that.
Yes: remember the size of the memory you allocate.
--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
|
|
0
|
|
|
|
Reply
|
gin (83)
|
8/9/2003 5:00:27 PM
|
|
"Jamil Anwar Zaman" <shantu@spin.net.au> wrote in message
news:oprtncrb1jfai5vg@news.spin.net.au...
> If I dynamically allocate memory to a pointer, then is it possible to see
> afterwards what is the memory size the pointer is looking at.
> e.g
>
> int SIZE = 10;
> int *a = malloc(SIZE*sizeof(int));
> printf("Size : %d", sizeof(a));
>
> this actually gives me 4 bytes, which essentially is the sizeof the
> pointer. But I want to know the actual memory size. Any idea how can I do
> that.
This is not possible in standard C. If you want the size you should store it
yourself.
One implementation I've seen has a function _msize, but this is of course
not portable and it's slow for some reason.
|
|
0
|
|
|
|
Reply
|
ik4795 (24)
|
8/9/2003 5:05:40 PM
|
|
Jamil Anwar Zaman <shantu@spin.net.au> writes:
> If I dynamically allocate memory to a pointer, then is it possible to
> see afterwards what is the memory size the pointer is looking at.
No; you need to track it yourself if you need it.
> e.g
>
> int SIZE = 10;
> int *a = malloc(SIZE*sizeof(int));
> printf("Size : %d", sizeof(a));
>
> this actually gives me 4 bytes, which essentially is the sizeof the
> pointer.
Not "essentially." Precisely.
> But I want to know the actual memory size. Any idea how can I
> do that.
The normal method is to carry the size along; I generally use a
structure that holds a size and either a pointer or an array.
|
|
0
|
|
|
|
Reply
|
lowell1 (3)
|
8/9/2003 5:07:51 PM
|
|
Jamil Anwar Zaman <shantu@spin.net.au> wrote in message
news:oprtncrb1jfai5vg@news.spin.net.au...
> If I dynamically allocate memory to a pointer, then is it possible to see
> afterwards what is the memory size the pointer is looking at.
> e.g
>
> int SIZE = 10;
> int *a = malloc(SIZE*sizeof(int));
> printf("Size : %d", sizeof(a));
>
> this actually gives me 4 bytes, which essentially is the sizeof the
> pointer. But I want to know the actual memory size. Any idea how can I do
> that.
Yes. The size is SIZE*sizeof(int). Why ask what you
already know?
-Mike
|
|
0
|
|
|
|
Reply
|
mkwahler (3821)
|
8/10/2003 6:30:25 AM
|
|
On Sat, 9 Aug 2003 23:30:25 -0700, Mike Wahler <mkwahler@mkwahler.net>
wrote:
>
> Jamil Anwar Zaman <shantu@spin.net.au> wrote in message
> news:oprtncrb1jfai5vg@news.spin.net.au...
>> If I dynamically allocate memory to a pointer, then is it possible to
>> see
>> afterwards what is the memory size the pointer is looking at.
>> e.g
>>
>> int SIZE = 10;
>> int *a = malloc(SIZE*sizeof(int));
>> printf("Size : %d", sizeof(a));
>>
>> this actually gives me 4 bytes, which essentially is the sizeof the
>> pointer. But I want to know the actual memory size. Any idea how can I
>> do
>> that.
>
> Yes. The size is SIZE*sizeof(int). Why ask what you
> already know?
>
> -Mike
>
I was trying not to pass the array size when I pass array as a parameter to
soem other function. I'm wondering how java actually does that ? Direct
memory manipulation ?
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
|
|
0
|
|
|
|
Reply
|
shantu (2)
|
9/3/2003 3:53:38 PM
|
|
in comp.lang.c i read:
>On Sat, 9 Aug 2003 23:30:25 -0700, Mike Wahler <mkwahler@mkwahler.net>
>wrote:
>> Yes. The size is SIZE*sizeof(int). Why ask what you
>> already know?
>I was trying not to pass the array size when I pass array as a
>parameter to soem other function. I'm wondering how java actually does
>that ? Direct memory manipulation ?
it passes a reference, which contains (amongst other things) the size. c
is not java, don't try to apply any of the rules you learned about java to
c or you will just frustrate yourself.
--
a signature
|
|
0
|
|
|
|
Reply
|
not-a-real-address (328)
|
9/3/2003 4:45:15 PM
|
|
Jamil Anwar Zaman wrote:
>
> > Jamil Anwar Zaman <shantu@spin.net.au> wrote in message
> > news:oprtncrb1jfai5vg@news.spin.net.au...
> >> If I dynamically allocate memory to a pointer, then is it possible to
> >> see
> >> afterwards what is the memory size the pointer is looking at.
> [...]
>
> I was trying not to pass the array size when I pass array as a parameter to
> soem other function. I'm wondering how java actually does that ? Direct
> memory manipulation ?
<off-topic>
An array in Java is a full-fledged Object. In addition
to the "contents" portion, the Java array Object also has a
`length' member that gives the number of array elements.
In short, Java passes the array size along with the array;
they're just bundled together conveniently.
</off-topic>
A way to imitate Java's "packaging" of the array contents
with the array size is to use a struct, e.g.
struct array_of_int {
size_t element_count;
int *elements;
};
It's still not quite as convenient as Java's formulation
because you've got to maintain both the count and the pointer,
and because the struct's presence is so obtrusive. Still,
this sort of "descriptor" style can be useful.
--
Eric.Sosman@sun.com
|
|
0
|
|
|
|
Reply
|
Eric.Sosman (4228)
|
9/3/2003 4:47:56 PM
|
|
|
7 Replies
26 Views
(page loaded in 0.609 seconds)
|