const

  • Thread starter Massimiliano Alberti
  • Start date
M

Massimiliano Alberti

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

Ron Natalie

Massimiliano Alberti said:
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.
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.
 
R

Rolf Magnus

Massimiliano said:
Sometimes I've seen something like:

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

Is this legal?
Yes.

Why?

What makes you think it's not?
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.
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.
 
L

Leor Zolman

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
(e-mail address removed)
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
 
N

Nick Hounsome

Massimiliano Alberti said:
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 ""; }
 
R

Rolf Magnus

Nick said:
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.
 
R

Roger Leigh

Massimiliano Alberti said:
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.]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top