Hi, On Nov 30, 2020, at 9:06 PM, Ashutosh Bapat <ashutosh.bapat....@gmail.com<mailto:ashutosh.bapat....@gmail.com>> wrote:
On Fri, Nov 27, 2020 at 9:51 PM Li Japin <japi...@hotmail.com<mailto:japi...@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.