The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-30-2008
naan naan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 31
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
  #2 (permalink)  
Old 08-30-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
size of a line means its length right??
  #3 (permalink)  
Old 08-30-2008
naan naan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 31
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.
  #4 (permalink)  
Old 08-30-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by naan View Post
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.
Why dont you construct a buffer of some size say 1024. Initialize the buffer contents with 0. As you read each character put that character into the buffer. Once the line is done, you have the length as well as the contents. Create your new memory with that length and do a memcpy (destination, source, length) to copy the contents.
  #5 (permalink)  
Old 08-30-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
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.
  #6 (permalink)  
Old 08-31-2008
tsurko tsurko is offline
Registered User
  
 

Join Date: Jun 2007
Location: Sofia, Bulgaria
Posts: 35
Maybe this will help?
  #7 (permalink)  
Old 09-02-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by naan View Post
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
Any compelling reason to go for a dynamic solution??
A static solution may not be optimized but a dynamic solution would be expensive in terms of mp units.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0