pass by Reference/value ???

D

dumboo

hi there
i m little bit confused over the following problem, i have understood wt the
following code is doing...but not able to get the actual techinical
stuff.....i have had a lot of hot debate over the following code whether its
pass by reference or pass by value...hope somebody clears it up :)
void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

int main()
{
char *s;
foo(s);
cout<<s;
delete s;

return 0;
}

regards
 
A

Artie Gold

dumboo said:
hi there
i m little bit confused over the following problem, i have understood wt the
following code is doing...but not able to get the actual techinical
stuff.....i have had a lot of hot debate over the following code whether its
pass by reference or pass by value...hope somebody clears it up :)

Well, the code is totally bogus and exhibits undefined behavior. If you
are lucky, it'll only crash.
void foo(char *s)

`foo()' is passed a pointer-to-char -- by value
{
s = new char[10];

s -- the local copy of this pointer-to-char now points to enough memory
out of the free store to hold ten chars.
strcpy(s, "gotit");

The string literal "gotit" is copied into that memory.

And the local copy of `s' has gone out of scope, causing a memory leak.
int main()
{
char *s;

`s' is an uninitialized pointer-to-char that points to...well, *anywhere*,

`foo()' is called; however as it has been passed by value it is
unchanged, hence still uninitialized.

An attempt is made to send the contents pointed to by `s' to the stream
`cout'. Since `s' points...well *anywhere*...undefined behavior is
invoked. [first time]
delete s;

An attempt to delete the memory pointed to by `s' is made. Since s is an
uninitialized pointer, undefined behavior is invoked. [second time]
return 0;
}
If you change the signature of `foo()' to:

void foo(char*& s)

you will get the behavior I would suspect you want.

HTH,
--ag
 
J

Jonathan Turkanis

dumboo said:
hi there
i m little bit confused over the following problem, i have understood wt the
following code is doing...but not able to get the actual techinical
stuff.....i have had a lot of hot debate over the following code whether its
pass by reference or pass by value...hope somebody clears it up :)
void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

int main()
{
char *s;
foo(s);
cout<<s;
delete s;

return 0;
}

Here you are passing a pointer by value.

Among other things, you need to delete s like so: delete [] s.

BTW, who having you been hotly debating?

Jonathan
 
S

Sharad Kala

dumboo said:
hi there
i m little bit confused over the following problem, i have understood wt the
following code is doing...but not able to get the actual techinical
stuff.....i have had a lot of hot debate over the following code whether its
pass by reference or pass by value...hope somebody clears it up :)

Didn't you try to run the program? Let's analyse the program line by line.
void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

int main()
{
char *s;
s points to some garbage address.
Copy the garbage address contained in s to foo's s. This is pass-by-value.
Then in foo you make foo's copy of s to point to some valid dynamic store (heap)
address. Write something to it and then do a *memory leak* when you don't delete
what s points to. s gets destructed when function returns and the memory it
allocated is
never given back to the system.
Now you return in main. Here s still points to the same garbage address it
pointed to before it entered foo.
You know now what you can expect from this program..right?
delete s;
Now you try to free the memory pointed by s. But actaully s points to some
garbage address!
Also you should have done delete [] s;
return 0;
}

The easiest way to resolve this is by changing foo like -
void foo(char *& s)
Now when you change s within foo it gets reflected in main too.
This is pass-by-reference.

-Sharad
 
J

John Carson

dumboo said:
hi there
i m little bit confused over the following problem, i have understood
wt the following code is doing...but not able to get the actual
techinical stuff.....i have had a lot of hot debate over the
following code whether its pass by reference or pass by value...hope
somebody clears it up :)
void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

int main()
{
char *s;
foo(s);
cout<<s;
delete s;

return 0;
}

regards


There is some confusion here resulting from C++'s origin in C. The C
language does not have C++-style references. Accordingly, if you want a
function in C to modify something, you need to pass it a pointer to the
thing to be modified. In C, this is called "passing by reference". Now
consider your function:

void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

In C, this would be called "passing by reference". The important thing to
note, however, is that the thing that is passed by reference is *not* the
variable s but what s points to, i.e., s is passed by value and what s
points to is passed by reference. What this means is that

1. foo receives a local copy of s and any changes that it makes to s only
affect that local copy.
2. the copy of s that foo receives points to the same memory area as the
original s did, so any changes made to what s points to have the same effect
within the function as they would have in the place from which the function
is called.

In your line:

s = new char[10];

you are changing the local copy of s, which has no effect on the original s.
Thus cout in main() is being applied to the uninitialised s.

If you want to modify s, then there are two ways to go about it. The C-style
approach is to pass the address of s rather than s itself, as follows:

void foo(char **ps)
{
*ps = new char[10];
strcpy(*ps, "gotit");
}

int main()
{
char *s;
foo(&s);
cout << s;
delete[] s;
return 0;
}

Note:

1. Since s is a pointer, a variable storing the address of s is a pointer to
pointer. Thus there is a double asterisk in the definition of foo's
parameter. ps is a pointer to s, so *ps gives the original s. Hence *ps
replaces s in your foo function.
2. foo is called with an argument of &s rather than with an argument of s.
3. I have changed your original delete to the (correct) array form of
delete[].

The C++-style approach is to use references as follows:

void foo(char *& s)
{
s = new char[10];
strcpy(s, "gotit");
}
int main()
{
char *s;
foo(s);
cout << s;
delete[] s;
return 0;
}

This involves the least change to your code. All you need to do is replace

void foo(char *s)

with

void foo(char *& s)

i.e., you only need add the & symbol. This change means that s itself ---
and not just what it points to --- is "passed by reference" (in the C++
sense of that term). This means that foo operates on the original s --- no
local copy is made --- so that any changes that foo makes to s affect the
original.
 
D

dumboo

hi there,

John Carson said:
dumboo said:
hi there
i m little bit confused over the following problem, i have understood
wt the following code is doing...but not able to get the actual
techinical stuff.....i have had a lot of hot debate over the
following code whether its pass by reference or pass by value...hope
somebody clears it up :)
void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

int main()
{
char *s;
foo(s);
cout<<s;
delete s;

return 0;
}

regards


There is some confusion here resulting from C++'s origin in C. The C
language does not have C++-style references. Accordingly, if you want a
function in C to modify something, you need to pass it a pointer to the
thing to be modified. In C, this is called "passing by reference". Now
consider your function:

void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

In C, this would be called "passing by reference". The important thing to
note, however, is that the thing that is passed by reference is *not* the
variable s but what s points to, i.e., s is passed by value and what s
points to is passed by reference. What this means is that

1. foo receives a local copy of s and any changes that it makes to s only
affect that local copy.
2. the copy of s that foo receives points to the same memory area as the
original s did, so any changes made to what s points to have the same effect
within the function as they would have in the place from which the function
is called.

In your line:

s = new char[10];

you are changing the local copy of s, which has no effect on the original s.
Thus cout in main() is being applied to the uninitialised s.

If you want to modify s, then there are two ways to go about it. The C-style
approach is to pass the address of s rather than s itself, as follows:

void foo(char **ps)
{
*ps = new char[10];
strcpy(*ps, "gotit");
}

int main()
{
char *s;
foo(&s);
cout << s;
delete[] s;
return 0;
}

Note:

1. Since s is a pointer, a variable storing the address of s is a pointer to
pointer. Thus there is a double asterisk in the definition of foo's
parameter. ps is a pointer to s, so *ps gives the original s. Hence *ps
replaces s in your foo function.
2. foo is called with an argument of &s rather than with an argument of s.
3. I have changed your original delete to the (correct) array form of
delete[].

The C++-style approach is to use references as follows:

void foo(char *& s)
{
s = new char[10];
strcpy(s, "gotit");
}
int main()
{
char *s;
foo(s);
cout << s;
delete[] s;
return 0;
}

This involves the least change to your code. All you need to do is replace

void foo(char *s)

with

void foo(char *& s)

i.e., you only need add the & symbol. This change means that s itself ---
and not just what it points to --- is "passed by reference" (in the C++
sense of that term). This means that foo operates on the original s --- no
local copy is made --- so that any changes that foo makes to s affect the
original.

thanks all the guys for the superb answer, got many of my concepts cleared
:)
 
J

Jon Bell

The C++-style approach is to use references as follows:

void foo(char *& s)
{
s = new char[10];
strcpy(s, "gotit");
}
int main()
{
char *s;
foo(s);
cout << s;
delete[] s;
return 0;
}

Or better yet, use C++'s standard 'string' data type. Then you don't have
to bother with char pointers and new and delete at all.

#include <iostream>
#include <string>

using namespace std;

void foo (string& s)
{
s = "gotit";
}

int main ()
{
string s;
foo (s);
cout << s;
return 0;
}
 
G

Geetesh

I will like to say that references were introduced in c++ and the
prototype of the function u have given has been there since c. and the
prototype for with pass by reference is:
void foo(char *&s)
in above prototype s is passsed by refernece parameter.
regards
 
D

Default User

Jonathan said:
dumboo said:
hi there
i m little bit confused over the following problem, i have understood wt the
following code is doing...but not able to get the actual techinical
stuff.....i have had a lot of hot debate over the following code whether its
pass by reference or pass by value...hope somebody clears it up :)
void foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
}

int main()
{
char *s;
foo(s);
cout<<s;
delete s;

return 0;
}

Here you are passing a pointer by value.

Among other things, you need to delete s like so: delete [] s.

That just somewhat different undefined behavior to the undefined
behavior he already has due to his faulty allocation.

The pointer back in main is unaffected by the new over in foo(). So
deferencing it in main() via the cout is undefined. The delete is wrong,
but so is using delete[] on an uninitialized pointer.

The function foo() either needs to take a char** or return a char*.



Brian Rodenborn
 
D

Default User

John said:
If you want to modify s, then there are two ways to go about it. The C-style
approach is to pass the address of s rather than s itself, as follows:

void foo(char **ps)
{
*ps = new char[10];
strcpy(*ps, "gotit");
}
The C++-style approach is to use references as follows:

void foo(char *& s)
{
s = new char[10];
strcpy(s, "gotit");
}


A third way is to return a pointer, like so:

char* foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
return s;
}


Brian Rodenborn
 
O

Old Wolf

hi there
i m little bit confused over the following problem, i have understood wt the
following code is doing...but not able to get the actual techinical
stuff.....i have had a lot of hot debate over the following code whether its
pass by reference or pass by value...hope somebody clears it up :)

int main()
{
char *s;

`s' is an uninitialized pointer-to-char that points to...well, *anywhere*,

`foo()' is called; however as it has been passed by value it is
unchanged, hence still uninitialized.

An attempt is made to send the contents pointed to by `s' to the stream
`cout'. Since `s' points...well *anywhere*...undefined behavior is
invoked. [first time]

Second time, actually; foo(s) is UB because it evaluates s
(it could be a trap value).
 
J

John Carson

Default User said:
A third way is to return a pointer, like so:

char* foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
return s;
}

In which case no use is being made of the argument passed except as a source
of a local variable, so it would be simpler to use:

char* foo()
{
char *s = new char[10];
strcpy(s, "gotit");
return s;
}
 
S

Sharad Kala

Default User said:
John said:
If you want to modify s, then there are two ways to go about it. The C-style
approach is to pass the address of s rather than s itself, as follows:

void foo(char **ps)
{
*ps = new char[10];
strcpy(*ps, "gotit");
}
The C++-style approach is to use references as follows:

void foo(char *& s)
{
s = new char[10];
strcpy(s, "gotit");
}


A third way is to return a pointer, like so:

char* foo(char *s)
{
s = new char[10];
strcpy(s, "gotit");
return s;
}

For a simple program like this, it's fine.
But atleast in a library I would not like this approach.
This would mean that you need to tell your users that the responsibility to free
the memory is with you.
Unless one is quite careful with such libraries there are bound to be potential
memory leaks.

-Sharad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top