Solved

return or memory address

Posted on 2008-06-23
7
226 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
Comment Utility
for your example, it's exactly the same.
0
 

Author Comment

by:zizi21
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
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
Comment Utility
thanks
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
@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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now