| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Jess" |
| Date: |
28 Jun 2007 10:54:55 PM |
| Object: |
is a char array created in a function local? |
Hello,
If a function that returns an array of char like this one:
const char* f(){
return "abc";
}
then is the char array "abc" local/temporary object? I thought it
should be temporary and hence the returned pointer should point to an
undefined memory. However, a test showed I was wrong
int main(){
const char* p = f();
cout << p << endl;
}
it outputs "abc". Is the "abc" in "f" non-local?
Thanks,
Jess
.
|
|
| User: "Default User" |
|
| Title: Re: is a char array created in a function local? |
28 Jun 2007 11:57:46 PM |
|
|
Jess wrote:
Hello,
If a function that returns an array of char like this one:
const char* f(){
return "abc";
}
then is the char array "abc" local/temporary object?
The others already told you that it wasn't
I thought it
should be temporary and hence the returned pointer should point to an
undefined memory. However, a test showed I was wrong
Now this:
const char* f(){
char arr[] = "abc";
return arr;
}
Would be a problem.
Brian
.
|
|
|
| User: "Jess" |
|
| Title: Re: is a char array created in a function local? |
29 Jun 2007 06:32:18 AM |
|
|
Thanks a lot to all your responses!
Does it mean *all* literals of types int, double, char array etc
defined in a local scope (such as within a function) are static? If
the function "f" is
int* f(){
return 10;
}
then 10 is always in the memory and is statically allocated?
Thanks,
Jess
.
|
|
|
| User: "Robert Bauck Hamar" |
|
| Title: Re: is a char array created in a function local? |
29 Jun 2007 06:49:50 AM |
|
|
Jess wrote:
Thanks a lot to all your responses!
Does it mean *all* literals of types int, double, char array etc
defined in a local scope (such as within a function) are static?
No. Only string literals and wide string literals are.
If the function "f" is
int* f(){
return 10;
}
then 10 is always in the memory and is statically allocated?
No. And this example will not even compile.
--
rbh
.
|
|
|
|
| User: "James Kanze" |
|
| Title: Re: is a char array created in a function local? |
29 Jun 2007 06:48:19 AM |
|
|
On Jun 29, 1:32 pm, Jess <w...@hotmail.com> wrote:
Does it mean *all* literals of types int, double, char array etc
defined in a local scope (such as within a function) are static? If
the function "f" is
int* f(){
return 10;
}
then 10 is always in the memory and is statically allocated?
No. 10 is a value. (Remember our discussions concerning
lvalues and rvalues?) It just is; it isn't anywhere in
particular. (On my machine, the compiler will put the 10 in
register i0; on an Intel, probably in EAX.) In other contexts,
the 10 will be embedded in a machine instruction.
String literals are a bit special, since they are the only
literals which are lvalues, and thus, which have an address and
occupy memory. Thus, if I write "abc" in a program, I have no
problem getting the address of the 'a'; if I write 10, there is
almost no way of getting the address of the 10. (I can bind it
to an "int const&", and then take the address of that. Formally,
however, this is not the address of the 10, but of a compiler
generated unnamed constant which was initialized with the value
10. But practically, I don't see any real difference.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
| User: "Jess" |
|
| Title: Re: is a char array created in a function local? |
29 Jun 2007 08:31:10 AM |
|
|
On Jun 29, 9:48 pm, James Kanze <james.ka...@gmail.com> wrote:
On Jun 29, 1:32 pm, Jess <w...@hotmail.com> wrote:
Does it mean *all* literals of types int, double, char array etc
defined in a local scope (such as within a function) are static? If
the function "f" is
int* f(){
return 10;
}
then 10 is always in the memory and is statically allocated?
No. 10 is a value. (Remember our discussions concerning
lvalues and rvalues?) It just is; it isn't anywhere in
particular. (On my machine, the compiler will put the 10 in
register i0; on an Intel, probably in EAX.) In other contexts,
the 10 will be embedded in a machine instruction.
String literals are a bit special, since they are the only
literals which are lvalues, and thus, which have an address and
occupy memory. Thus, if I write "abc" in a program, I have no
problem getting the address of the 'a'; if I write 10, there is
almost no way of getting the address of the 10. (I can bind it
to an "int const&", and then take the address of that. Formally,
however, this is not the address of the 10, but of a compiler
generated unnamed constant which was initialized with the value
10. But practically, I don't see any real difference.)
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
I see, thanks a lot!
Jess
.
|
|
|
|
|
|
|
| User: "John Harrison" |
|
| Title: Re: is a char array created in a function local? |
29 Jun 2007 01:12:01 AM |
|
|
Jess wrote:
Hello,
If a function that returns an array of char like this one:
const char* f(){
return "abc";
}
then is the char array "abc" local/temporary object? I thought it
should be temporary and hence the returned pointer should point to an
undefined memory. However, a test showed I was wrong
int main(){
const char* p = f();
cout << p << endl;
}
it outputs "abc". Is the "abc" in "f" non-local?
Thanks,
Jess
It's global as others have told you. Only one thing to add, ironically
your test has not told you anything. Even if the array had been a local
object it still might have printed correctly. Accessing an invalid
object is undefined behaviour, which means your programs behaviour is
undefined, it doesn't mean your program will not work.
john
.
|
|
|
|
| User: "" |
|
| Title: Re: is a char array created in a function local? |
28 Jun 2007 11:27:16 PM |
|
|
On 6 29 , 11 54 , Jess <w...@hotmail.com> wrote:
Hello,
If a function that returns an array of char like this one:
No,it only return the address of "abc".
const char* f(){
return "abc";
}
then is the char array "abc" local/temporary object? I thought it
should be temporary and hence the returned pointer should point to an
undefined memory. However, a test showed I was wrong
"abc" is a string literal,so it will be placed in the *static
memory*,which
does not release until your program exits.
int main(){
const char* p = f();
cout << p << endl;
}
it outputs "abc". Is the "abc" in "f" non-local?
Thanks,
Jess
.
|
|
|
|
| User: "" |
|
| Title: Re: is a char array created in a function local? |
28 Jun 2007 11:36:20 PM |
|
|
On Jun 29, 11:54 am, Jess <w...@hotmail.com> wrote:
Hello,
If a function that returns an array of char like this one:
const char* f(){
return "abc";
}
then is the char array "abc" local/temporary object? I thought it
should be temporary and hence the returned pointer should point to an
undefined memory. However, a test showed I was wrong
int main(){
const char* p = f();
cout << p << endl;
}
it outputs "abc". Is the "abc" in "f" non-local?
Thanks,
Jess
return "abc"; returns the address of "abc", and "abc" is gloable and
allocated when compiling. It's accessable util your program exits.
.
|
|
|
| User: "Jack Klein" |
|
| Title: Re: is a char array created in a function local? |
29 Jun 2007 10:14:20 PM |
|
|
On Fri, 29 Jun 2007 04:36:20 -0000, <zouy02@gmail.com> wrote in
comp.lang.c++:
On Jun 29, 11:54 am, Jess <w...@hotmail.com> wrote:
Hello,
If a function that returns an array of char like this one:
const char* f(){
return "abc";
}
then is the char array "abc" local/temporary object? I thought it
should be temporary and hence the returned pointer should point to an
undefined memory. However, a test showed I was wrong
int main(){
const char* p = f();
cout << p << endl;
}
it outputs "abc". Is the "abc" in "f" non-local?
Thanks,
Jess
return "abc"; returns the address of "abc", and "abc" is gloable and
allocated when compiling. It's accessable util your program exits.
Assuming you meant "global" and not "gloable", a term not defined by
C++, this part of your answer is incorrect under the normal meaning of
the term.
The string literal "abc" is not global, objects never are. Symbols,
the names of objects, may be "global", if they have external linkage.
The string literal has no name and no linkage.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.
|
|
|
|
|
| User: "Amal P" |
|
| Title: Re: is a char array created in a function local? |
28 Jun 2007 11:26:34 PM |
|
|
On Jun 29, 12:54 pm, Jess <w...@hotmail.com> wrote:
Hello,
If a function that returns an array of char like this one:
const char* f(){
return "abc";
}
then is the char array "abc" local/temporary object? I thought it
should be temporary and hence the returned pointer should point to an
undefined memory. However, a test showed I was wrong
int main(){
const char* p = f();
cout << p << endl;
}
it outputs "abc". Is the "abc" in "f" non-local?
Thanks,
Jess
Hi,
The memory will not be created in the local scope. The abc will be
put in a memory and that memory location will be returned by the
function. It will not be done in local scope.
For example as per your code the abc will be written to memory. And
from the main() function this address will be assigned to p.
Thanks and regards,
Amal P
.
|
|
|
|

|
Related Articles |
|
|