Are there any other semi/portable ways to dynamically allocate memory?

心已入冬 提交于 2019-12-22 12:21:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!