Re: CurrentMemoryContext and MemoryContextStrdup

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Yessica Brinkmann <yessica(dot)brinkmann(at)gmail(dot)com>
Cc: pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Re: CurrentMemoryContext and MemoryContextStrdup
Date: 2019-11-26 16:48:21
Message-ID: 14261.1574786901@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Yessica Brinkmann <yessica(dot)brinkmann(at)gmail(dot)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

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Yessica Brinkmann 2019-11-26 17:34:32 Re: CurrentMemoryContext and MemoryContextStrdup
Previous Message Yessica Brinkmann 2019-11-26 16:24:12 Re: CurrentMemoryContext and MemoryContextStrdup