问题
this:
char *buf = NULL;
scanf("%ms", &buf);
will get a dynamically allocated char buffer. I know this is limited to code compiled with Gcc (and specifically version 2.7 of glibc).
I am also aware that the "correct" and portable way is to just use malloc()
(and friends) to get the memory.
I'm curious however, are there any other portable or semi-portable* implementations out there for getting dynamically allocated memory? I find tricks and tips of memory allocation in C a hard topic to "Google".
Note: This is not "required" for anything, so there are no limitations on answers.
* semi-portable meaning "if you compile the same way I do, this will work"
回答1:
POSIX has
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
and
ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
which should qualify as semi-portable.
来源:https://stackoverflow.com/questions/13863063/are-there-any-other-semi-portable-ways-to-dynamically-allocate-memory