Re: how to implement routines that return general strings?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 8/10/06, Scott <drmemory@xxxxxxxxxxx> wrote:
With the caveat that I haven't done any serious programming in C for
many years:  As I recall, I always used a protocol halfway between
these, as:

char *func(size_t bufsiz)
{
        char *retval = malloc (bufsiz);
        if (retval == NULL) return (NULL);
        /* whatever */
        return (retval);
}

This code is to live in a shared library and you wish for it to be
portable then you may want to keep in mind the difficulties on some
platforms of allocating memory in one shared library and freeing in
another.  If this concern is relevant to you, the above must be paired
with void funcCleanup(char*).

This approach is also nice if the calling function can't know the
amount of memory necessary to allocate.

-K
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux