size of passed array

V

Vasileios Zografos

Hi everyone,

I need to build a function to plug it in a program (that I didnt make or
can change) that should be called something like this:


float someFunction(float x[])
{
...

}


the problem is, I cannot use someFunction(float x[], int sizex) so I
cannot know the size of the array.
Is there a way I can calculate the size of the static array from inside
the function e.g. sizeof(x)/sizeof(float) or something similar?

Thank you
V.
 
J

John Harrison

Vasileios said:
Hi everyone,

I need to build a function to plug it in a program (that I didnt make or
can change) that should be called something like this:


float someFunction(float x[])
{
...

}


the problem is, I cannot use someFunction(float x[], int sizex) so I
cannot know the size of the array.
Is there a way I can calculate the size of the static array from inside
the function e.g. sizeof(x)/sizeof(float) or something similar?

Thank you
V.

No. This is a good reason not to use arrays, use vectors instead.

BTW in 'float someFunction(float x[])' x is *not* an array, it is a
pointer, it is exactly the same as if you had written 'float
someFunction(float* x)'. You should prefer the second form because it
doesn't try to pretend that x is an array when it isn't.

You need to read up on arrays and pointers in a good C++ book.

john
 
J

John Harrison

Vasileios said:
Hi everyone,

I need to build a function to plug it in a program (that I didnt make or
can change) that should be called something like this:


float someFunction(float x[])
{
...

}


the problem is, I cannot use someFunction(float x[], int sizex) so I
cannot know the size of the array.
Is there a way I can calculate the size of the static array from inside
the function e.g. sizeof(x)/sizeof(float) or something similar?

Thank you
V.

If I understand this right, you have to write a function, which can be
called from various places in someone else's code, and could be called
with different size arrays, but you are not allowed to know what the
size of the array is.

Such a function is impossible to write. Either you have misunderstood
what you are supposed to do, or the person who wrote the other code is a
very poor programmer.

john
 
V

Vasileios Zografos

John said:
If I understand this right, you have to write a function, which can be
called from various places in someone else's code, and could be called
with different size arrays, but you are not allowed to know what the
size of the array is.

Such a function is impossible to write. Either you have misunderstood
what you are supposed to do, or the person who wrote the other code is a
very poor programmer.

john

Hi John,
yes I think you are probably right. Maybe I misunderstood the
requirements because indeed it sounds impossible. I will check again.
Thank you
V.
 
V

Vasileios Zografos

John said:
If I understand this right, you have to write a function, which can be
called from various places in someone else's code, and could be called
with different size arrays, but you are not allowed to know what the
size of the array is.

Such a function is impossible to write. Either you have misunderstood
what you are supposed to do, or the person who wrote the other code is a
very poor programmer.

john

Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])

What is the passed parameter? An array of float right? If not, what
exactly is it? Help :)

Thank you
V.
 
J

John Harrison

Vasileios said:
John said:
If I understand this right, you have to write a function, which can be
called from various places in someone else's code, and could be called
with different size arrays, but you are not allowed to know what the
size of the array is.

Such a function is impossible to write. Either you have misunderstood
what you are supposed to do, or the person who wrote the other code is
a very poor programmer.

john


Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])

What is the passed parameter? An array of float right? If not, what
exactly is it? Help :)

Thank you
V.

No, its a pointer to a float.

It is impossible to pass an array to a function in C or C++. You
*always* pass a pointer to the first element of the array.

If you write this

void func(float[] x)
{
}

the compiler understands it like this

void func(float* x)
{
}

Here's proof

void func(float[] x);

int main()
{
float array[10];
cout << sizeof array << '\n';
func(array);
}

void func(float[] x)
{
cout << sizeof x << '\n';
}

Run this program, the output will be

40
4

The actual numbers might be different on your computer, but the point is
that 'array' really is an array so it's size is 40, but x really is a
pointer, so it's size is 4.

It a bad habit (in my opinion) to use the 'func(float[] x)' notation
because it just consufes beginners into thinking that something is an
array when really it is a pointer.

You really need a good C++ book, to explain the differences and
similarities between arrays and pointers. This is always a confusing
subject for beginners.

john
 
J

John Harrison

Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])

That isn't a function call. It's a declaration of a function pointer.
The function pointer points a ta function which takes a pointer to float
as it's only argument.
What is the passed parameter? An array of float right? If not, what
exactly is it? Help :)

As I said, that isn't a function call, so there is no passed parameter.
Thank you
V.

john
 
V

Vasileios Zografos

John said:
Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])

That isn't a function call. It's a declaration of a function pointer.
The function pointer points a ta function which takes a pointer to float
as it's only argument.
What is the passed parameter? An array of float right? If not, what
exactly is it? Help :)

As I said, that isn't a function call, so there is no passed parameter.
Thank you
V.

john
Hi,
yes its not the function call but I instead pasted the declaration so
people can see what kind of parameters it takes. The actual call is:

(*funk)(psum);

but that wouldn't be very helpful would it? :)
Anyway, yes I know the difference between pass by value of by ref and
the default pass by reference way of passing arrays in C++
The problem is that I need to know how many members are in this float
array and I am not sure how to do that.

So if you see a function declaration like that:
float (*funk)(float [])
and the only info to go with it is:
"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

how can I know what ndim is from INSIDE the function?
Any ideas?

Thanks
V.
 
A

Alf P. Steinbach

* John Harrison:
It is impossible to pass an array to a function in C or C++. You
*always* pass a pointer to the first element of the array.

In C, but not in C++.

E.g.

void foo( int (&a)[10] ) {}

accepts an array of ten 'int', and nothing else. The array is passed by
reference.
 
J

John Harrison

Vasileios said:
John said:
Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])


That isn't a function call. It's a declaration of a function pointer.
The function pointer points a ta function which takes a pointer to
float as it's only argument.
What is the passed parameter? An array of float right? If not, what
exactly is it? Help :)


As I said, that isn't a function call, so there is no passed parameter.
Thank you
V.

john

Hi,
yes its not the function call but I instead pasted the declaration so
people can see what kind of parameters it takes. The actual call is:

(*funk)(psum);

but that wouldn't be very helpful would it? :)
Anyway, yes I know the difference between pass by value of by ref and
the default pass by reference way of passing arrays in C++
The problem is that I need to know how many members are in this float
array and I am not sure how to do that.

So if you see a function declaration like that:
float (*funk)(float [])
and the only info to go with it is:
"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

how can I know what ndim is from INSIDE the function?
Any ideas?

Well I've said it a few times, the answer is you cannot.

john
 
J

John Harrison

Alf said:
* John Harrison:
It is impossible to pass an array to a function in C or C++. You
*always* pass a pointer to the first element of the array.


In C, but not in C++.

E.g.

void foo( int (&a)[10] ) {}

accepts an array of ten 'int', and nothing else. The array is passed by
reference.

Yes, it's true, my mistake. But not a possibility that's helpful to the
OP I think.

john
 
J

John Harrison

John said:
Vasileios said:
John said:
Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])



That isn't a function call. It's a declaration of a function pointer.
The function pointer points a ta function which takes a pointer to
float as it's only argument.


What is the passed parameter? An array of float right? If not, what
exactly is it? Help :)



As I said, that isn't a function call, so there is no passed parameter.


Thank you
V.


john


Hi,
yes its not the function call but I instead pasted the declaration so
people can see what kind of parameters it takes. The actual call is:

(*funk)(psum);

but that wouldn't be very helpful would it? :)
Anyway, yes I know the difference between pass by value of by ref and
the default pass by reference way of passing arrays in C++
The problem is that I need to know how many members are in this float
array and I am not sure how to do that.

So if you see a function declaration like that:
float (*funk)(float [])
and the only info to go with it is:
"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

how can I know what ndim is from INSIDE the function?
Any ideas?


Well I've said it a few times, the answer is you cannot.

john

In fact the comment

"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

makes me think of Pascal. Are you sure this program hasn't been (badly)
ported from Pascal? In Pascal you would be able to get the size of the
vector from inside the function.

john
 
V

Vasileios Zografos

John said:
John said:
Vasileios said:
John Harrison wrote:


Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])



That isn't a function call. It's a declaration of a function
pointer. The function pointer points a ta function which takes a
pointer to float as it's only argument.


What is the passed parameter? An array of float right? If not, what
exactly is it? Help :)



As I said, that isn't a function call, so there is no passed parameter.


Thank you
V.


john


Hi,
yes its not the function call but I instead pasted the declaration so
people can see what kind of parameters it takes. The actual call is:

(*funk)(psum);

but that wouldn't be very helpful would it? :)
Anyway, yes I know the difference between pass by value of by ref and
the default pass by reference way of passing arrays in C++
The problem is that I need to know how many members are in this float
array and I am not sure how to do that.

So if you see a function declaration like that:
float (*funk)(float [])
and the only info to go with it is:
"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

how can I know what ndim is from INSIDE the function?
Any ideas?


Well I've said it a few times, the answer is you cannot.

john

In fact the comment

"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

makes me think of Pascal. Are you sure this program hasn't been (badly)
ported from Pascal? In Pascal you would be able to get the size of the
vector from inside the function.

john
hehe :) I am not that dumb...honestly
In case you are wondering the code snippet is from Numerical recipes in
C. Its the simplex optimisation code.
Yes ok I CAN actually change the function to call "func" in a different
way, but i want to know why they programmed the code like that. And its
NOT pseudocode.
Ok so its C not C++ but I think the question can be answered in this
newsgroup
 
J

John Harrison

hehe :) I am not that dumb...honestly
In case you are wondering the code snippet is from Numerical recipes in
C. Its the simplex optimisation code.
Yes ok I CAN actually change the function to call "func" in a different
way, but i want to know why they programmed the code like that. And its
NOT pseudocode.
Ok so its C not C++ but I think the question can be answered in this
newsgroup

OK, I looked at the code, what can I say except that it appears to be
poor code. I guess they just forgot that you might need the size of the
array.

john
 
V

Vasileios Zografos

John said:
OK, I looked at the code, what can I say except that it appears to be
poor code. I guess they just forgot that you might need the size of the
array.

john

Yes I guess so. Probably my only choice is to change the function calls
and declarations.
Ok John (and the rest of the people) thanks for going through the
trouble of checking the code. I appreciate it.

V.
 
I

Ian Collins

Vasileios said:
Yes I guess so. Probably my only choice is to change the function calls
and declarations.
Ok John (and the rest of the people) thanks for going through the
trouble of checking the code. I appreciate it.
When you do, use std::vector rather than adding the size as a parameter.
 
A

Adrian Hawryluk

John said:
BTW in 'float someFunction(float x[])' x is *not* an array, it is a
pointer, it is exactly the same as if you had written 'float
someFunction(float* x)'. You should prefer the second form because it
doesn't try to pretend that x is an array when it isn't.

Sorry, you are wrong, x can decompose into a pointer, but it is _not_ a
pointer. A pointer definition (float *x) can have its value changed,
whereas an array definition (float x[]) cannot change its base location.
Of course, you could define your pointer as a constant pointer (float
* const x). But if you are passing an array, then don't hide it as a
pointer, use the array syntax.

Really though, if you are passing an array either:

1. don't, use a container class such as vector
2. pass the bounds of the first element as a separate variable
3. pass a pointer to the array and use a template function. Be warned
though, using a template can cause extra code bloat (a new function
is emitted for each different size array pointer passed). Also the
extra dereference may also add extra overhead, though I think that
should be optimised out. Here is an example.


template <int SIZE>
int f(int (*a)[SIZE])
{
int element0 = (*a)[0];
return SIZE;
}

int a[10];
int b[20];
cout << f(&a) << endl; // prints 10
cout << f(&b) << endl; // prints 20


Adrian

--
==========================================================
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
==========================================================
 
A

Adrian Hawryluk

Vasileios said:
John said:
John said:
Vasileios Zografos wrote:

John Harrison wrote:


Hi again,
well the function call from the main program looks like this:


float (*funk)(float [])



That isn't a function call. It's a declaration of a function
pointer. The function pointer points a ta function which takes a
pointer to float as it's only argument.


What is the passed parameter? An array of float right? If not,
what exactly is it? Help :)



As I said, that isn't a function call, so there is no passed
parameter.


Thank you
V.


john


Hi,
yes its not the function call but I instead pasted the declaration
so people can see what kind of parameters it takes. The actual call
is:

(*funk)(psum);

but that wouldn't be very helpful would it? :)
Anyway, yes I know the difference between pass by value of by ref
and the default pass by reference way of passing arrays in C++
The problem is that I need to know how many members are in this
float array and I am not sure how to do that.

So if you see a function declaration like that:
float (*funk)(float [])
and the only info to go with it is:
"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

how can I know what ndim is from INSIDE the function?
Any ideas?


Well I've said it a few times, the answer is you cannot.

john

In fact the comment

"the function funk(x) where x[1..ndim] is a vector in ndim dimensions"

makes me think of Pascal. Are you sure this program hasn't been
(badly) ported from Pascal? In Pascal you would be able to get the
size of the vector from inside the function.

john
hehe :) I am not that dumb...honestly
In case you are wondering the code snippet is from Numerical recipes in
C. Its the simplex optimisation code.
Yes ok I CAN actually change the function to call "func" in a different
way, but i want to know why they programmed the code like that. And its
NOT pseudocode.
Ok so its C not C++ but I think the question can be answered in this
newsgroup

The only way is to define the array with a termination value, such as 0
or other constant. Then you can read the array sequentially and if it
does not match, the element can be processed, otherwise you have reached
the end or the array.


Adrian

--
==========================================================
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
==========================================================
 
G

Gavin Deane

John said:
BTW in 'float someFunction(float x[])' x is *not* an array, it is a
pointer, it is exactly the same as if you had written 'float
someFunction(float* x)'. You should prefer the second form because it
doesn't try to pretend that x is an array when it isn't.

Sorry, you are wrong, x can decompose into a pointer, but it is _not_ a
pointer.

Yes it is.
A pointer definition (float *x) can have its value changed,
whereas an array definition (float x[]) cannot change its base location.

I'm not sure what you mean here. Are you suggesting that some part of
the following code should not compile? Or that the behaviour of some
part of it is undefined? Or have I misunderstood you? In both foo1 and
foo2, x is a (non-const) pointer to (non-const) float.

Comeau online compiles the code with no errors.

void foo1(float* x)
{
float f = 0.0f;
x = &f;
}

void foo2(float x[])
{
float f = 0.0f;
x = &f;
}

int main()
{
float array[10] = {0.0f};
foo1(array);
foo2(array);

float a_float = 0.0f;
foo1(&a_float);
foo2(&a_float);
}

Also, the following does not compile due to a redefinition error. If
float* x and float x[] were different types in the argument list, this
should be acceptable as two distinct overloads of foo. But it isn't.

void foo(float* x) {}
void foo(float x[]) {}

Gavin Deane
 
A

Adrian Hawryluk

Gavin said:
John said:
BTW in 'float someFunction(float x[])' x is *not* an array, it is a
pointer, it is exactly the same as if you had written 'float
someFunction(float* x)'. You should prefer the second form because it
doesn't try to pretend that x is an array when it isn't.
Sorry, you are wrong, x can decompose into a pointer, but it is _not_ a
pointer.

Yes it is.

No it isn't.
A pointer definition (float *x) can have its value changed,
whereas an array definition (float x[]) cannot change its base location.

I'm not sure what you mean here. Are you suggesting that some part of
the following code should not compile? Or that the behaviour of some
part of it is undefined? Or have I misunderstood you? In both foo1 and
foo2, x is a (non-const) pointer to (non-const) float.
[snip]

void foo2(float x[])
{
float f = 0.0f;
x = &f;
}

_If_ that does work, it cannot be portable to all compilers, and I would
deem it as a compiler bug.

Try this:

void foo(float *x)
{
++x;
float element1 = *x;
}

void bar(float[] x)
{
++x; // compilation error
float element1 = *x;
}

void zozo(float * const x)
{
++x; // different compilation error
float element1 = *x;
}

Unless you can point to a _specific_ location in the C++ spec that
states unequivocally that a float[] is *the same as* and not *equivalent
to* a float *, then forget it. If it don't quack like a duck in /all/
respects, then it ain't a duck.
Also, the following does not compile due to a redefinition error. If
float* x and float x[] were different types in the argument list, this
should be acceptable as two distinct overloads of foo. But it isn't.

void foo(float* x) {}
void foo(float x[]) {}

No, this is an ambiguous overload, not the same thing. Some compilers
may allow it to look the same for added simplicity, but it is *not* the
same!

http://www.cplusplus.com/articles/siavoshkc1.html


Adrian

--
==========================================================
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
==========================================================
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top