Trouble freeing memory after ctrl+D


 
Thread Tools Search this Thread
Top Forums Programming Trouble freeing memory after ctrl+D
# 1  
Old 12-05-2009
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:

Code:
int main(int argc, char *argv[]) {

   char *line;
   line = (char*)malloc(sizeof(char)*(512));

   line = fgets(line, 512, stdin);

   if (feof(stdin)) {     /* Detects ctrl+D */
      free(line);         /* Attempts to free line before return */
      return(EXIT_SUCCESS);
   }

   return(EXIT_SUCCESS);
}

When I run this under valgrind with --leak-check=full --show-reachable=yes it is saying the line is not freed when I terminate with ctrl+D. What am I doing wrong here?

Last edited by pludi; 12-05-2009 at 06:26 AM..
# 2  
Old 12-05-2009
Oddly enough there is a thread from 2 days ago on this very topic. Consider using the forum search facility next time. That is why we have it.

Catch ctrl+d in C

There are some links to POSIX behavior and an explanation about changing the behavior of the terminal.

Also - when your program exits, the OS "frees" all memory anyway. There is no memory leak, therefore.
# 3  
Old 12-05-2009
Quote:
Originally Posted by jim mcnamara
Also - when your program exits, the OS "frees" all memory anyway. There is no memory leak, therefore.
I don't think this is true, at least using SUS2 like I am. Any piece of malloc'd memory has to be freed before the program exits or it is considered leaked memory. If you run the sample code I posted above with valgrind and just hit enter, it will report the leaked memory (since it does not free the line if the program exits normally). My question is, why does the program leak memory when an EOF is sent from the keyboard even though it tries to free the memory before returning in that case?
# 4  
Old 12-05-2009
Quote:
Originally Posted by oddworld
I don't think this is true
You should, though. Leaked memory does not survive its allocating process death by design. Allocated memory is virtual memory which means nothing when the context of a process disappear.
# 5  
Old 12-05-2009
I'd make double-sure the program is really doing what you think it does. print a line and free the memory when ctrl-d is found.
# 6  
Old 12-05-2009
Quote:
Originally Posted by Corona688
I'd make double-sure the program is really doing what you think it does. print a line and free the memory when ctrl-d is found.
This is exactly what I did to make sure the code reaches the free(line). If I change the if statement to something like:

Code:
if (feof(stdin)) {
   printf("CTRL+D pressed by user\n");
   free(line);
   return(EXIT_SUCCESS);
}

The printf is printed, but the line isn't freed...
# 7  
Old 12-05-2009
You didn't show what headers you included, are they at least stdio.h and stdlib.h ?

[edit] AHAH! On an EOF condition, fgets returns null, overwriting line with that and preventing the real value from being freed. Overwriting line with fgets' return value serves no purpose in any case, since if it finds any input it won't change.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation of keyboard inputs..like Ctrl+d and Ctrl+a

Hi..! I'm stuck with my automation of starting a process and keeping it running even after the current ssh session has exited.. So i'm trying to use command 'screen'. which is doing exactly what i wanted, But the problem is automation of the same. i will have to press Ctrl+a and Ctrl+d for... (2 Replies)
Discussion started by: chandana hs
2 Replies

2. UNIX for Dummies Questions & Answers

Ctrl-V + Ctrl-J for newline character does not work inside vi editor

Hi friends, I am trying to add a newline char ('\n') between the query and the commit statement in the following shell script. #! /bin/sh echo "select * from tab; commit;" > data.sql I have tried typing in "Ctrl-V + Ctrl-J" combination which has inserted ^@ (NUL) character but the commit... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. AIX

memory is more than 80% how will u trouble shoot it?

memory is more than 80% how will u trouble shoot it? (1 Reply)
Discussion started by: ramraj731
1 Replies

4. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

5. Shell Programming and Scripting

Ctrl-C or Ctrl-Z causing exit the session

H! I have written script where it need to invoke the perl script in background, then write the pid in temp file then bring back the job to foreground. whenever the Ctrl-C or Ctrl-Z is pressed in the script has to exit and prompt should be dispalyed. but this script causing exit from shell session... (2 Replies)
Discussion started by: jramesh1
2 Replies

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

7. AIX

Memory trouble

Hi All, I just installed 8GB of Memory in a P550 and when the system booted, it's only showing 4GB (3808MB). How can I get the system to see the other 4? (12 Replies)
Discussion started by: bbbngowc
12 Replies

8. Programming

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(); (8 Replies)
Discussion started by: james2432
8 Replies

9. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies
Login or Register to Ask a Question