473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing arrays to a function

24
Say I have a couple functions like so:

Expand|Select|Wrap|Line Numbers
  1. typedef float vector[3];
  2.  
  3. void foo(vector t){
  4. /* Do whatever */
  5. }
  6.  
  7. void bar(void){
  8. vector v;
  9. foo(v);
  10. /* Rest of program */
  11. }
Would that be treated the same as:


Expand|Select|Wrap|Line Numbers
  1. typedef float vector[3];
  2.  
  3. void foo(float * t){
  4. /* Do whatever */
  5. }
  6.  
  7. void bar(void){
  8. vector v;
  9. foo(v);
  10. /* Rest of program */
  11. }
The main thing I'm worried about is if it is pushing 3 temporary float variables on to the stack for the array, rather than passing the array's pointer.
Feb 11 '10 #1
9 2209
Ectara
24
Did a quick test in GDB, and it appears to pass it as a float pointer. I'll leave this as a reference for others, though it may be implementation dependent.
Feb 11 '10 #2
Banfa
9,065 Expert Mod 8TB
The whole stack is implementation dependent, in fact it is so implementation dependent that there is actually no requirement to have a stack.

What is implementation dependent is how a given platform passes a compound object, like an array or structure when passed by value. If you pass a pointer then a pointer is what will get passed.
Feb 11 '10 #3
donbock
2,426 Expert 2GB
Following up on Banfa's post ... you should get in the habit of defining your functions to pass pointers to compound types instead of the compound types themselves. You can pass a const pointer if you want to insure the function doesn't change the caller's copy. Likewise, you should get in the habit of defining your functions so they do not return compound types.

The language allows you to pass and return compound types, and that feature does indeed work, but you don't really know how the underlying implemention accomplishes that. You may end of with especially ineffiicient code or with code that is not thread-safe.
Feb 11 '10 #4
Ectara
24
If I pass a pointer to a vector3, I get a pointer to a pointer, and I'm not sure it would be efficient to double dereference. I never return a vector3 in the first place. I was merely wondering if it passed a float pointer to a function rather than allocate/occupy/magically create 3 float values in an array for local use, like it would passing a structure by value. I know how a stack works.
Feb 15 '10 #5
I hope you don't mind my jumping in, but I'm puzzled by Banfa and donbock's posts. Should arrays be lumped together with all compound types? In Ectara's example, v is just an array of 3 floats, not a structure. Isn't it true that whenever an arrayname is passed as a function argument it is implicitly converted to a pointer ( although NOT when passed to the sizeof or addressof operators)? Isn't that required by both C and C++ standards, and not implementation-specific?

Also, if there's any doubt about that, you could pass &v[0] which is certainly a pointer.
Feb 16 '10 #6
Banfa
9,065 Expert Mod 8TB
Hi rstiltskin,

The confusion here is partly that Don and I were talking theory where as you are talking practically. You can't actually directly pass an array by value and as you say the array name is a pointer to the first element in the array.

However examine the first code snipet in the first post. It is not actually passing an array, it is passing a type vector which is typedef'd to an array of 3 floats. vector is therefore a compound type that is an array and it could be passed by value.

The reason you can passs an array by value is there is no syntax to support this. The typedef gets round this by letting you declare an array using the same syntax you would use to declare an int. Therefore the typedef'd type vector, that is an array, can be passed by value.

Don and my point is that no compound type, typedef'd vector, structure or anything else you can think of should be passed by value or returned by value in C.

In C++ things are a bit different, compound types are often returned by value, they have to be for objects to work properly, and so passing by value is not quite so frowned upon either. However you should prefer passing by reference were possible over passing by value.


Ectara

Double deferencing is not particularly inefficient and not a particularly great overhead over the single dereference of passing an array or a pointer to an array of float. Passing a pointer to a vector has the added benifit of type checking. You know you will always be receiving the right number of floats because the type checking of the compiler ensures it. If your function accepts a pointer to float then it could be passed a pointer to an array of any number of floats including 1 or 2 which would presumably crash your function.

If you are really worried about the double dereference and the function is an exceptionally complex you could copy the vector into local data first to remove one or both dereferencing steps during the actual calculation.
Feb 16 '10 #7
donbock
2,426 Expert 2GB
Typedefs in C accomplish less than you might think. The typedef name is little more than a synonym for the underlying type and the two can be used interchangeably.

rstiltkin: I think you're right -- declaring a variable as vector is equivalent to declaring it as an array of 3 floats. In C89, function arguments declared as [fixed-length] arrays are treated as pointers to the first element of the array and the array size is disregarded. (I don't know if C99 changed this; but I doubt it.) This last point about the array size being disregarded means that the compiler will not complain if you pass an array of a different size!

Consider the definition of the vector type:
Expand|Select|Wrap|Line Numbers
  1. typedef float vector[3];
It seems to me that Ectara intends to define a compound type that consists of exactly three floats -- no more, no less. Unfortunately, this type definition fails to achieve that goal; at least when it comes to passing it as a function argument. The type should be defined as follows to guarantee that all usages, including function arguments, are identical.
Expand|Select|Wrap|Line Numbers
  1. typedef struct { float values[3]; } vector;
or
Expand|Select|Wrap|Line Numbers
  1. typedef struct { float x, y, z; } vector;
Now you truly have a compound type that Banfa and I recommend be passed and returned by reference rather than by value (in C).
Feb 16 '10 #8
Banfa
9,065 Expert Mod 8TB
Hey thats not how I thought it worked by after a little impeical experimentation it does appear that with this defined

typedef float vector[3];

this

void foo(vector vec);

and this

void foo(float vec[]);

are equivilent to the extent that they could both appear in the same file without causing an error.

So kudos to donbock


rstiltskin what this means is that what I should have said (which is what I almost said) is that it is not actually possible to pass an array by value at all but we were sort of thorising that if you could you shouldn't (in C) for the same reasons that you shouldn't pass a structure by value.
Feb 16 '10 #9
Thanks. I have no argument with that. I was just bothered because you seemed to be suggesting that typedef'ing an array changes the way that the compiler treats it -- in effect creating a new type rather than simply a 'nickname'.

But I have to say that this
...what this means is that what I should have said (which is what I almost said) is that it is not actually possible to pass an array by value at all but we were sort of thorising that if you could you shouldn't ...
is priceless. ;)
Feb 16 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
2
by: sonaliagr | last post by:
I am trying to update a msg array in function by passing the address but it is showing an error. and also, i want the value of msg array to be accessible to the full code that is inside the main...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: Fizzics | last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.