Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Analogue of ReadLine() in C

Reply
Thread Tools

Analogue of ReadLine() in C

 
 
Максим Фомин
Guest
Posts: n/a
 
      10-19-2011
I need function similar to ReadLine() in any other language.
The program below fails[1] when data more then LAG_SIZE is passed

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

char * read_line(void)
{
bool read_more = true;
enum { LAG_SIZE = 32 };
char *return_buf = NULL;
char *new_line_pos = NULL;
char tmp_buf[LAG_SIZE] = {'\0'};
size_t size = 0;
while (read_more && fgets(tmp_buf, LAG_SIZE, stdin)){
char *tmp_realloc;
size += LAG_SIZE;
tmp_realloc = realloc(return_buf, size);
if (!tmp_realloc){
free(return_buf);
return_buf = NULL;
break;
}
return_buf = tmp_realloc;
memset(return_buf + size - LAG_SIZE, '\0', LAG_SIZE);
new_line_pos = strchr(tmp_buf, '\n');
if (new_line_pos){
*new_line_pos = '\0';
read_more = false;
}
strcat(return_buf, tmp_buf);
memset(tmp_buf, '\0', LAG_SIZE);
}
return return_buf;
}

void loop(void)
{
bool repeat = true;
while (repeat){
char *command;
printf("Enter> ");
command = read_line();
if (command && !strcmp(command, "exit"))
repeat = false;
free(command);
}
}

int main(void)
{
loop();
return 0;
}

[1]: *** glibc detected *** ./a.out: realloc(): invalid next size:
0x0804b008 ***
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      10-19-2011
If one has problems with dynamical allocations of pointers in the main memory, please use static buffer first to dump buffer contents and define the DEBUG modes for all pointers!


======
It is bad in C/C++ to use macros with nested operations in any argument!

Macros should not affect arguments' variables values unless desired in the macros!


 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      10-19-2011
On 2011-10-19, 88888 Dihedral <(E-Mail Removed)> wrote:
> If one has problems with dynamical allocations of pointers in the main memory, please use static buffer first to dump buffer contents and define the DEBUG modes for all pointers!
>
>
>======
> It is bad in C/C++ to use macros with nested operations in any argument!


ITYM, "side effects in any argument".

I do not agree. The obvious counterexample are properly designed macros which
are carefully designed to evaluate their arguments exactly once, and have a
proper sequence point after the evaluation of arguments.

Removing this requirement sometimes make it easier to design a macro, and then
the users of the macro have to be given a big warning.

> Macros should not affect arguments' variables values unless desired in the macros!


When it is acceptable for a computer operation to have an effect on data,
such that the effect is not desired?
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      10-19-2011
China Blue Corn Chips <(E-Mail Removed)> writes:
>char *p = 0; int m = 0, n = 0, c;
>while (c=fgetc(file), c!='\n' && c!=EOF) {
> if (n>=m) {m = 2*n+1; p = realloc(p, m);}
> p[n++] = c;


What if !realloc(p,m)?

 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      10-19-2011
On 2011-10-19, ?????? ????? <(E-Mail Removed)> wrote:
> I need function similar to ReadLine() in any other language.
> The program below fails[1] when data more then LAG_SIZE is passed
>
> #include <stdio.h>
> #include <stdbool.h>
> #include <string.h>
> #include <stdlib.h>
>
> char * read_line(void)
> {
> bool read_more = true;
> enum { LAG_SIZE = 32 };
> char *return_buf = NULL;
> char *new_line_pos = NULL;
> char tmp_buf[LAG_SIZE] = {'\0'};
> size_t size = 0;
> while (read_more && fgets(tmp_buf, LAG_SIZE, stdin)){
> char *tmp_realloc;
> size += LAG_SIZE;
> tmp_realloc = realloc(return_buf, size);
> if (!tmp_realloc){
> free(return_buf);
> return_buf = NULL;
> break;
> }
> return_buf = tmp_realloc;
> memset(return_buf + size - LAG_SIZE, '\0', LAG_SIZE);
> new_line_pos = strchr(tmp_buf, '\n');
> if (new_line_pos){
> *new_line_pos = '\0';
> read_more = false;
> }
> strcat(return_buf, tmp_buf);
> memset(tmp_buf, '\0', LAG_SIZE);
> }
> return return_buf;
> }
>
> void loop(void)
> {
> bool repeat = true;
> while (repeat){
> char *command;
> printf("Enter> ");
> command = read_line();
> if (command && !strcmp(command, "exit"))


Is this really what you meant? This means that the program will
loop forever when no more input can be read (e.g. when EOF is
reached). I'd guess you want:

if (command == NULL || !strcmp(command, "exit"))

instead.

> repeat = false;
> free(command);
> }
> }
>
> int main(void)
> {
> loop();
> return 0;
> }
>
> [1]: *** glibc detected *** ./a.out: realloc(): invalid next size:
> 0x0804b008 ***



--
http://www.velocityreviews.com/forums/(E-Mail Removed)
SDF Public Access UNIX System - http://sdf.lonestar.org
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      10-20-2011
On Oct 19, 8:26*pm, Максим Фомин <(E-Mail Removed)> wrote:

> I need function similar to ReadLine() in any other language.


*what* other language? It certinly isn't present in all other
langauges.

> The program below fails[1] when data more then LAG_SIZE is passed


so fix it

<snip>
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      10-20-2011
On 2011-10-20, China Blue Corn Chips <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)-berlin.de>,
> http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) wrote:
>> China Blue Corn Chips <(E-Mail Removed)> writes:
>> >char *p = 0; int m = 0, n = 0, c;
>> >while (c=fgetc(file), c!='\n' && c!=EOF) {
>> > if (n>=m) {m = 2*n+1; p = realloc(p, m);}
>> > p[n++] = c;


>> What if !realloc(p,m)?


> Buy a bigger swap disk.


This is crappy advice, because it's completely unrelated. There are
tons of systems which, for sane and well-considered reasons, will cap
allocations far below the size of physical memory or swap. 2*n+1 goes
pathological pretty fast. Also, it's... ridiculous, frankly, to start
at 0. That guarantees at least one realloc, and probably several for
even a tiny string.

Imagine that you're on a system which, quite reasonably, limits your
32-bit code to 3GB of memory. You can have all 3GB, but you can't have
more.

Now imagine that you want to process a string which contains 1GB of
data. Your algorithm will do ~30 reallocs, most of which will almost
certainly be copying all that data, and then fail. Think it through;
what happens? At any given time, your current length is a value of
the form 2^n - 1. So what's it going to do for 2^n bytes of data,
say, 1GB exactly?

1. It reallocs until it has exactly one byte less than 1GB of data.
2. It then tries to allocate exactly one byte less than 2GB of data,
to copy that first GB into.

But that's two bytes less than 3GB of data it needs to be able to handle,
and I doubt your code and support libraries fit into 2 bytes.

Depending, I'd say either start with a "likely" length or start with
something around a page size, and reallocate up a little more conservatively.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Максим Фомин
Guest
Posts: n/a
 
      10-20-2011
On Oct 20, 11:11*am, Nick Keighley <(E-Mail Removed)>
wrote:
> On Oct 19, 8:26*pm, Максим Фомин <(E-Mail Removed)> wrote:
>
> > I need function similar to ReadLine() in any other language.

>
> *what* other language? It certinly isn't present in all other
> langauges.


Yes, not all languages have. But many decent have.
However, it is not an issue.

>
> > The program below fails[1] when data more then LAG_SIZE is passed

>
> so fix it
>
> <snip>


I haven't not found any error in red_line() function (I consider calls
to malloc, realloc and free legitimate), in fact I doubt that there is
one.
That's why I asked the question.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-20-2011
On 10/19/2011 03:26 PM, Максим Фомин wrote:
> I need function similar to ReadLine() in any other language.
> The program below fails[1] when data more then LAG_SIZE is passed

....
> [1]: *** glibc detected *** ./a.out: realloc(): invalid next size:
> 0x0804b008 ***


I could not duplicate that symptom. Could you provide the exact command
line you used to compile the program, and exact input line that triggers
the failure?
--
James Kuyper
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      10-20-2011
In article <(E-Mail Removed)-september.org>,
China Blue Corn Chips <(E-Mail Removed)> plaintively whind:
....
>I'm not going to do it unless you pay me.


That's what your girlfriends always tell you, isn't it?

--
Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
an 8 bit operating system based on a 4 bit architecture from a 2 bit company
that can't stand 1 bit of competition.

Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
operating system based on a 4 bit architecture from a 2 bit company that
can't stand 1 bit of competition.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Trying to get a 3640 to work as dial-in ISDN/analogue accessserver Guy Dawson Cisco 1 04-17-2007 10:08 AM
Analogue V DVI display quality xxxxxxxxxx Computer Support 2 12-05-2004 06:25 PM
Analogue or digital camcorder? Kenny Computer Support 1 11-24-2004 09:17 PM
Thrustmaster Dual Analogue JayneB Computer Support 0 03-04-2004 06:48 PM
as5200 and incoming analogue calls problem John Gelavis Cisco 0 11-27-2003 01:50 AM



Advertisments