How to get the size of a dynamically allocated array?

L

linq936

Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?

I tried sizeof(arr)/sizeof(int), I get 1.

So this means that I must change the signature so that the caller of
this function must provide the size.

Thanks.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?

That is correct.
I tried sizeof(arr)/sizeof(int), I get 1.

sizeof(int *) / sizeof(int) == 1 on your system. It's not possible to have
an array function parameter. int func(int arr[]) declares func as a
function accepting a single argument of type pointer-to-int, exactly as
does int func(int *arr). When you then evaluate sizeof(arr), you get the
size of the pointer.
So this means that I must change the signature so that the caller of
this function must provide the size.

Either that, or you need a special value to behave as an "end of array"
marker, similar to the '\0' that terminates strings.
 
B

Ben Bacarisse

Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?
Yes.

So this means that I must change the signature so that the caller of
this function must provide the size.

That is the most flexible solution. You *might* be able to get away with:

(a) A globally known size -- this might just work if, say, you program
is modeling n-dimensional vectors and matrices. You might be able to
make "n" a compile-time constant (#define is the way to do this).

(b) Some kind of sentinel value you always place at the end of the
array so that functions know when to avert their eyes. Personally I
_hate_ such things, but they have their place (no pun intended).
 
M

Malcolm McLean

Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?

I tried sizeof(arr)/sizeof(int), I get 1.

So this means that I must change the signature so that the caller of
this function must provide the size.
Yes.

Some versions of C had a function called msize() which gave the size of a
block allocated with malloc(). Whilst not hard to implement it never made it
into the standard. The problem is that every funtion that uses it to
evaluate the size of its argument must be passed a block allocated with
malloc(), so msize() is a actually a nuisance.
 
B

BiGYaN

Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?

I tried sizeof(arr)/sizeof(int), I get 1.

So this means that I must change the signature so that the caller of
this function must provide the size.

Thanks.

Yup, you are quite right. Except for maybe using a global variable to
store the size.
 
K

Keith Thompson

BiGYaN said:
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?

I tried sizeof(arr)/sizeof(int), I get 1.

So this means that I must change the signature so that the caller of
this function must provide the size.

Thanks.

Yup, you are quite right. Except for maybe using a global variable to
store the size.

Yes, I suppose you could use a global variable, but why on Earth would
you want to? Providing information to functions is exactly what
parameters are for.
 
B

BiGYaN

BiGYaN said:
If the function signature is like this:
int func( int arr[] );
Or this:
int func ( int* arr );
Then inside the function I have no way to know the size of the
array, is that right?
I tried sizeof(arr)/sizeof(int), I get 1.
So this means that I must change the signature so that the caller of
this function must provide the size.
Thanks.
Yup, you are quite right. Except for maybe using a global variable to
store the size.

Yes, I suppose you could use a global variable, but why on Earth would
you want to? Providing information to functions is exactly what
parameters are for.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


@ Keith
I was just mentioning an alternative way. Of course it doesn't make
much sense and is quite silly. But one can never be sure about all the
cases. So I just mentioned it.
 
S

Szabolcs Nagy

@ Keith
I was just mentioning an alternative way. Of course it doesn't make
much sense and is quite silly. But one can never be sure about all the
cases. So I just mentioned it.

or one can use implementation dependent ways
eg if one knows the array is malloced, then some malloc
implementations store the allocated size somwhere before the pointed
object

okok i know this is the worst hack possible
 
F

Frodo Baggins

or one can use implementation dependent ways
eg if one knows the array is malloced, then some malloc
implementations store the allocated size somwhere before the pointed
object

okok i know this is the worst hack possible

I had tried this out once and it didn't work. Yes, the size is stored
before in the malloc'ed block header. That's why free() doesn't
require a size argument. The problem is that this info cannot be
accessed from user-space. Probably msize() uses this.

Regards,
Frodo B
 
K

Keith Thompson

Frodo Baggins said:
I had tried this out once and it didn't work. Yes, the size is stored
before in the malloc'ed block header. That's why free() doesn't
require a size argument. The problem is that this info cannot be
accessed from user-space. Probably msize() uses this.

The problem is that there is no standard for storing this information.
One implementation might store the size (which could be either the
requested size or the allocated size) just before the malloc'ed block;
another might store it in a centralized table. Code that make
assumptions about this is *extremely* non-portable.
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top