[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

return or memory address

Posted on 2008-06-23
7
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 opening and reading files in the C programming language.

656 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