Reply embedded...
> -----Original Message-----
> From: Michael White [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, January 04, 2005 6:43 AM
> To: [email protected]
> Subject: Re: [C-Paradise] Pointers
>
>
> Is someone looking to increase the speed of a program, or are they trying
> to save memory?
> Forgive me if my observation is incorrect, as I am a new programmer
myself.
>
> As I think of it, when you pass a variable by value to a function, you are
> having to make another copy of the value thus taking up more space in
> memory for the duration of the function.
Same as pointer, A copy of the address is passed.
Consider the following two variations:
1. int foo(const int i) { return i * i; }
2. int foo(const int * const pi) { return *pi * *pi; }
What gain do you have by passing address instead of a copy? With addition
and unnecessary redirection, you're most likely to lose than gain.
However, what you said is true if the parameter is a structure or a
user-defined class. When pass by value, the entire structure is copied
verbatim or copy-constructor is invokes for class. This might invokes
additional cost both in terms of time and space. Thus in this case, pass by
pointer or reference is the desire method.
> Of course with how much RAM is in the average system now days this
> shouldn't too much of an issue. Passing by value is fine if you are
> only having to effect one value by a 'return' statement.
It still might matter. Sometimes, an (C++) object is not designed to be
duplicated, for those situation, the copy-constructor (and assignment
operator) are not publicly accessible. The only way is to pass via pointer
or reference.
> But when your function changes several variables, you need to use pointer
> notation to be able to return more than one value (which in reality you're
> not returning anything but adjusting the variable itself through
> dereferencing).
This is true. And this is the time when pointer (or reference in C++) is
needed to accomplish the task.
> Of course one definite benefit here is you are not taking
> up extra memory by making a copy of every variable that is sent to the
> function. This saves space in RAM, and keeps the program from spending any
> clock time to make the copy.
You're still making copy of the pointer. To make your statement concrete,
you have to compare between copying a collection of stuff in a struct verses
single pointer to the same collection of stuff.
> Also, if you can effect several variables that need to have the same
> function called upon them you can save yourself some personal time by not
> having to type more code. So...try to go for 'portability' when writing
> your programs.
Elaborate please.
> As my teacher's advice was..."If only effecting one variable, pass by
> value...if effecting two or more variables pass by reference".
Not entire true.
There is one situation where pointer is the only solution: when your
function expecting an "invalid state" or "unused state".
Only pointer offers two states. A non-null value means the pointer pointing
to some valid memory and a NULL pointer means the pointer is invalid. No
other passing methods (by value or by reference) offer this kind of
functionality.
One example is the strtol() function. The second argument is the address of
a pointer where strtol() will modified to point to the position of the input
string where the scan stopped. This is useful for issuing feedback to user
where the error is. If your program does not need this information at all,
simply pass NULL or 0 to in place of this argument.
Another example is the non-standard _splitpath() function in VC++. This
function will take a full path name and store its various components (drive,
directory, filename, extension) into its subsequences arguments. You can
pass NULL to one of those arguments to tell _splitpath() that you are not
interested in that particular component.
HTH
Shyan
>-----------------------------------------~-~>
CHECK THE ARCHIVE BEFORE POSTING!!!! Archive is available at
http://www.eScribe.com/software/C-Paradise/
>------------------------------------------_->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/C-Paradise/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/