Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!mcgill-vision!snorkelwacker!usc!zaphod.mps.ohio-state.edu!uwm.edu!psuvax1!rutgers!mcnc!kk From: kk@mcnc.org (Krzysztof Kozminski) Newsgroups: comp.sys.mac.programmer Subject: Re: global data in Think C Message-ID: <2069@speedy.mcnc.org> Date: 7 May 90 18:47:48 GMT References: <704@dimebox.cs.utexas.edu> Reply-To: kk@mcnc.org.UUCP (Krzysztof Kozminski) Organization: MCNC; RTP, NC Lines: 62 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... What are your strings doing in the program code instead of being stored in resources (so that you can change them without recompiling) ? The only strings that have a good reason to be in the code are those used for debugging (hopefully enclosed in #ifdef's so that you can get rid of them in the final version). >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"; >} Where do you think the string "generic text here" will get put by the compiler? Yup, right into the global data segment. The size of your data segment would get reduced only if you had some redundant strings (provided that the compiler is not smart enough to do this for you). >and get 'illegal operation on array'. So I can't say: >Str32 test; >test = "stuff"; > >This seems kinda silly. Lets see, 'test' is an array of 32 charcters. "stuff" is a POINTER to the location where character 's' is stored, followed by 'tuff' and '\0'. You're assigning a wrong entity here. What you have requires string copy, NOT assignment. "strcpy(myArray[0],"generic text here")" "strcpy(test,"stuff"); This is rather silly, since it wastes lots of space (all this stuff trailing the strings that are shorter than 31 characters). Or you can do: char **myArray; ... myArray = char **NewPtr(sizeof(*myArray) *(long) NUMELMS); myArray[0] = "generic text here"; except that this is sorta silly, too, since you can just do the initialization: char *myArray[] = { "generic text here", ... that acompplishes exactly the same thing with less hassle ... The bottom line: put the strings in 'STR#'. KK -- Kris Kozminski kk@mcnc.org "The party was a masquerade; the guests were all wearing their faces."