Array length

  • Thread starter Jamil Anwar Zaman
  • Start date
J

Jamil Anwar Zaman

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.
 
P

Pieter Droogendijk

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.
 
S

Serve La

Jamil Anwar Zaman said:
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.
 
L

Lowell Gilbert

Jamil Anwar Zaman said:
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.
 
M

Mike Wahler

Jamil Anwar Zaman said:
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
 
J

Jamil Anwar Zaman

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 ?
 
T

those who know me have no need of my name

in comp.lang.c i read:
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.
 
E

Eric Sosman

Jamil said:
[...]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top