![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
| HP-UX memory usage allocation | dehuang83 | HP-UX | 3 | 06-02-2007 03:40 AM |
| HP-UX memory usage allocation | dehuang83 | UNIX for Dummies Questions & Answers | 1 | 05-03-2007 01:40 AM |
| tar: Memory allocation failed | gfhgfnhhn | UNIX for Dummies Questions & Answers | 1 | 03-05-2007 11:23 AM |
| threads and memory allocation | prankster | UNIX for Advanced & Expert Users | 3 | 12-14-2005 12:45 PM |
| memory allocation | sagar | UNIX for Dummies Questions & Answers | 1 | 01-05-2002 11:53 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Dynamic memory allocation
Hi,
I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated with the size of the line? Is there any optimised way to do this? Thanks, Anitha |
|
||||
|
Yes, the length of each line.
Suppose I have a file with the lines: Today is tuesday. Tomorrow is Wednesday. I have to find the length of the first line which I can do by character by character reading till I reach a '\n' with a count of the number of characters but then I have to go back to the beginning of the line again to read the contents of the line and put in the buffer which is allocated the number I obtained by the first level of counting. |
|
||||
|
this wont work for lines that are greater than size 1024
instead, parse through the line and then do a dynamic allocation and copy the contents, free that. But throughput will suffer here as memory is allocated and deallocated each time for parsing a line. Instead of a big buffer size like 1KB or 2 KB ask Vino suggested can be safely used. |
|
||||
|
Quote:
A static solution may not be optimized but a dynamic solution would be expensive in terms of mp units. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|