C - Freeing resources


 
Thread Tools Search this Thread
Top Forums Programming C - Freeing resources
# 1  
Old 04-13-2009
C - Freeing resources

I was wondering what is the function in C to free a resource(usually a variable)

I know in C# there a Garbage collector, but in c and C++ there are none.
I believe in c++ the function is free([resource name here]);
# 2  
Old 04-13-2009
If it's dynamically allocated (via malloc(3) in C or new in C++) you can free(3) it. But it's not garbage collection per se, as you'll have to do it yourself.
# 3  
Old 04-13-2009
so if I do say
Code:
int test;

should I have a?
Code:
free(test);

or is it for when you create pointers?
Code:
malloc(*pointer);

and how would you go about disposing of the freed memory, it's managed by the O/S ? or you have to manually write "null" to that memory address ?
# 4  
Old 04-13-2009
You can't free an statically allocated variable (like your int test above), not in C nor Java nor C# or any other language. It's always valid until execution leaves the variables scope (usually the containing block or when an object is destroyed).

With malloc(3) you can allocate memory for an array or structure, which you can access via a pointer afterward. When you're done, use free(3) to return that memory to OS control. It's basically "ask the OS for control of a memory segment and return that control afterwards", no need to dispose of anything.

In C++ you'll use new and delete instead of malloc and free.
# 5  
Old 04-13-2009
so it's essentially only if you want to reserve a segment of memory(to assure that it's always there) and to access via pointer later(not that I like using pointers)?

So the int would be freed after it hits the end of the block?
Code:
main
{
}<--would be freed here

# 6  
Old 04-13-2009
Quote:
Originally Posted by james2432
so it's essentially only if you want to reserve a segment of memory(to assure that it's always there)
Um, no. Statically allocated memory is always there, guaranteed (otherwise the program won't even start). Via malloc you ask the OS for some memory. If you get it, it's yours until you return it. If you don't your program has to cope with that (either complain to the user and/or try again later). It's the same in C#, if the VM doesn't have any memory left for objects you can't create them.
Quote:
Originally Posted by james2432
and to access via pointer later(not that I like using pointers)?
Good luck then, because in C/C++ there are very, very few things you can do without pointers. Arrays, for example:
Code:
char string[10];
string[3] = 'A';

is the same as
Code:
char *string = (char *)malloc(10 * sizeof(char));
*(string + 3) = 'A';

And modifying multiple values in one function/method without pointers is virtually impossible.
Quote:
Originally Posted by james2432
So the int would be freed after it hits the end of the block?
Code:
main
{
}<--would be freed here

That's right.
# 7  
Old 04-13-2009
Quote:
Originally Posted by pludi
Code:
char string[10];
string[3] = 'A';

would you have to free(string) after?
Quote:
is the same as
Code:
char *string = (char *)malloc(10 * sizeof(char));
*(string + 3) = 'A';

And modifying multiple values in one function/method without pointers is virtually impossible.
you would have to free(string) here no matter what right?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX Openserver ver 5.0 freeing up space.

:)Hi all, Please advice me is it safer to use the following command to free up (truncate) space in HDD. 1) > /usr/adm/messages 2) > /usr/adm/sulog 3) > /usr/adm/ctlog 4) > /tmp 5) > /usr/adm/sa 6) > /var/spool Thanks in advance Rukshan (1 Reply)
Discussion started by: rukshan4u2c
1 Replies

2. Shell Programming and Scripting

Freeing the terminal from busy shell script?

Hi guys, I wrote a basic inotifywait shell script on my CentOS 5.6 x86_64 test server that syncs any deleted files in a directory. /usr/bin/script #!/bin/sh inotifywait -m -e delete /home/user/test | while read file; do # log event here done The script alone works fine. However, the... (4 Replies)
Discussion started by: TECK
4 Replies

3. Boot Loaders

Bootloader Resources

Here is a list of resources for Unix and GNU/Linux bootloaders: GRUB Legacy: The original GRand Unified Bootloader. Now known as GRUB Legacy. GRUB: The latest and greatest. More commonly known as GRUB2. BRUG: Brand-new Universal loadeR from GRUB. Based on GRUB. Adds features like new object... (0 Replies)
Discussion started by: fpmurphy
0 Replies

4. Programming

Trouble freeing memory after ctrl+D

Hello, I am trying to free memory allocation after EOF from keyboard is detected (ctrl+D) in a C program. I've written a small program to replicate my problem: int main(int argc, char *argv) { char *line; line = (char*)malloc(sizeof(char)*(512)); line = fgets(line, 512,... (10 Replies)
Discussion started by: oddworld
10 Replies

5. UNIX for Dummies Questions & Answers

Trouble freeing memory after ctrl+D

Hello, I am trying to free memory allocation after EOF from keyboard is detected (ctrl+D) in a C program. I've written a small program to replicate my problem: int main(int argc, char *argv) { char *line; line = (char*)malloc(sizeof(char)*(512)); line = fgets(line, 512,... (1 Reply)
Discussion started by: oddworld
1 Replies

6. Solaris

CPU resources

Hi, I got a solarsi 10 box with 9 zones and the cpu shares as following ID NAME SHARES 0 global 1 1 FMW1 100 2 FMW2 100 3 OID1 100 4 OID2 100 5 OVD1 100 6 OID0 100 7 FMW5 100 8... (2 Replies)
Discussion started by: fugitive
2 Replies

7. UNIX for Dummies Questions & Answers

Freeing up a serial port with a modem connected

I currently access a remote Unix server which has an external modem connected to one of it's serial ports (/dev/cua/b). At times, this server undergoes a hard reset and for some reason this disallows us from making use of the modem any longer. A hard reset of the modem always seems to fix the... (0 Replies)
Discussion started by: ebender1
0 Replies

8. UNIX for Dummies Questions & Answers

Who is using up all of my resources?!

For some reason, I'm having a bit of a brain fart here and cannot think of a simple solution to this problem... We have a samba server installed on one of our Darwin boxes. Someone is doing massive amounts of work through a samba share, and in turn in pegging samba and the box. I can see how... (1 Reply)
Discussion started by: fender177
1 Replies
Login or Register to Ask a Question