is a char array created in a function local?

J

Jess

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
 
A

Amal P

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
 
K

kmoving

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.
 
G

Guest

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.
 
D

Default User

Jess said:
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
 
J

John Harrison

Jess said:
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
 
J

Jess

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
 
J

James Kanze

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.)
 
R

Robert Bauck Hamar

Jess said:
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.
 
J

Jess

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:[email protected]
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


I see, thanks a lot!
Jess
 
J

Jack Klein

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
 

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

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,163
Latest member
NicolasMcL

Latest Threads

Top