473,624 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Choosing function parameter types - pointers or objects?

At the moment, I only write functions which accept pointers as
arguments in order to change a value, but I've realised it would often
be a lot more effiecient to pass a pointer rather than an actual
object.
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?

I'm also interested in any grey areas, or even off-topic functions that
have been written like in the win32 API for instance, but I doubt
anyone else is.

Cheers, Matt

Jun 30 '06 #1
19 1584
ballpointpenthi ef said:
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?


If it's a native type (int, short, long, double, that sort of thing), pass
it in raw unless you need to hack its value. Anything else - structs and
stuff - pass a pointer.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 30 '06 #2
ballpointpenthi ef posted:

I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?

You'd have the measure the efficiency of:

(1) Passing the object by value (thus copying it in the process).

and

(2) Passing its address, and dereferencing the address.
For the intrinsic types, (1) is faster.

For a huge type, an object of which is perhaps a kilobyte, (2) would be
faster.
I'm sure the others can give you a more detailed reply.

--

Frederick Gotham
Jun 30 '06 #3
Richard Heathfield <in*****@invali d.invalid> writes:
ballpointpenthi ef said:
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?


If it's a native type (int, short, long, double, that sort of thing), pass
it in raw unless you need to hack its value. Anything else - structs and
stuff - pass a pointer.


I wouldn't hesitate to pass a *small* structure directly.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 30 '06 #4
Keith Thompson said:
Richard Heathfield <in*****@invali d.invalid> writes:
ballpointpenthi ef said:
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?


If it's a native type (int, short, long, double, that sort of thing),
pass it in raw unless you need to hack its value. Anything else - structs
and stuff - pass a pointer.


I wouldn't hesitate to pass a *small* structure directly.


I would. Small structures have a habit of growing larger. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 30 '06 #5

Frederick Gotham wrote:

<snipped>
You'd have the measure the efficiency of:

(1) Passing the object by value (thus copying it in the process).

and

All function arguments are passed by value in C.
(2) Passing its address, and dereferencing the address.
For the intrinsic types, (1) is faster.

For a huge type, an object of which is perhaps a kilobyte, (2) would be
faster.


Although the OP never mentioned efficiency[1], I think a good strategy
to pass as little as possible would be to pass a pointer whenever
sizeof (datatype) > sizeof (void *).

Efficiency on such a microscopic scale is a silly goal anyway, the
savings are not going to offset the disadvantages. The general rule
I use is to use opaque data types whenever I can which solves the
whole problem quite neatly. In the rare occasion that I cannot use
an opaque type, I'd still use pointers for any data that contains more
than one primitive type (structs, arrays) and only pass the value
itself when it is a primitive type.

goose,
hand

[1] I got the impression the question was more one of style than
of efficiency, but thats just me :-).

Jun 30 '06 #6
"ballpointpenth ief" <Ma************ *@gmail.com> wrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
At the moment, I only write functions which accept pointers as
arguments in order to change a value, but I've realised it would often
be a lot more effiecient to pass a pointer rather than an actual
object.
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?


It is common practice to pass read-only parameters of primitive types by
value, and everything else by pointer.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
--
Posted via a free Usenet account from http://www.teranews.com

Jun 30 '06 #7
goose posted:

Although the OP never mentioned efficiency[1], I think a good strategy
to pass as little as possible would be to pass a pointer whenever
sizeof (datatype) > sizeof (void *).

Then there's no discussion, because both methods solve the problem.

One of them is more efficient in certain cases, yes, but both get the job
done.

If the OP doesn't care about efficiency, the just flip a coin.

--

Frederick Gotham
Jun 30 '06 #8
goose wrote:
Frederick Gotham wrote:

<snipped>
You'd have the measure the efficiency of:

(1) Passing the object by value (thus copying it in the process).

and

All function arguments are passed by value in C.

(2) Passing its address, and dereferencing the address.
For the intrinsic types, (1) is faster.

For a huge type, an object of which is perhaps a kilobyte, (2) would be
faster.

Although the OP never mentioned efficiency[1], I think a good strategy
to pass as little as possible would be to pass a pointer whenever
sizeof (datatype) > sizeof (void *).

Unfortunately this is platform specific.

The cut-off point between pass by value and by pointer varies form
system to system. sizeof(void*) is a reasonable general case.

--
Ian Collins.
Jun 30 '06 #9
Keith Thompson wrote:
Richard Heathfield <in*****@invali d.invalid> writes:
ballpointpenthi ef said:
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?

If it's a native type (int, short, long, double, that sort of thing), pass
it in raw unless you need to hack its value. Anything else - structs and
stuff - pass a pointer.


I wouldn't hesitate to pass a *small* structure directly.

Why pass a struct rather than a pointer to it? You can examine the
struct either way but if you would modify any member of it, and pass the
struct by value, the function must 'return' the value and you must
assign it to the original struct. If you pass a pointer to it, you can
modify the member of the original struct (in)directly.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3625
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
10
10021
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return value of float is changed to float seems not to work; I belive it was like this: typedef int (*foo) (int); function_ptr_bar = (foo)function_that_returns_float;
3
1819
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling of a sequence of functions with arbitrary return types and/or parameters? -- Randy Yates Sony Ericsson Mobile Communications Research Triangle Park, NC, USA
41
10016
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed...
5
2090
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
12
8381
by: srinivas.satish | last post by:
Hi, is it possible to typecast a function pointer to two different prototypes. eg., typedef void (functptr1 *) (int , int); typedef void (functptr2 *) (int); functptr1 fptr; fptr = somefunction_name; fptr(10,20); fptr = (functptr2)someotherfunction_name;
54
24484
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
4
2486
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec 2006) $ Author: Collin Winter <collinw@gmail.com>, Tony Lownds <tony@lownds.com> Status: Draft Type: Standards Track
36
2578
by: Pat | last post by:
Hi, I've run into a strange problem, but one that seems like it might be fairly common. I have a single base class, from which several other classes are derived. To keep the example simple, the base class is "Animal" and the derived classes are "Cat," "Dog," and "Horse." The base class has a pure virtual method:
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.