Re: Printing LSN made easy
| От | Li Japin |
|---|---|
| Тема | Re: Printing LSN made easy |
| Дата | |
| Msg-id | 95EBC593-1A19-4E0B-B778-373C8A6F0238@hotmail.com обсуждение исходный текст |
| Ответ на | Re: Printing LSN made easy (Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>) |
| Ответы |
Re: Printing LSN made easy
|
| Список | pgsql-hackers |
Hi,
On Nov 30, 2020, at 9:06 PM, Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> wrote:On Fri, Nov 27, 2020 at 9:51 PM Li Japin <japinli@hotmail.com> wrote:
Hi,
Here, we cannot use sizeof(but) to get the buf size, because it is a pointer, so it always
8 bytes on 64-bit or 4 bytes on 32-bit machine.
For an array, the sizeof() returns the size of memory consumed by the
array. See section "Application to arrays" at
https://en.wikipedia.org/wiki/Sizeof.
That’s true! However, in pg_lsn_out_buffer(), it converts to a pointer, not an array. See the following test:
```c
#include <stdio.h>
#include <stdint.h>
typedef uint64_t XLogRecPtr;
typedef uint32_t uint32;
#define MAXPG_LSNLEN 17
#define LSN_FORMAT "%X/%X"
#define LSN_FORMAT_ARG(lsn) (uint32) ((lsn) >> 32), (uint32) (lsn)
char *
pg_lsn_out_buffer(XLogRecPtr lsn, char *buf)
{
printf("pg_lsn_out_buffer: sizeof(buf) = %lu\n", sizeof(buf));
snprintf(buf, sizeof(buf), LSN_FORMAT, LSN_FORMAT_ARG(lsn));
return buf;
}
char *
pg_lsn_out_buffer1(XLogRecPtr lsn, char *buf, size_t len)
{
printf("pg_lsn_out_buffer1: sizeof(buf) = %lu, len = %lu\n", sizeof(buf), len);
snprintf(buf, len, LSN_FORMAT, LSN_FORMAT_ARG(lsn));
return buf;
}
int
main(void)
{
char buf[MAXPG_LSNLEN + 1];
XLogRecPtr lsn = 1234567UL;
printf("main: sizeof(buf) = %lu\n", sizeof(buf));
pg_lsn_out_buffer(lsn, buf);
printf("buffer's content from pg_lsn_out_buffer: %s\n", buf);
pg_lsn_out_buffer1(lsn, buf, sizeof(buf));
printf("buffer's content from pg_lsn_out_buffer1: %s\n", buf);
return 0;
}
```
The above output is:
```
main: sizeof(buf) = 18
pg_lsn_out_buffer: sizeof(buf) = 8
buffer's content from pg_lsn_out_buffer: 0/12D68
pg_lsn_out_buffer1: sizeof(buf) = 8, len = 18
buffer's content from pg_lsn_out_buffer1: 0/12D687
```
--
Best regards
Japin Li
ChengDu WenWu Information Technolog Co.,Ltd.
В списке pgsql-hackers по дате отправления: