TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: TIKA CARR
from: DARIN MCBRIDE
date: 1998-01-23 18:10:00
subject: free(question)

 TC> Well, sometimes there's just no other way, really, as was in the case of
 TC> getting that pointer to the font table. Sometimes I 
 TC> feel as guilty as using a global variable, though.
Well, the only positive thing I can see here is that you are forced to think 
about your options.  However, when you've eliminated all the impossible  
solutions, whatever is left, however "unclean" you may think of it, is your 
answer.  Kinda a poor paraphrase, I know, but I'm just saying that when your 
solution is to use a static, use it.  Sometimes it is cleaner to use a static 
than to not.
One example of my own:
char* SomeProgramDirectory()
{
   static Dir[MAX_PATH];
   if (Dir[0] == '\0')
   {
     // haven't found it before
     // find it now.
   }
   return Dir;
}
No matter how many times I use the directory path, I only search for it once. 
 It gives me a simple "cache" for this value.
 DM> If I(the OS) moved memory around on you(the user program), what would
 DM> your pointers be good for anymore?  Everything has moved!
 TC> Good point (pun not intended) Sometimes I feel like my brain is like 
that.
 TC> But then the compiler should have code that would 
 TC> engage when a page is freed
 TC> (doesn't it trigger some kind of IRQ or call?) and then 
It does - but the system doesn't know what is a pointer and what isn't until 
it is used (and sometimes not even then!).
Note that the OS is free to move pages because pointers are actually 
page-dependant.  Using the value of the pointer, the CPU can tell which page 
to look at and uses a descriptor table to find out which real piece of memory 
it is pointing to.  As a trade-off between accuracy (and the freedom to move 
memory around easier) and speed, the hardware designers chose 2^14 bytes (4k) 
as their table size.
 DM> If there is space available, realloc *CAN* (but doesn't have to) use
 DM> it to grow.  When it shrinks, it can *NOT* move 
 DM> other memory around to
 DM> use up the extra space created.
 DM> What other questions?  Oh, and I try to avoid realloc at almost all
 DM> costs. It's worse than goto in my books.  :-)
 TC> GOTO! Yuk  don't say that *word* (it's even taboo in BASIC 
hese
 TC> days!) It can't be that* bad? Maybe realloc can be 
 TC> compared to using global
 TC> variables for 5% of your code, but not as bad coding as gotos! ;)
Used "normally", realloc is quite dangerous.  It isn't even "present" in the 
C++ style of memory allocation (new/delete).
1. if you use it as p = realloc(p, new_size), and there isn't enough room for 
new_size, realloc returns NULL, deleting your pointer to p.  However, the 
original memory is still valid - memory leak!
2. what happens when you have pointers into the memory when it moves due to 
growing?  They become invalid.
Both of these can be dealt with in proper defensive programming, but they are 
such minor details that don't really hit most programmers.
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

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