Solved

return or memory address

Posted on 2008-06-23
7
257 Views
Last Modified: 2011-09-20
hi,

is it more efficient to assign value to memory address by passing pointer to the function or by using return...
for instance...

void myfunc(int * a)
*a=4;

or
int myfunc(int a)
return a=4;

thanks

0
Comment
Question by:zizi21
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Expert Comment

by:figroc
ID: 21852160
for your example, it's exactly the same.
0
 

Author Comment

by:zizi21
ID: 21852241
yup...but for others it might be different..which would be more better...

for instance, if i call return 1000 times would be more time consuming than using pointer....since we are not returning anything....or it does not make any diffrene...
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21852334
It really does not matter and it depends on the implementation ... C does not dictate how pointers get assigned and how values get returned ... Each implementation is free to optimize them as they please.
In any case the difference, if any is insignificant.

>if i call return 1000 times would be more time consuming than using pointer
You would return after the first return so effectively 999 times it wont be executed.

The choice between returning and using an output parameter is generally dictated by need and coding conventions.
Many softwares use return value to indicate success/failure and output parameters for communicating values.
At times when your function can change the value it is being passed and this change needs to be reflected in the caller, you have to use pointers.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 21852752
It does matter whether you return or pass it as parameter.

For example :

        void fun(Big *bp) {
            bp->data = someValue;
            bp->data2 = someOtherValue;
        }

        Big someBigObject;
        fun(&someBigObject);

allows the function to work on the original Big object. While :

        Big fun() {
            Big b;
            b.data = someValue;
            b.data2 = someOtherValue;
            return b;
        }

        Big someBigObject;
        someBigObject = fun();

would potentially copy the data twice (once to place it in the local functionBig object, and once to return it). The compiler MIGHT optimize this, but there's no guarantee that it can do that.

The first version will most likely be faster.
0
 

Author Closing Comment

by:zizi21
ID: 31470029
thanks
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21852766
The underling assumption of comparison is that we are passing comparable data size ... Returning a pointer to someBigObject intead of a someBigObject makes more sense for comparison ...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21852795
@sunnycoder, following the example from the original question only strengthens my argument. Take a look at :

        void fun(Big *bp) {
            bp->data = someValue;
            bp->data2 = someOtherValue;
        }

        Big someBigObject;
        fun(&someBigObject);

versus :

        Big fun(Big b) {
            b.data = someValue;
            b.data2 = someOtherValue;
            return b;
        }

        Big someBigObject;
        someBigObject = fun(someBigObject);

The second is even worse when it comes to copies.


Remember that the question was about :

>> void myfunc(int * a)
>> *a=4;

versus :

>> int myfunc(int a)
>> return a=4;

There are no pointers in the second version.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question