| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | How is memory handled? |
Hello Neil!
Replying to a message of Neil Heller to Darin McBride:
NH>> The targets of the pointers still exist ... but where? Do I
NH>> need to worry about "free"ing or
"delete"ing the memory?
DM>> No. You didn't malloc/strdup/call any function to allocate
DM>> the memory, so neither should you (or can you!) free/call
DM>> any function to de-allocate the memory.
NH> Is there any chance of having a memory leak when using
NH> char *foo "For whom the bell tolls";
char *foo = "For whom the tell bolls";
No. If you try to confuse it, it won't work.
void a1()
{
char *foo = "here I am";
printf("a1 -> [%p] (%p} \n", foo, &foo);
foo = "now there";
}
void a2()
{
int useless;
a1();
}
void a3()
{
float more_useless;
a2();
a1();
}
int main()
{
a1(); a2(); a3();
}
Each time should show foo being the same [pointer], but will itself be
located in a different (memory address). That's because it's pointing to
static, read-only, protected, and possibly shared, memory.
Note that even if it is shared, multiple processes would be able to give
different addresses since all memory is virtualised anyway.
There is no memory leak in the above program since there is no
"manual" memory allocation.
Darin
---
* Origin: Tanktalus' Tower BBS (1:250/102)SEEN-BY: 633/267 270 @PATH: 250/102 99 10/345 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™.