Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to get the size of a dynamically allocated array?

Reply
Thread Tools

How to get the size of a dynamically allocated array?

 
 
linq936@hotmail.com
Guest
Posts: n/a
 
      06-08-2007
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.

 
Reply With Quote
 
 
 
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      06-08-2007
wrote:
> 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.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      06-08-2007
In article < .com>,
<> wrote:

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


Quite correct. If you must know the size inside the routine,
you must pass the size in, or there must be some way from the data
itself to identify the size.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-08-2007
writes:

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

--
Ben.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      06-09-2007

<> wrote in message
news: oups.com...
> 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.


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
BiGYaN
Guest
Posts: n/a
 
      06-09-2007
On Jun 8, 9:30 pm, linq...@hotmail.com wrote:
> 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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-09-2007
BiGYaN <> writes:
> On Jun 8, 9:30 pm, linq...@hotmail.com wrote:
>> 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) kst- <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"
 
Reply With Quote
 
BiGYaN
Guest
Posts: n/a
 
      06-10-2007
On Jun 10, 1:39 am, Keith Thompson <k...@mib.org> wrote:
> BiGYaN <bigyan.tec...@gmail.com> writes:
> > On Jun 8, 9:30 pm, linq...@hotmail.com wrote:
> >> 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) k...@mib.org <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.

 
Reply With Quote
 
Szabolcs Nagy
Guest
Posts: n/a
 
      06-10-2007

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

> >

>
> @ 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

 
Reply With Quote
 
Frodo Baggins
Guest
Posts: n/a
 
      06-10-2007
On Jun 10, 6:24 am, Szabolcs Nagy <nszabo...@gmail.com> wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Get the size of allocated objects Davide Java 10 01-10-2006 05:26 PM
How to find the size of dynamically allocated struct-type variable? nmtoan@gmail.com C Programming 5 10-21-2005 06:26 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Checking size for Dynamically Allocated Memory kapilk C Programming 1 08-16-2004 11:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57