Trouble freeing memory after ctrl+D


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers 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?
# 2  
Old 12-05-2009
Double post, continued here. Closed.
 
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. 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

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