LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-23-2005, 09:32 AM   #1
thinkgeek
LQ Newbie
 
Registered: Jun 2005
Posts: 15

Rep: Reputation: 0
Longest line in C


I want to enter the arbitrary line as long as possible from termiinal and then print the entered line when EOF is encountered. This has to be done without using system storage allocation functions such as malloc or realloc.

thinkgeek
 
Old 08-23-2005, 09:34 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 53
There's no way to dynamically grow your buffer without using malloc(), etc. and you can't know how big to make the buffer beforehand. Why can't you use malloc()? Probably because it's homework.
 
Old 08-23-2005, 10:18 AM   #3
thinkgeek
LQ Newbie
 
Registered: Jun 2005
Posts: 15

Original Poster
Rep: Reputation: 0
I just wanted to know/try whether is it possible without malloc/realloc.
Is it possible that you don't store at all and you first input 'entire string'
and then print entire string. (No storage could be required..)

thinkgeek
 
Old 08-23-2005, 10:45 AM   #4
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 199

Rep: Reputation: 33
Quote:
I just wanted to know/try whether is it possible without malloc/realloc.
Is it possible that you don't store at all and you first input 'entire string'
and then print entire string. (No storage could be required..)
If you input anything, you need to store it someplace in RAM. That is why you need to malloc/realloc the memory. In answer to your question, no it is not possible to input anything without preallocating a memory buffer to hold it. Storage in this case is RAM.
 
Old 08-23-2005, 12:06 PM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 33
I think this may be what he means, but the 'storage' is pre-allocated:
[code]
int main(int argc, char *argv[])
{
printf("%s\n",argv[1]);
return 0;
}

usage: myprogram cmcmcmskfirurhjdj.....
displays the junk you typed.


So your C compiler should be able to take in as part of the command line 4096 characters - the POSIX1 standard for the number of chars that C exec functions can take as the total length of *argp + *envp.

In practice this number is larger.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Longest Uptime for Linux, yours not somebody else's. dalek Linux - General 60 Yesterday 10:00 AM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
longest time awake without sleeping? nixel General 25 05-09-2004 01:42 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 08:54 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration