Solved

return or memory address

Posted on 2008-06-23
7
235 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

948 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

18 Experts available now in Live!

Get 1:1 Help Now