0

When writing a function in C for use in a Postgres extension, you allocate memory using palloc().

Is there any way to know how much memory you allocated? For example,

char *foo = palloc(10);
int size = some_size_of_function(foo);

Or do I have to remember the size in my application code?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
ccleve
  • 15,239
  • 27
  • 91
  • 157
  • 1
    sizeof is giving you the size of the object - in this case the size of the pointer, not the object referenced by the pointer. Such a information is not available in C. You need to store the size yourself – 0___________ Sep 29 '21 at 23:16
  • @0___________ This isn't a C question. It's a Postgres question. Postgres has a different memory management system. – ccleve Sep 29 '21 at 23:46
  • You have to track the size in your code. There is no way to figure out how much memory was allocated from the pointer. Such are the joys of memory management in C. – Laurenz Albe Sep 30 '21 at 06:22
  • @ccleve No, you simply use library to access postgresql server. It is plain C. Nothing common with SQL – 0___________ Sep 30 '21 at 06:22

0 Answers0