TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Neil Heller
from: Jasen Betts
date: 2003-02-09 22:14:30
subject: How is memory handled?

Hi Neil.

08-Feb-03 12:04:04, Neil Heller wrote to Jasen Betts

 NH> I just realized that my original post would have been correct had
 NH> I made the variable:

 NH> char * foo[] = {"For", "whom", "the",
"bell", "tolls");

which gets you an array of pointers to constant strings.
the array appears on the stack. the strings stay where they are with the
other literal strings constants used in the program.

 NH> The fun comes in rearranging the words yet keeping the meaning the
 NH> same (although breaking a few rules of English grammar).

:)

 NH> The question with which I am interested is what happens to the
 NH> memory used by the array after foo* goes out of scope?

the array of pointers is discarded like any mundane local variable,
the targets of the pointers still exist because they are string constants.

a further comment:

how will you code know how many elements are in foo[]?

additing a NULL entry to mark the end of the array may be handy:

 char * foo[] = {"For", "whom", "the",
"bell", "tolls" , NULL);

OTOH if foo[] is local you can get a count of the elements with the
expression   ( sizeof(foo) / siezof(foo*) )   that computation is done at
compile time, and will onty work with arrays (not pointers to arrays, or
references in c++)

 -=> Bye <=-

---
* Origin: I'm pink, therefore I'm SPAM. (3:640/1042)
SEEN-BY: 633/267 270
@PATH: 640/1042 531 954 774/605 123/500 106/1 379/1 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.