[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#1054891: gcc-12: sizeof() error in called function



Hi

On Sat, Oct 28, 2023 at 07:57:54PM +1300, rhys wrote:
> char myBuf[] = { '\x01', '\x04', '\x31', '\x00', '\x00', '\x1D', '\x7E', '\xF7' };
>     printf ("sizeof (myBuf) = %d\n", sizeof (myBuf));

This is an array, so sizeof() shows the length of that array.  Making it
longer would have showed you your mistake directly.

> void process_buffer (char *bufPtr) {
>     printf ("sizeof (bufPtr) = %d\n", sizeof (bufPtr));

This is a pointer.  A pointer points to somethine else.  The size of a
pointer is unrelated to your array.

> produces following output:
> sizeof (myBuf) = 8
> sizeof (bufPtr) = 4

You run on a 32 bit system, so pointers are 4*sizeof(char), aka 4 byte.

> expected output (confirmed on Oracle Linux 8.4):
> sizeof (myBuf) = 8
> sizeof (bufPtr) = 8

Oracle Linux 8 does not support 32 bit systems, so you run on a 64 bit
system.  Pointers are 8*sizeof(char), aka 8 bytes.

Arrays and pointers in C are sometimes interchangeable, but with great
precision you found one of the exceptions.

Bastian

-- 
You!  What PLANET is this!
		-- McCoy, "The City on the Edge of Forever", stardate 3134.0


Reply to: