Trouble freeing memory after ctrl+D


 
Thread Tools Search this Thread
Top Forums Programming Trouble freeing memory after ctrl+D
# 8  
Old 12-06-2009
Ah, you are right. Once again, fgets causes me to fumble something. Everything is working solid now, thank you.
# 9  
Old 12-06-2009
Quote:
Originally Posted by Corona688
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.
And the lesson here is:

Never, ever overwrite your only pointer to dynamically-allocated memory. Even if the value is supposed to be the original value of the pointer.
# 10  
Old 12-08-2009
Honestly, I'm not entirely sure why people use feof, everything I've ever written does this:

Code:
while (fgets(line, sizeof(line), stdin)
{
     /* process line */
}

Has never failed me....
# 11  
Old 12-08-2009
For things that aren't line-based, I think.
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