Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!tut.cis.ohio-state.edu!ucsd!usc!apple!agate!e260-3f!c60c-3cf From: c60c-3cf@e260-3f.berkeley.edu (Dan Kogai) Newsgroups: comp.sys.mac.programmer Subject: Re: global data in Think C Message-ID: <1990May8.024609.13256@agate.berkeley.edu> Date: 8 May 90 02:46:09 GMT References: <704@dimebox.cs.utexas.edu> Sender: usenet@agate.berkeley.edu (USENET Administrator;;;;ZU44) Organization: UC Bezerkeley Lines: 52 In article <704@dimebox.cs.utexas.edu> ted@cs.utexas.edu (Ted Woodward) writes: >Well, I just found out that string constants in Think C 4.0 get put into the >global data segment...bad move, guys. Because of this, I have 47K of 'global' >data... >So what do I try? I try this: >#define NUMELEMS 29 >Str32 *myArray; /*yes, I did a #include */ >InitElms() >{ myArray = (Str32 *) NewPtr(sizeof(Str32) * (long) NUMELMS); > myArray[0] = "generic text here"; ^^^^^^^^^^^^^^^^^^Problem >} > >and get 'illegal operation on array'. So I can't say: >Str32 test; >test = "stuff"; ^^^^^Problem #2' >This seems kinda silly. I'd also like to be able to say something like this: >myArray = {"generic1","genereic2",...} ^^^^^^^^^^^^^^^^^^^^^Problem >like in a declaration, so I can put the stuff in the heap instead of global >space... Okay, I grokked your problem enough so let me explain. You keep forgetting Ptr32 is pascal string! You have to put "\p" in case of pascal string. And you should use pStrCopy() to copy it down: String assignment cannot be done with "=" in C. Second example is fine except you forgot "\p". And you also forgot to check null pointer in case NewPtr() couldn't find enough space. Plus since you are using NUMELEM as CONSTANT size of array, why do you have to bother allocating it dynamically? Dynamic allocation is necessary only when the size of array is variant. In your case you don't even have to bother writhing InitElms() and it's as simple as: Str32 myarray[NUMELEM]={ "\pgeneric string here" } Personally I hate pascal string so I store all string in C convention and convert it to pascal whenever I need to use pascal string. --- ################## Dan The "Think C fan" Man + ____ __ __ + (Aka Dan Kogai) + ||__||__| + E-mail: dankg@ocf.berkeley.edu + ____| ______ + Voice: 415-549-6111 + | |__|__| + USnail: 1730 Laloma Berkeley, CA 94709 + |___ |__|__| + U.S.A + |____|____ + Disclaimer: I'd rather be blackmailed for my long .sig + \_| | + than give up my cool name in Kanji. And my + <- THE MAN -> + citizenship of People's Republic o' Berkeley ################## has nothing 2 do w/ whatever I post, ThanQ.