🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.next.programmer
2 messages
2 total messages Started by Art Isbell Tue, 04 Aug 1992 17:44
How are string constants stored in Objective-C?
#440
Author: Art Isbell
Date: Tue, 04 Aug 1992 17:44
62 lines
3503 bytes

I've never been completely clear about how ANSI-C (and, hence, Objective-C)
stores string constants.  I'm sure the amount of memory consumed by strings in
the typical NeXT app is a mere drop in the bucket, but I'm still curious about
the "proper" way to allocate string constants.

const char *aStringPtr = "string";
const char aString[] = "string";

I've assumed that the memory referenced by aStringPtr is in some read-only heap
area.  When aStringPtr is no longer needed, can this memory be freed with
free(aStringPtr)?

I've assumed that aString is a character array that's allocated on the stack if
aString is a local variable, so its memory is freed once the block in which
it's defined is exited.

But where do NXUniqueString() and NXUniqueStringNoCopy() fit into this scheme?
If I say

	NXAtom anAtom = NXUniqueString("string");
	NXAtom anotherAtom = NXUniqueStringNoCopy("string");

does "anAtom" point to a hashed copy of "string" so that memory is allocated in
2 different places for "string" whereas "anotherAtom" points to only a hashed
pointer to the heap memory where "string" is found?  Should unique strings be
used only for strings that are repeated throughout an app?  I guess I can avoid
defining a global variable by using

	NXAtom anAtom = NXUniqueString("string");

repeatedly because "string" is already in the unique string hash table, but
won't C allocate additional heap memory for the "string" argument of each
NXUniqueString() call?  If so, I guess another approach would be

	NXAtom anAtom = NXUniqueString(aString);

where "aString" is a local character array.

Pretty dumb, insignificant questions in the universe of NeXTSTEP questions
(probably would fail AFS's test for the programmer FAQ :-), but someone at NeXT
thought that unique strings were worth the effort of writing several functions
to deal with them.  I realize another advantage is quicker string comparisons
using unique strings, but I doubt that defining a pair of unique strings is
more efficient that just comparing them using NXOrderStrings().

Another implication of all this is seen when using MallocDebug.  For instance,
Storage's "free" does not free the type encoding string, so does this show up
as a memory leak in MallocDebug if a string constant is used as the type
encoding string?  So is making the type encoding string a local char array the
proper approach?  Pretty nit picky, but I'd like to eliminate all this
relatively minor cruft in the MallocDebug memory leak output (if it's actually
there - it's been a while since I've used MallocDebug).

Thanks for your comments and clarifications.
-- 

Art Isbell                                                      Cubic Solutions
NeXT Registered Developer #745         NeXT software development and consulting
Email: isbell@cats.UCSC.EDU                                Voice: (408)335-1154
USmail: 95018-9442                                           Fax: (408)335-2515
Re: How are string constants stored in Objective-C?
#446
Author: Jim Large
Date: Wed, 05 Aug 1992 03:24
76 lines
3654 bytes
In article <15mfliINNj9a@darkstar.UCSC.EDU> isbell@cats.ucsc.edu (Art Isbell)  
writes:
> const char *aStringPtr = "string";
> const char aString[] = "string";

The first declaration defines a NON-CONSTANT pointer var which INITIALLY
points to a constant string.  Total storage is 4 bytes more than for the
second case which defines a constant array.

On the NeXT, both the constant string and the constant array are stored in
memory which is actually write protected (and also shared if multiple copies
of the same program are executed simultaneously on the same machine).



> I've assumed that the memory referenced by aStringPtr is in some read-only
> heap area.  When aStringPtr is no longer needed, can this memory be freed
> with free(aStringPtr)?

NO!  The constant string is not allocated from a heap.  You can't free it,
and you can't ever change its contents.  (It really is a CONSTANT.)



> I've assumed that aString is a character array that's allocated on the stack 
> if aString is a local variable, so its memory is freed once the block in
> which it's defined is exited.

ALMOST true.  If aString is an "automatic" variable (i.e., not global or
static), then it is allocated on the stack.  But the NeXT compiler also creates
a static string, and initializes the array by COPYING the static string into it 
each time the function is called.



> If I say
> 	NXAtom anAtom = NXUniqueString("string");
> 	NXAtom anotherAtom = NXUniqueStringNoCopy("string");
> does "anAtom" point to a hashed copy of "string" so that memory is allocated  
> in 2 different places for "string"[?]

YES.


> [does] "anotherAtom" point to a hashed pointer to the heap memory 
> where "string" is found?

Well, that depends.  anotherAtom will *NEVER* point to "heap" memory (i.e.,
memory you can "free()"), but whether or not it points to the original string
or a copy of it depends on whether a copy of it already existed in the hash
table.  When you call NXWhateverNoCopy(foo), you are promising that you will
never change or free() the thing that foo points to.  The library may OR MAY 
NOT act more efficiently because of your promise.


> Should unique strings be used only for strings that are repeated throughout
> an app?

*THE* reason for using "unique strings" is that you can test them for equality
by comparing POINTERS instead of comparing byte-by-byte a la strcmp().  Several 
AppKit facilities (e.g., named image-reps and named sounds) make use of
this feature. 


> Storage's "free" does not free the type encoding string, so does this show up
> as a memory leak in MallocDebug?

Maybe you should try it for yourself and see what happens. :-)

HINT:  The Storage object remembers the pointer you give it, but it 
doesn't ever copy the string.

----------------+------------------+------------------------------------
     |\ /|      |  Jim Large       |  Visual Understanding Systems, Inc.
     | V |      |  jml@visus.com   |  2100 Wharton St.                  
   \_|   |__    |  (412)488-3600   |  Pittsburgh, PA  15203             
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads