C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Massimiliano Alberti
Guest





PostPosted: Wed Feb 25, 2004 6:25 pm    Post subject: const Reply with quote



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





PostPosted: Wed Feb 25, 2004 6:29 pm    Post subject: Re: const Reply with quote



Quote:
const char* ReturnNull()
{
return "";
}

Wrong example:

const char* ReturnNull()
{
const char *p = "";
return p;
}



Back to top
Ron Natalie
Guest





PostPosted: Wed Feb 25, 2004 6:42 pm    Post subject: Re: const Reply with quote




"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





PostPosted: Wed Feb 25, 2004 8:05 pm    Post subject: Re: const Reply with quote

Massimiliano Alberti wrote:

Quote:
Sometimes I've seen something like:

const char* ReturnNull()
{
return "";
}

Is this legal?

Yes.

Quote:
Why?

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





PostPosted: Wed Feb 25, 2004 10:01 pm    Post subject: Re: const Reply with quote

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





PostPosted: Thu Feb 26, 2004 7:38 am    Post subject: Re: const Reply with quote


"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 ""; }

Quote:





Back to top
Rolf Magnus
Guest





PostPosted: Thu Feb 26, 2004 10:48 am    Post subject: Re: const Reply with quote

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





PostPosted: Wed Mar 03, 2004 7:26 pm    Post subject: Re: const Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.