public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Classic C problems, need help!
@ 2002-05-03 11:44 Kári Davíðsson
  0 siblings, 0 replies; 5+ messages in thread
From: Kári Davíðsson @ 2002-05-03 11:44 UTC (permalink / raw)
  To: Glover George, John Love-Jensen; +Cc: gcc-help

You don't know.

Welcome to the world of C and C++.

K.D.

> -----Original Message-----
> From: Glover George [mailto:dime@gulfsales.com]
> Sent: 3. maí 2002 18:39
> To: 'John Love-Jensen'
> Cc: gcc-help@gcc.gnu.org
> Subject: RE: Classic C problems, need help!
> 
> 
> I guess that's the problem is how do I know?  If the prototype of the
> function is (char *someval) does that tell me anything?  I mean, other
> than well documented function headers, how do I know?
> 
> > -----Original Message-----
> > From: gcc-help-owner@gcc.gnu.org 
> > [mailto:gcc-help-owner@gcc.gnu.org] On Behalf Of John Love-Jensen
> > Sent: Friday, May 03, 2002 12:50 PM
> > To: Glover George
> > Cc: gcc-help@gcc.gnu.org
> > Subject: Re: Classic C problems, need help!
> > 
> > 
> > Hi Dime,
> > 
> > What you are running into is the concept known as OWNERSHIP.  
> > I put OWNERSHIP in capitals because it is a very important 
> > C/C++ detail to get right -- otherwise memory leaks or 
> > dangling pointers, then Bad Things Happen shortly thereafter. 
> >  Aside: Java gets around the OWNERSHIP problem by having 
> > garbage collection, pass-by-value for POD and 
> > pass-by-reference for UDT.
> > 
> > When you pass the char* to the other routine, does it's 
> > contract say that it is taking OWNERSHIP of that string?  And 
> > thus, is responsible to destruct it, when appropriate.
> > 
> > Or does that other routine merely borrow that string for 
> > whatever purposes... input only?  output only?  input/output 
> > (aka update)?  utility or functor (more so for C++ than C)?
> > 
> > Without known the contract that the routine is operating 
> > under, I cannot tell you your proper course of action.
> > 
> > --Eljay
> > 
> > 
> 
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Classic C problems, need help!
  2002-05-03 10:38 Glover George
  2002-05-03 10:50 ` John Love-Jensen
@ 2002-05-05  5:34 ` Sebastian Huber
  1 sibling, 0 replies; 5+ messages in thread
From: Sebastian Huber @ 2002-05-05  5:34 UTC (permalink / raw)
  To: gcc-help

Hello,
if you have for example a function declared as:
void f( const char*);
you can be quite sure that the function doesn't alter your string.
On the outher hand:
void g( char*);
may alter your string (modify the data, call free(), ...) that must clarify 
the documentation. If g doesn't alter the string it is a bad style to omit 
the const.

To return a string there are two possibilities:
1. The receiver has to free the memory
2. The function takes care about the memory stuff
	Example:
	const char* f()
	{
		static char* c [SIZE]'
		return c;
	}
But method 2. is not reentrant.

On Friday 03 May 2002 19:42, you wrote:
> Hello,
>
> Please forgive me if this is off topic as I don't know where to ask it.
> I am having the hardest time with this and was wondering if anyone can
> give me some tips.  I have a function that I want to perform some
> operations and return a string.  This string will never be assigned to a
> variable, merely only passed to a function.  What that function does
> with it I have no idea.
>
> So at first I thought well why not just return a char * which has been
> newed.  Well the problem with this is if I don't assign it to a
> variable, then how do I delete it?  And if I do assign it to a variable,
> and I pass the variable to the function, how do I know that if I delete
> it, the function won't go trying to use that again?  I mean, does the
> function have to actually take care of making a copy of it first?
>
> Ok, so if I choose to do this.  If I create a char array in my function,
> say char sting1[20]; and then I assign the value to it, and do a return
> string1; I know that it is out of scope once the function returns, but
> doesn't it stay alive for at least a second like when it is returned and
> passed as an unamed variable to another function?
>
> Of course I don't have this problem with returning integers, but with
> strings I don't understand.  If you return something and don't give it a
> name, such as a string, how can it be dynamic, and yet you delete it if
> you have nothing to delete it with?
>
> I know to the trained C veteran, this sounds stupid, but this is the
> only area I've struggled with in C and c++, and would hope that someone
> on here can give me some insight into this and explain the proper ways
> to return strings from a function and not have to worry about it
> dissapearing.  Thank you very much.
>
> Dime

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Classic C problems, need help!
  2002-05-03 10:50 ` John Love-Jensen
@ 2002-05-03 11:35   ` Glover George
  0 siblings, 0 replies; 5+ messages in thread
From: Glover George @ 2002-05-03 11:35 UTC (permalink / raw)
  To: 'John Love-Jensen'; +Cc: gcc-help

I guess that's the problem is how do I know?  If the prototype of the
function is (char *someval) does that tell me anything?  I mean, other
than well documented function headers, how do I know?

> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org 
> [mailto:gcc-help-owner@gcc.gnu.org] On Behalf Of John Love-Jensen
> Sent: Friday, May 03, 2002 12:50 PM
> To: Glover George
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: Classic C problems, need help!
> 
> 
> Hi Dime,
> 
> What you are running into is the concept known as OWNERSHIP.  
> I put OWNERSHIP in capitals because it is a very important 
> C/C++ detail to get right -- otherwise memory leaks or 
> dangling pointers, then Bad Things Happen shortly thereafter. 
>  Aside: Java gets around the OWNERSHIP problem by having 
> garbage collection, pass-by-value for POD and 
> pass-by-reference for UDT.
> 
> When you pass the char* to the other routine, does it's 
> contract say that it is taking OWNERSHIP of that string?  And 
> thus, is responsible to destruct it, when appropriate.
> 
> Or does that other routine merely borrow that string for 
> whatever purposes... input only?  output only?  input/output 
> (aka update)?  utility or functor (more so for C++ than C)?
> 
> Without known the contract that the routine is operating 
> under, I cannot tell you your proper course of action.
> 
> --Eljay
> 
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Classic C problems, need help!
  2002-05-03 10:38 Glover George
@ 2002-05-03 10:50 ` John Love-Jensen
  2002-05-03 11:35   ` Glover George
  2002-05-05  5:34 ` Sebastian Huber
  1 sibling, 1 reply; 5+ messages in thread
From: John Love-Jensen @ 2002-05-03 10:50 UTC (permalink / raw)
  To: Glover George; +Cc: gcc-help

Hi Dime,

What you are running into is the concept known as OWNERSHIP.  I put
OWNERSHIP in capitals because it is a very important C/C++ detail to get
right -- otherwise memory leaks or dangling pointers, then Bad Things Happen
shortly thereafter.  Aside: Java gets around the OWNERSHIP problem by having
garbage collection, pass-by-value for POD and pass-by-reference for UDT.

When you pass the char* to the other routine, does it's contract say that it
is taking OWNERSHIP of that string?  And thus, is responsible to destruct
it, when appropriate.

Or does that other routine merely borrow that string for whatever
purposes... input only?  output only?  input/output (aka update)?  utility
or functor (more so for C++ than C)?

Without known the contract that the routine is operating under, I cannot
tell you your proper course of action.

--Eljay


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Classic C problems, need help!
@ 2002-05-03 10:38 Glover George
  2002-05-03 10:50 ` John Love-Jensen
  2002-05-05  5:34 ` Sebastian Huber
  0 siblings, 2 replies; 5+ messages in thread
From: Glover George @ 2002-05-03 10:38 UTC (permalink / raw)
  To: gcc-help

Hello,

Please forgive me if this is off topic as I don't know where to ask it.
I am having the hardest time with this and was wondering if anyone can
give me some tips.  I have a function that I want to perform some
operations and return a string.  This string will never be assigned to a
variable, merely only passed to a function.  What that function does
with it I have no idea.  

So at first I thought well why not just return a char * which has been
newed.  Well the problem with this is if I don't assign it to a
variable, then how do I delete it?  And if I do assign it to a variable,
and I pass the variable to the function, how do I know that if I delete
it, the function won't go trying to use that again?  I mean, does the
function have to actually take care of making a copy of it first?

Ok, so if I choose to do this.  If I create a char array in my function,
say char sting1[20]; and then I assign the value to it, and do a return
string1; I know that it is out of scope once the function returns, but
doesn't it stay alive for at least a second like when it is returned and
passed as an unamed variable to another function? 

Of course I don't have this problem with returning integers, but with
strings I don't understand.  If you return something and don't give it a
name, such as a string, how can it be dynamic, and yet you delete it if
you have nothing to delete it with?  

I know to the trained C veteran, this sounds stupid, but this is the
only area I've struggled with in C and c++, and would hope that someone
on here can give me some insight into this and explain the proper ways
to return strings from a function and not have to worry about it
dissapearing.  Thank you very much.

Dime

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-05-05 12:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-03 11:44 Classic C problems, need help! Kári Davíðsson
  -- strict thread matches above, loose matches on Subject: below --
2002-05-03 10:38 Glover George
2002-05-03 10:50 ` John Love-Jensen
2002-05-03 11:35   ` Glover George
2002-05-05  5:34 ` Sebastian Huber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).