| View previous topic :: View next topic |
| Author |
Message |
Massimiliano Alberti Guest
|
Posted: Wed Feb 25, 2004 6:25 pm Post subject: const |
|
|
Sometimes I've seen something like:
const char* ReturnNull()
{
return "";
}
Is this legal? Why? Is this because the string is a constant?
And... Is this legal:
const int* ReturnZero()
{
const int i = 0;
return &i;
}
|
|
| Back to top |
|
 |
Massimiliano Alberti Guest
|
Posted: Wed Feb 25, 2004 6:29 pm Post subject: Re: const |
|
|
| Quote: | const char* ReturnNull()
{
return "";
}
|
Wrong example:
const char* ReturnNull()
{
const char *p = "";
return p;
}
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Feb 25, 2004 6:42 pm Post subject: Re: const |
|
|
"Massimiliano Alberti" <xanatos (AT) geocities (DOT) com> wrote
| Quote: | Sometimes I've seen something like:
const char* ReturnNull()
{
return "";
}
|
This is legal because a string literal is persistant. It's a bunch of const chars
at an undetermined location. They are not local to the block.
| Quote: | const int* ReturnZero()
{
const int i = 0;
return &i;
}
|
This will compile, but any attempt to use the returned pointer will be problematic
as in this case the value is a pointer to an object that doesn't exist anymore.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Feb 25, 2004 8:05 pm Post subject: Re: const |
|
|
Massimiliano Alberti wrote:
| Quote: | Sometimes I've seen something like:
const char* ReturnNull()
{
return "";
}
Is this legal?
|
Yes.
What makes you think it's not?
| Quote: | Is this because the string is a constant?
|
No, it's because it's a literal, and string literals exist through the
whole life time of the program.
| Quote: | And... Is this legal:
const int* ReturnZero()
{
const int i = 0;
return &i;
}
|
No. You're returning a pointer to a local variable that gets destroyed
when the function returns.
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Wed Feb 25, 2004 10:01 pm Post subject: Re: const |
|
|
On Wed, 25 Feb 2004 13:42:35 -0500, "Ron Natalie" <ron (AT) sensor (DOT) com> wrote:
| Quote: |
const int* ReturnZero()
{
const int i = 0;
return &i;
}
This will compile, but any attempt to use the returned pointer will be problematic
as in this case the value is a pointer to an object that doesn't exist anymore.
|
It will compile, but every self-respecting compiler will at least issue a
warning about it. If you don't see a warning, consider getting a newer
compiler (or tightening up the warning level options).
-leor
Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
|
|
| Back to top |
|
 |
Nick Hounsome Guest
|
Posted: Thu Feb 26, 2004 7:38 am Post subject: Re: const |
|
|
"Massimiliano Alberti" <xanatos (AT) geocities (DOT) com> wrote
| Quote: | const char* ReturnNull()
{
return "";
}
Wrong example:
const char* ReturnNull()
{
const char *p = "";
return p;
}
|
No - his example is fine - yours just wastes a bit of stack space to acheive
exactly
the same thing.
Actually according to the principle of calling it what it is it should be
const char* ReturnNull() { return NULL; }
or
const char* ReturnEmptyString() { return ""; }
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Feb 26, 2004 10:48 am Post subject: Re: const |
|
|
Nick Hounsome wrote:
| Quote: |
"Massimiliano Alberti" <xanatos (AT) geocities (DOT) com> wrote in message
news:8Q5%b.21112$gk.900093 (AT) news3 (DOT) tin.it...
const char* ReturnNull()
{
return "";
}
Wrong example:
const char* ReturnNull()
{
const char *p = "";
return p;
}
No - his example is fine - yours just wastes a bit of stack space to
acheive exactly the same thing.
|
The "his" and the "yours" you are talking about are the same person.
|
|
| Back to top |
|
 |
Roger Leigh Guest
|
Posted: Wed Mar 03, 2004 7:26 pm Post subject: Re: const |
|
|
"Massimiliano Alberti" <xanatos (AT) geocities (DOT) com> writes:
| Quote: | Sometimes I've seen something like:
const char* ReturnNull()
{
return "";
}
Is this legal? Why? Is this because the string is a constant?
|
Yes.
[OT: On a UNIX-like OS (e.g. Linux/ELF), this will probably be put in
the .rodata (read-only data) section of the executable. This part is
mapped into memory with read-only permissions, so if you actually
attempt to modify it, you'll get a SEGV.]
| Quote: | And... Is this legal:
const int* ReturnZero()
{
const int i = 0;
return &i;
}
|
It's "legal" in that it will compile. However, it's wrong because i
will be destroyed when it goes out of scope when the function returns.
This will leave you with a pointer to a nonexistent object, and
probably a crash.
--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
|
|
| Back to top |
|
 |
|