C - Dynamically allocating array for stdin without realloc

dietbacon

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!

chux - Reinstate Monica

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - Dynamically sized array of struct pointers without using realloc?

From Dev

Dynamically allocating an array in a function in C

From Dev

Dynamically allocating lines from stdin?

From Dev

Dynamically allocating a 2D array in C

From Dev

Dynamically allocating array of struct from file C

From Dev

Dynamically allocating array explain

From Dev

Dynamically Allocating Array With Datafile

From Dev

Dynamically allocating an array of a struct

From Dev

Grow array in C without using realloc?

From Dev

Dynamically allocating threads in C

From Dev

c: issues when allocating 2d char array dynamically?

From Dev

C segmentation fault when dynamically allocating 2d array

From Dev

dynamically allocating my 2d array in c

From Dev

Allocating an Array of Structs without Malloc?

From Dev

Dynamically allocating a 2D string array

From Dev

dynamically allocating an int array inside a structure

From Dev

Trouble dynamically allocating memory for string array

From Dev

Allocating memory for array of pointers of int dynamically

From Dev

Allocating 2D array dynamically

From Dev

Dynamically allocating memory for changing array size starting with unknown size C++

From Dev

C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

From Dev

when allocating memory for an array dynamically (in C), what does the (int *) cast do?

From Dev

Allocating an array of a class c++

From Dev

Allocating an array of a class c++

From Dev

Allocating memory to array of strings in c

From Dev

Dynamically Allocating 2D Arrays in C

From Dev

Dynamically allocating memory in a function WITHIN a function in C

From Dev

C Runtime Error: Dynamically Allocating Memory in a For Loop

From Dev

Create an array of strings without allocating each string

Related Related

  1. 1

    C - Dynamically sized array of struct pointers without using realloc?

  2. 2

    Dynamically allocating an array in a function in C

  3. 3

    Dynamically allocating lines from stdin?

  4. 4

    Dynamically allocating a 2D array in C

  5. 5

    Dynamically allocating array of struct from file C

  6. 6

    Dynamically allocating array explain

  7. 7

    Dynamically Allocating Array With Datafile

  8. 8

    Dynamically allocating an array of a struct

  9. 9

    Grow array in C without using realloc?

  10. 10

    Dynamically allocating threads in C

  11. 11

    c: issues when allocating 2d char array dynamically?

  12. 12

    C segmentation fault when dynamically allocating 2d array

  13. 13

    dynamically allocating my 2d array in c

  14. 14

    Allocating an Array of Structs without Malloc?

  15. 15

    Dynamically allocating a 2D string array

  16. 16

    dynamically allocating an int array inside a structure

  17. 17

    Trouble dynamically allocating memory for string array

  18. 18

    Allocating memory for array of pointers of int dynamically

  19. 19

    Allocating 2D array dynamically

  20. 20

    Dynamically allocating memory for changing array size starting with unknown size C++

  21. 21

    C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

  22. 22

    when allocating memory for an array dynamically (in C), what does the (int *) cast do?

  23. 23

    Allocating an array of a class c++

  24. 24

    Allocating an array of a class c++

  25. 25

    Allocating memory to array of strings in c

  26. 26

    Dynamically Allocating 2D Arrays in C

  27. 27

    Dynamically allocating memory in a function WITHIN a function in C

  28. 28

    C Runtime Error: Dynamically Allocating Memory in a For Loop

  29. 29

    Create an array of strings without allocating each string

HotTag

Archive