1

I have a project in which I'm working on, that's gonna take an input that comes out from another program ran on the terminal, like so:

./other_program | ./project

so I'm taking the output from other_program and using it on project with

read(0, buffer, BUFF_SIZE);

But if imagine that's not the best way to do that. I know I can iterate through stdin and just use realloc to increase the buffer size, but I'm forbidden from using realloc, due to project specifications, which say I can only use malloc, read, write, open and free.

Is there any other way out? thanks!

5
  • Strange constraints... You want to dynamically allocate memory without using memory allocation functions... Well, if such constraints are required it may be because perhaps it's unnecessary to put all data in a huge buffer. Can you process the input data in chunks of BUFF_SIZE bytes? (for example, counting words does not require grabbing all data in a single buffer). Commented Sep 15, 2014 at 15:22
  • Well then, what are those functions? There's only two choices: either you need dynamic allocation, either you don't. Commented Sep 15, 2014 at 15:23
  • I think it's weird too :P I can use malloc, read, write, open, free Commented Sep 15, 2014 at 15:29
  • @dolan functions up here /\ Commented Sep 15, 2014 at 15:46
  • 5
    You can emulate the behavior of realloc purely with malloc and free. Commented Sep 15, 2014 at 15:48

3 Answers 3

2

Repeatedly read data into a local buffer and append it to a big buffer.

After reading, memory allocated to bigbuf will be right-sized to the data read.

A more robust solution would use an exponentially (maybe 1.5x to 3x) growing bigbufsize.

#define BUFF_SIZE 1024
char buffer[BUFF_SIZE];
char *bigbuf = NULL;
size_t bigbufsize = 0;

ssize_t len;
while( (len = read(0, buffer, sizeof buffer)) > 0) {
  size_t newbigbufsize = bigbufsize + len;
  char *newbigbuf = malloc(newbigbufsize);
  if (newbigbuf == NULL) exit(1);  //Handle OOM

  memcpy(newbigbuf, bigbuf, bigbufsize);
  memcpy(&newbigbuf[bigbufsize], buffer, len);
  free(bigbuf);
  bigbuf = newbigbuf;
  bigbufsize = newbigbufsize;
}

// Use data
foo(bigbuf, bigbufsize);

// clean-up
free(bigbuf);
bigbuf = NULL;
bigbufsize = 0;
Sign up to request clarification or add additional context in comments.

Comments

1

If you can process the input in chunks of bounded length, prefer reusing a buffer of that length, clearing it with memset on each iteration. If you must have the entire input in memory at once to move forward (or some arbitrarily large part of it) then you will have to emulate realloc as @dolan mentions. That can be done like so:

#define BUFFER_INCREMENT 128

// In some function somewhere...

    size_t bufsize = BUFFER_INCREMENT;
    char *buffer = malloc(bufsize * sizeof(char));
    char *bufferp = buffer;
    while( fgets(bufferp, BUFFER_INCREMENT, stdin) ){
        size_t newSize = strlen(bufferp);
        char *newbuffer = malloc( (bufsize + newSize) * sizeof(char) );
        memcpy( newbuffer, buffer, bufsize );
        free( buffer );
        buffer = newbuffer;
        bufferp += newSize;
        bufsize += newSize;
    }

1 Comment

Thanks @chux for pointing out my oversight with regards to newlines, I have updated the code accordingly.
-2

Use ft_concat ;) And ask to your neighbour before stackoverflow !

Personally, I have build a chained list then translate it into char array

2 Comments

okey sorry my responses isn't very great but I m pretty sure than the 'asker' is in the same school than me and, for lots of reasons, put a question here is not in the school pedagogie. We're about 600 students and the goal is to help each others, and be helped by each others. By the way lots of students know how to do this ... Maybe I m wrong ... but I don't think so
I wanted this for the evalexpr haha so I waited until it was done. I'm not using this for the final project even because we're not getting the input through command line, it's gonna be a file. I'm trying to be as fair as possible, that's why I waited for the project to be over. I appreciate that people at 42 are trying to be fair tho :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.