 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kenneth Kasajian Guest
|
Posted: Sun Mar 20, 2005 11:42 pm Post subject: When is returning a pointer to a stack-allocated variable ok |
|
|
example code:
char *r = get();
char* get()
{
char buf[16];
....
return buf
}
On the Microsoft compiler I'm using, it only warns me. Anyone know the
reason why C does not make this a compiler error?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Mon Mar 21, 2005 7:02 am Post subject: Re: When is returning a pointer to a stack-allocated variabl |
|
|
On 20 Mar 2005 18:42:19 -0500, "Kenneth Kasajian" <ken (AT) kasajian (DOT) com>
wrote in comp.lang.c++.moderated:
| Quote: | example code:
char *r = get();
char* get()
{
char buf[16];
....
return buf
}
On the Microsoft compiler I'm using, it only warns me. Anyone know the
reason why C does not make this a compiler error?
|
If your question is about C, you should ask it in comp.lang.c or
comp.lang.c moderated.
As to why C++ does not make it an error, there is no problem unless
the caller actually does something, pretty much anything at all, with
the returned value.
On a higher level, this is fairly straight forward example of
undefined behavior. A compiler is never required to issue a
diagnostic for undefined behavior. It is always easy to produce
simple code snippets where undefined behavior is right out in the open
and easy to see. But there are many, many more ways to produce
undefined behavior in a program that are much more difficult for a
human being to see, and can't actually be identified until the code is
executed.
Most compilers, probably including yours, have options to issue
warning messages that are not required by the language standard. Have
you tried this?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Mar 21, 2005 9:53 pm Post subject: Re: When is returning a pointer to a stack-allocated variabl |
|
|
Kenneth Kasajian wrote:
| Quote: | example code:
char *r = get();
char* get()
{
char buf[16];
....
return buf
}
On the Microsoft compiler I'm using, it only warns me. Anyone
know the reason why C does not make this a compiler error?
|
Because it's not easily detectable in the general case.
Consider something like:
char*
get()
{
char buffer[ L_tmpnam ] ;
return tmpnam( buffer ) ;
}
Compilers are free to warn in the cases they detect, but
requiring an error would require compilers to be able to "look
into" functions in separate compilation units, possibly even
functions for which no source code is available.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Mon Mar 21, 2005 9:55 pm Post subject: Re: When is returning a pointer to a stack-allocated variabl |
|
|
Kenneth Kasajian wrote:
| Quote: | On the Microsoft compiler I'm using, it only warns me. Anyone know the
reason why C does not make this a compiler error?
Because the language designers didn't want to add the overhead to |
make the compiler keep track of what memory returned pointers are
allocated from. It's fairly obvious here but if you were to take
buf's address and stick it in a pointer and do some operations with
it and return it, then you've probably had to add some special
overhead to determine you're returning a pointer to memory that's
about to become invalid.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Mon Mar 21, 2005 10:07 pm Post subject: Re: When is returning a pointer to a stack-allocated variabl |
|
|
Kenneth Kasajian wrote:
| Quote: | example code:
char *r = get();
char* get()
{
char buf[16];
....
return buf
}
On the Microsoft compiler I'm using, it only warns me. Anyone know the
reason why C does not make this a compiler error?
|
Keep in mind that this is the easy case to recognize. There are harder ones:
char *f(char *ptr);
char *get()
{
char buf[16];
return f(ptr);
}
Does this version of get return a pointer to a local object?
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|