Re: CurrentMemoryContext and MemoryContextStrdup - Mailing list pgsql-novice

From Tom Lane
Subject Re: CurrentMemoryContext and MemoryContextStrdup
Date
Msg-id 14261.1574786901@sss.pgh.pa.us
Whole thread Raw
In response to Re: CurrentMemoryContext and MemoryContextStrdup  (Yessica Brinkmann <yessica.brinkmann@gmail.com>)
List pgsql-novice
Yessica Brinkmann <yessica.brinkmann@gmail.com> writes:
> Thank you very much for the reply!
> Not really, I don't feel better informed because MemoryContextStrdup is not
> even mentioned once in the README.

The next thing to do would be to look at that function's header comment
(find it in src/backend/utils/mmgr/mcxt.c):

/*
 * MemoryContextStrdup
 *        Like strdup(), but allocate from the specified context
 */
char *
MemoryContextStrdup(MemoryContext context, const char *string)


As I recall your original problem, people were suggesting that
you make a longer-lived copy of some transiently-allocated
string.  Copying it into a different context would be the
way to do that, as I hope you now understand from the README
discussion, and this function is the easiest way to do that.

Or at least the second easiest; the very easiest is pstrdup,
which is just

char *
pstrdup(const char *in)
{
    return MemoryContextStrdup(CurrentMemoryContext, in);
}

but I don't remember whether the current context was a suitable
target for your use-case.

            regards, tom lane



pgsql-novice by date:

Previous
From: Yessica Brinkmann
Date:
Subject: Re: CurrentMemoryContext and MemoryContextStrdup
Next
From: Yessica Brinkmann
Date:
Subject: Re: CurrentMemoryContext and MemoryContextStrdup