Array size using pointer

D

dati_remo

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
 
A

Artie Gold

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
 
K

Karthik Kumar

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

dati_remo

Karthik Kumar said:
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
 
F

Flash Gordon

On 12 Oct 2004 00:37:26 -0700
Karthik Kumar said:
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.
 
K

Karthik Kumar

Karthik Kumar said:
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>
 
T

Thomas Stegen

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

John Bode

Karthik Kumar said:
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.
 
D

dati_remo

Karthik Kumar said:
(e-mail address removed) 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
 
P

Peter Shaggy Haywood

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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top