The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
get sysname with getline in awk timj123 Shell Programming and Scripting 2 02-24-2008 06:43 PM
awk getline help maybe? chlawren Shell Programming and Scripting 0 10-24-2007 07:56 AM
utility of getline here? remid1985 UNIX for Dummies Questions & Answers 3 11-12-2006 09:38 AM
awk:Problem with getline Klashxx Shell Programming and Scripting 3 05-12-2006 03:24 AM
getline with a unique gozer13 Shell Programming and Scripting 6 03-03-2005 08:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-08-2008
Registered User
 

Join Date: Dec 2006
Posts: 29
Stumble this Post!
'getline' questions

I have a C program I've written that includes a 'getline' call that is used to read a line at a time from a file and process the information in each line. A potential problem comes up since I don't know the maximum length of each line; right now I'm using a constant of 256, but that might not be long enough when run in the real world. The description of getline says that it will 'realloc' the string if it's not long enough, so I've tried running the program lowering the aforementioned constant to a value less than most of the lines in my input file and passing that value into 'getline' as the 2nd parameter 'n'. When I run it using the 'ddd' debugger, I get a segmentation fault after a few lines had been read on a 'free' call. I also ran it outside of 'ddd' with 'valgrind' and the program runs all the way through with no seg fault, though it says that I "definitely lost" a little over a hundred bytes, something that didn't happen before. I wonder if I should just set the char pointer to NULL and 'n' to zero and let getline do all the allocation, though passing it an already-allocated string that's not long enough should work, too, according to the man page. I'm also unclear about the purpose of the 'n' parameter and what the value is that's returned there; it returns the number of bytes read, but not through that parameter. Any words of wisdom would be appreciated.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-08-2008
Registered User
 

Join Date: Jun 2008
Posts: 24
Stumble this Post!
Kinda hard to tell why its segfaulting without seeing any code. Make sure you are passing a **buf not a *buf.

But as far as the 'n' parameter its there so you can pass in the size of the buffer that you pre-allocated. It will contain the size of the buffer you get back. Thus if you passed in a buffer too small it will have the new size. If you passed in null, it will have the size that was allocated for it.

The return value of the function returns characters read (not necessarily size of the buffer) not counting the last null byte if there.
Reply With Quote
  #3 (permalink)  
Old 07-08-2008
Registered User
 

Join Date: Dec 2006
Posts: 29
Stumble this Post!
Thanks for the reply. I think I know why the segfault happened, having to do with the logic of my program and the fact that I changed a bit of it to try giving 'getline' a smaller character string; I won't bore you with all the details.

While looping through a file, reading one line at a time, I should just need to call 'free' once, after the loop is through, correct? It's doing a 'realloc' when it needs more room, and not allocating a whole new string each time. I'm wondering about how much more space it reallocs when it's necessary. I just ran my program again checking the return value of 'n', size of buffer returned, and the value of bytes that were read (my initial value of the size of line is 25, just picking one that would be too small most of the time):

bytes read size of buffer returned
7 25
56 57
36 57
97 114
70 114
23 114
39 114

Apparently, once the buffer being passed back gets a certain size, it won't get reallocated to a smaller size, what with it staying at 114. I'm wondering why when the line was 96 bytes it didn't return a buffer of size 97 (allowing for the null) instead of jumping to 114.

Thanks again.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0