C++ Memory Leak Check -
will leak memory:
std::string getstringfromuser(std::string sprompt, int nmaxbuffsize) { int i, ch; char *psbuffer = (char *) _alloca(nmaxbuffsize); fprintf(stderr, "%s", sprompt.c_str()); fflush(stderr); (i = 0; (i < nmaxbuffsize) && ((ch = getchar()) != eof) && (ch != '\n'); i++) { psbuffer[i] = (char) ch; } psbuffer[i] = '\0'; return (std::string) psbuffer; } // getstringfromuser()
written 1 of betters, seems psbuffer never deleted.
no, there no memory leak in function , using alloca won't cause memory leak it's allocated on stack. http://www.gnu.org/software/libc/manual/html_mono/libc.html#variable-size-automatic:
3.2.5 automatic storage variable size
the function alloca supports kind of half-dynamic allocation in blocks allocated dynamically freed automatically.
allocating block alloca explicit action; can allocate many blocks wish, , compute size @ run time. blocks freed when exit function alloca called from, if automatic variables declared in function. there no way free space explicitly.
Comments
Post a Comment