469,623 Members | 1,273 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Question on passing a structure to a function

I have a structure

typedef struct t_abcd
{
int a;
int b;
int c;
} abcd;

I have to pass the structure to another function. I can do this in two ways.
1) I am passing this structure as shown
func()
{
abcd *ps;
ps = malloc(sizeof(abcd);
someFunc(ps);
}

2) i am passing this structure as shown
func2()
{
abcd *ps;
ps = malloc(sizeof(abcd);
someFunc(&ps);
}


The associated API's for these two calls would be
int someFunc( abcd *ps); /*For the first type of call*/
int someFunc(abcd **pq); /*For the second type of call*/

Could you guys please tell me which would have the faster access to and from the CPU? Assuming that this function is called 97 % of the time during execution of the module which contains this function
May 9 '07 #1
1 1484
DeMan
1,806 1GB
In both cases you are pasing a pointer (in the first to the structure, in the second to the pointer to the structure).

I would imagine passing the pointer is just as efficient both times, But I suspect (because you have to translate two pointers) that the second would take fractionally longer to access because you have to check two pointers.



(I think you may have meant the question to refer to
someFunc(*ps); (passing the Actual as in - struct int someFunc( abcd ps)
in the first instance which I would think would create a lot more overhead if you called this function frequently)
May 9 '07 #2

Post your reply

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

Similar topics

27 posts views Thread by Ted Lilley | last post: by
5 posts views Thread by kazack | last post: by
2 posts views Thread by Steve Turner | last post: by
7 posts views Thread by Jake Thompson | last post: by
6 posts views Thread by RSH | last post: by
7 posts views Thread by Logan Lee | last post: by
13 posts views Thread by Andy Baker | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.