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
Jul 22 '05
14 3097
Jonathan Turkanis wrote: "dumboo" <vt***@yahoo.co m> wrote in message news:c0******** *****@ID-211285.news.uni-berlin.de... 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
John Carson wrote: 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
> > 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(s);
`foo()' is called; however as it has been passed by value it is unchanged, hence still uninitialized.
cout<<s;
An attempt is made to send the contents pointed to by `s' to the stream `cout'. Since `s' points...well *anywhere*...un defined behavior is invoked. [first time]
Second time, actually; foo(s) is UB because it evaluates s
(it could be a trap value).
"Default User" <fi********@boe ing.com.invalid > wrote in message
news:40******** *******@boeing. com.invalid 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;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
"Default User" <fi********@boe ing.com.invalid > wrote in message
news:40******** *******@boeing. com.invalid... John Carson wrote:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Seeker |
last post by:
Newbie question here...
I have a form with some radio buttons. To verify that at least one of the
buttons was chosen I use the following code ("f" is my form object) :
var btnChosen;
for (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons.checked)) {
btnChosen = true;
}
|
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object must be an object
instead of
|
by: z_learning_tester |
last post by:
I'm reading the MS press C# book and there seems to be a contradiction.
Please tell me which one is correct, 1 or 2.
Thanks!
Jeff
1. First it gives the code below saying that it prints 0 then 42.
They say that 42 is printed the second time since the value was wrapped in a
class and therefore became passed by reference.
(sorry for any typos I am a newbie here ;-)
|
by: David++ |
last post by:
Hi folks,
I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.
The thing that troubles me about pass-by-reference into a WebService is that
essentially we are passing an address of an object which resides on the
'local machine' i.e. a local machine object address. Surely when the
WebService method is called and run 'on the server', the reference type will
be...
|
by: Jon Slaughter |
last post by:
I'm reading a book on C# and it says there are 4 ways of passing types:
1. Pass value type by value
2. Pass value type by reference
3. Pass reference by value
4. Pass reference by reference.
My interpretation:
1. Essentially pushes the value type on the stack
| |
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont...
|
by: kinaxx |
last post by:
Hello,
now I'm learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism
1) pass by value (inother words : call by value)
2) pass by reference (inother words: call by reference)
3) pass by value-result <- i have question this subject .
4) pass by name
|
by: Robert Dailey |
last post by:
Hi,
I noticed in Python all function parameters seem to be passed by
reference. This means that when I modify the value of a variable of a
function, the value of the variable externally from the function is
also modified.
Sometimes I wish to work with "copies", in that when I pass in an
integer variable into a function, I want the function to be modifying
a COPY, not the reference. Is this possible?
|
by: lisp9000 |
last post by:
I've read that C allows two ways to pass information between
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.
Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I'm sure, but just didn't think this was my
problem; LOL, I am needling him)
If...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |