Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array size using pointer

Reply
Thread Tools

Array size using pointer

 
 
dati_remo@libero.it
Guest
Posts: n/a
 
      10-11-2004
Hi, is it possible to find the dimension of an array using a pointer?

main()
{
int a[10];

f(a);

return;
}


f(int *b)
{
/*how can I know here the size of b??*/
/*because sizeof(b) is always 4*/
/*and sizeof(b[0]) is always 4*/

......
}

Thank you

Dati
 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      10-11-2004
wrote:
> Hi, is it possible to find the dimension of an array using a pointer?


No.

>
> main()

int main() /* implicit `int' was eliminated in C99 */
> {
> int a[10];
>
> f(a);

f(a, sizeof a / sizeof *a);
>
> return;
> }
>
>
> f(int *b)

void f(int *b, size_t n)
> {
> /*how can I know here the size of b??*/


By passing it as an argument.

> /*because sizeof(b) is always 4*/
> /*and sizeof(b[0]) is always 4*/
>
> .....
> }
>

HTH,
--ag

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
 
Reply With Quote
 
 
 
 
Karthik Kumar
Guest
Posts: n/a
 
      10-11-2004
wrote:
> Hi, is it possible to find the dimension of an array using a pointer?
>
> main()
> {
> int a[10];
>
> f(a);
>
> return;
> }
>
>
> f(int *b)
> {
> /*how can I know here the size of b??*/
> /*because sizeof(b) is always 4*/
> /*and sizeof(b[0]) is always 4*/


Both ought to be the same, since both are pointers.
And you are trying to get the size of a pointer variable
(which is implementation - dependent).

As Artie had already suggested, pass the length
explicitly as an argument to the function.


--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
 
Reply With Quote
 
dati_remo@libero.it
Guest
Posts: n/a
 
      10-12-2004
Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
> wrote:
> > Hi, is it possible to find the dimension of an array using a pointer?
> >
> > main()
> > {
> > int a[10];
> >
> > f(a);
> >
> > return;
> > }
> >
> >
> > f(int *b)
> > {
> > /*how can I know here the size of b??*/
> > /*because sizeof(b) is always 4*/
> > /*and sizeof(b[0]) is always 4*/

>
> Both ought to be the same, since both are pointers.
> And you are trying to get the size of a pointer variable
> (which is implementation - dependent).
>
> As Artie had already suggested, pass the length
> explicitly as an argument to the function.


I know, but I can't do it.

Do you thing it is possible to find in the stack this information?

I mean, I have the position of the first element of the array, then,
moving up and down in the stack, I find this number. Or is the stack a
simple storage for the program and only it knows this information.

And then how does really operate sizeof? Has it an internal table with
all variables?

Thank you

Best Regards

Dati Remo
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-12-2004
On 12 Oct 2004 00:37:26 -0700
() wrote:

> Karthik Kumar <> wrote in message
> news:<416adc48$1@darkstar>...
> > wrote:
> > > Hi, is it possible to find the dimension of an array using a
> > > pointer?
> > >
> > > main()
> > > {
> > > int a[10];
> > >
> > > f(a);
> > >
> > > return;
> > > }
> > >
> > >
> > > f(int *b)
> > > {
> > > /*how can I know here the size of b??*/
> > > /*because sizeof(b) is always 4*/
> > > /*and sizeof(b[0]) is always 4*/

> >
> > Both ought to be the same, since both are pointers.
> > And you are trying to get the size of a pointer variable
> > (which is implementation - dependent).
> >
> > As Artie had already suggested, pass the length
> > explicitly as an argument to the function.

>
> I know, but I can't do it.


In that case you are in trouble.

> Do you thing it is possible to find in the stack this information?


No.

1) The C programming language does not require the use of a stack.
2) There is no reason for the compiler to store the size of the array on
the stack so it is unlikely that it will.
3) As soon as you move a pointer out of an object you have created and
deference it (the only way I can think of for you to examine the stack
if there is one) you have entered the realms of undefined behaviour and
anything could happen.

> I mean, I have the position of the first element of the array, then,
> moving up and down in the stack, I find this number. Or is the stack a
> simple storage for the program and only it knows this information.


No. Forget it. Either find a way to pass the size in, find a way of
including an "end of array" marker like the 0 used to mark the end of
strings, or find another language.

> And then how does really operate sizeof? Has it an internal table with
> all variables?


sizeof could be implemented in any way the compiler writer chooses.
However, IMHO, since the compiler has to know the size of the object in
any case (how else can it allocate enough space for it?) the compiler
can just look up that size internally and substitute in the appropriate
value at compile time.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Karthik Kumar
Guest
Posts: n/a
 
      10-12-2004
wrote:
> Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
>
>> wrote:
>>
>>>Hi, is it possible to find the dimension of an array using a pointer?
>>>
>>>main()
>>>{
>>>int a[10];
>>>
>>>f(a);
>>>
>>>return;
>>>}
>>>
>>>
>>>f(int *b)
>>>{
>>>/*how can I know here the size of b??*/
>>>/*because sizeof(b) is always 4*/
>>>/*and sizeof(b[0]) is always 4*/

>>
>> Both ought to be the same, since both are pointers.
>>And you are trying to get the size of a pointer variable
>>(which is implementation - dependent).
>>
>> As Artie had already suggested, pass the length
>>explicitly as an argument to the function.

>
>
> I know, but I can't do it.


Why ? What is the problem ? After all, you are creating the array
and you should know its dimensions.

>
> Do you thing it is possible to find in the stack this information?
>
> I mean, I have the position of the first element of the array, then,
> moving up and down in the stack, I find this number.


How ? You would land up in *undefined behaviour* territory the moment
you access beyond the array boundary.

> Or is the stack a
> simple storage for the program and only it knows this information.


1. The C standard does not talk of stack / heap (though most of the
popular implementations do ).

2. The memory manager does not even come into the picture when you
are talking about sizeof. It is the compiler's job to determine
its value.

>
> And then how does really operate sizeof? Has it an internal table with
> all variables?



<OT>
In compiler design, it is called *symbol table*. And a given
compiler evaluates sizeof at compile-time thanks to the symbol table,
it (the compiler system) would have generated. But then, we are
discussing about the implementation of C. it outside the
scope of the C standard.
</OT>


--
Karthik. http://akktech.blogspot.com .
'Remove _nospamplz from my email to mail me.'
 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      10-12-2004
wrote:
> Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
>
>> As Artie had already suggested, pass the length
>>explicitly as an argument to the function.

>
>
> I know, but I can't do it.


Assuming the reason is that you have control of the implementation
but not the interface one thing you can do is to store the size
somewhere the function can access it. A file scope variable of suitable
linkage should do the trick. This should be a last resort solution
as variables with file scope are bad except when they are good...
It also smells like a hack, a workaround for architectural limits of
the design. If you do not have control of the design you should find
out whether these limits are there for a good reason or just because
your particular need was not thought when created. If you do _not_ have
control of the code that calls the function that needs the size then
you are fubared with respect to standard C. Try asking in a group which
is dedicated to your compiler perhaps.

Otherwise, give some more context and we'll help you if we can.

--
Thomas.
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      10-12-2004
() wrote in message news:<. com>...
> Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
> > wrote:
> > > Hi, is it possible to find the dimension of an array using a pointer?
> > >
> > > main()
> > > {
> > > int a[10];
> > >
> > > f(a);
> > >
> > > return;
> > > }
> > >
> > >
> > > f(int *b)
> > > {
> > > /*how can I know here the size of b??*/
> > > /*because sizeof(b) is always 4*/
> > > /*and sizeof(b[0]) is always 4*/

> >
> > Both ought to be the same, since both are pointers.
> > And you are trying to get the size of a pointer variable
> > (which is implementation - dependent).
> >
> > As Artie had already suggested, pass the length
> > explicitly as an argument to the function.

>
> I know, but I can't do it.
>


Why not?

> Do you thing it is possible to find in the stack this information?
>


No. Seriously, there is no (portable) way to find out the physical
size of an array based on a pointer to the first element alone. You
have two choices:

1. Save the size of the array and either pass it as an argument
or save it in a file scope variable.

2. Write a special sentinel value to the last element of the
array,
like how C strings are arrays of char terminated with a 0.
This
method is far more error prone, though, and won't necessarily
tell
you the *physical* size of the array, just how many elements
come
before the sentinel.

Actually, there's a third choice: pass a pointer to the array (which
is not the same thing as passing a pointer to the first element):

int f(int (*b)[10])
{
/* sizeof *b == sizeof (int) * 10 */
}

int main (void)
{
int a[10];

f(&a);
return 0;
}

The only problem is that this assumes you're only working with arrays
with 10 elements, so it's not very flexible.

> I mean, I have the position of the first element of the array, then,
> moving up and down in the stack, I find this number. Or is the stack a
> simple storage for the program and only it knows this information.
>


Aside from the fact that not all machines even *use* a stack for
passing arguments to functions, what you're passing is a *pointer*
type, not an array type. As far as the called function is concerned,
b is a pointer to a single int object, not an array. That object may
be the first element of an array. It may be the last element of an
array. It may be a scalar variable. There is simply no way for the
called function to know based on the pointer alone.

> And then how does really operate sizeof? Has it an internal table with
> all variables?
>


Possibly. Or it could do something completely different. It's up to
the compiler writer to decide how sizeof actually gets implemented.

> Thank you
>
> Best Regards
>
> Dati Remo

 
Reply With Quote
 
dati_remo@libero.it
Guest
Posts: n/a
 
      10-13-2004
(John Bode) wrote in message news:<. com>...
> () wrote in message news:<. com>...
> > Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
> > > wrote:
> > > > Hi, is it possible to find the dimension of an array using a pointer?
> > > >
> > > > main()
> > > > {
> > > > int a[10];
> > > >
> > > > f(a);
> > > >
> > > > return;
> > > > }
> > > >
> > > >
> > > > f(int *b)
> > > > {
> > > > /*how can I know here the size of b??*/
> > > > /*because sizeof(b) is always 4*/
> > > > /*and sizeof(b[0]) is always 4*/
> > >
> > > Both ought to be the same, since both are pointers.
> > > And you are trying to get the size of a pointer variable
> > > (which is implementation - dependent).
> > >
> > > As Artie had already suggested, pass the length
> > > explicitly as an argument to the function.

> >
> > I know, but I can't do it.
> >

>
> Why not?
>
> > Do you thing it is possible to find in the stack this information?
> >

>
> No. Seriously, there is no (portable) way to find out the physical
> size of an array based on a pointer to the first element alone. You
> have two choices:
>
> 1. Save the size of the array and either pass it as an argument
> or save it in a file scope variable.
>
> 2. Write a special sentinel value to the last element of the
> array,
> like how C strings are arrays of char terminated with a 0.
> This
> method is far more error prone, though, and won't necessarily
> tell
> you the *physical* size of the array, just how many elements
> come
> before the sentinel.
>
> Actually, there's a third choice: pass a pointer to the array (which
> is not the same thing as passing a pointer to the first element):
>
> int f(int (*b)[10])
> {
> /* sizeof *b == sizeof (int) * 10 */
> }
>
> int main (void)
> {
> int a[10];
>
> f(&a);
> return 0;
> }
>
> The only problem is that this assumes you're only working with arrays
> with 10 elements, so it's not very flexible.
>
> > I mean, I have the position of the first element of the array, then,
> > moving up and down in the stack, I find this number. Or is the stack a
> > simple storage for the program and only it knows this information.
> >

>
> Aside from the fact that not all machines even *use* a stack for
> passing arguments to functions, what you're passing is a *pointer*
> type, not an array type. As far as the called function is concerned,
> b is a pointer to a single int object, not an array. That object may
> be the first element of an array. It may be the last element of an
> array. It may be a scalar variable. There is simply no way for the
> called function to know based on the pointer alone.
>
> > And then how does really operate sizeof? Has it an internal table with
> > all variables?
> >

>
> Possibly. Or it could do something completely different. It's up to
> the compiler writer to decide how sizeof actually gets implemented.
>
> > Thank you
> >
> > Best Regards
> >
> > Dati Remo



Thanks to all.

I'll try other solutions.

Dati Remo
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      10-14-2004
Groovy hepcat Karthik Kumar was jivin' on Mon, 11 Oct 2004 12:13:11
-0700 in comp.lang.c.
Re: Array size using pointer's a cool scene! Dig it!

> wrote:
>> Hi, is it possible to find the dimension of an array using a pointer?
>>
>> main()
>> {
>> int a[10];
>>
>> f(a);
>>
>> return;


This return statement is pointless.

>> }
>>
>> f(int *b)
>> {
>> /*how can I know here the size of b??*/
>> /*because sizeof(b) is always 4*/
>> /*and sizeof(b[0]) is always 4*/

>
> Both ought to be the same, since both are pointers.


That is incorrect. b is a pointer, but b[0] is an int.

>And you are trying to get the size of a pointer variable
>(which is implementation - dependent).


No, he is trying to determine the size of an array from a pointer
pointing at the array's first element. But, as has been stated time
and time again here, it is impossible.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
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
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Cast a pointer to array to base class pointer to array Hansen C++ 3 04-24-2010 03:30 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



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