链表中 Next 指针的大小?
Size of Next pointer in linked list?
你好,给定链表的一个节点,
struct node
{
int data;
struct node* next;
};
考虑 4 的 int bytes.What 将是指针的大小 next?
我还有以下内容,
void * ptr;
printf("%d",sizeof(ptr));
指针大小为 8 个字节。
我得到的 sizeof(struct node) 为 12,next 指针在给定结构节点中的大小如何 12.Please 帮助我理解.
提前谢谢你。
指针的大小是存储地址所需的字节数:
printf("%zu",sizeof(ptr));
printf("%zu",sizeof(struct node *));
printf("%zu",sizeof &abc);
以上各项在 64 位地址的机器上应该 return 8
,在 32 位地址的机器上 4
。
可以通过取消引用指针来获得节点的大小:
struct node abc;
void *ptr = &abc;
printf("%zu",sizeof(*((struct node *)ptr)));
上面应该return12
在64位地址的机器上,如上所述。
sizeof(pointer)
是一个常量,无论它在最常见的现代系统上指向的是哪种普通旧数据类型。既然你这样做了:
sizeof(ptr)
并得到 8 bytes
我会冒险猜测您使用的是 64 位系统。这向我表明你的 sizeof(struct node)
将是 12 个字节,因为你有以下内容:
struct node {
int data; // 4 Bytes (32 bit, a common size for `int`s)
struct node* next; // 8 Bytes, as are all pointers on your system
}; // total size of 12 bytes.
在典型的系统上,指针的大小与其指向的数据的大小无关。在32位系统上,指针是32位(4字节),在64位系统上,指针是64位(8字节)。
您的结构大概是 12 个字节,因为它包含一个 4 字节的 int
和一个 8 字节的指针。但是,这是 platform-specific 并且可能会有所不同。许多系统要求值 aligned 为其大小的整数倍——也就是说,64 位指针必须从 8 字节的倍数的地址开始。编译器将在结构成员之间插入填充以满足对齐要求。
在我的 x86-64 Linux 系统上,您的结构大小为 16 个字节:int
4 个字节,4 个字节的填充以达到 8 个字节的边界,8 个指针的字节数。
What will be the size of pointer next?
尺码是sizeof(struct node*)
不应根据特定结果编写代码。
结果可能是 4 或 8,或 1 或 16 或其他。
编写可移植 C 代码依赖于不知道准确的答案,除了是一些理智的范围,如 1 到 64。
OP 没有提到需要知道 sizeof(ptr)
大小的原因,但答案只是指针的大小是 sizeof(ptr)
。代码应该使用 sizeof(ptr)
而不是像 4 或 8 这样的幻数。
要打印一个指针的大小,使用
some_type* ptr;
// printf("%d",sizeof(ptr));
printf("%zu",sizeof(ptr));
z Specifies that a following d, i, o, u, x, or X conversion specifier applies to a size_t
C11dr §7.21.6.1 7
int *
、const int *
、void *
、int (*)()
等指针的大小可能不同。可移植代码不假定指向各种类型的所有指针都具有相同的大小。
你好,给定链表的一个节点,
struct node
{
int data;
struct node* next;
};
考虑 4 的 int bytes.What 将是指针的大小 next?
我还有以下内容,
void * ptr;
printf("%d",sizeof(ptr));
指针大小为 8 个字节。
我得到的 sizeof(struct node) 为 12,next 指针在给定结构节点中的大小如何 12.Please 帮助我理解. 提前谢谢你。
指针的大小是存储地址所需的字节数:
printf("%zu",sizeof(ptr));
printf("%zu",sizeof(struct node *));
printf("%zu",sizeof &abc);
以上各项在 64 位地址的机器上应该 return 8
,在 32 位地址的机器上 4
。
可以通过取消引用指针来获得节点的大小:
struct node abc;
void *ptr = &abc;
printf("%zu",sizeof(*((struct node *)ptr)));
上面应该return12
在64位地址的机器上,如上所述。
sizeof(pointer)
是一个常量,无论它在最常见的现代系统上指向的是哪种普通旧数据类型。既然你这样做了:
sizeof(ptr)
并得到 8 bytes
我会冒险猜测您使用的是 64 位系统。这向我表明你的 sizeof(struct node)
将是 12 个字节,因为你有以下内容:
struct node {
int data; // 4 Bytes (32 bit, a common size for `int`s)
struct node* next; // 8 Bytes, as are all pointers on your system
}; // total size of 12 bytes.
在典型的系统上,指针的大小与其指向的数据的大小无关。在32位系统上,指针是32位(4字节),在64位系统上,指针是64位(8字节)。
您的结构大概是 12 个字节,因为它包含一个 4 字节的 int
和一个 8 字节的指针。但是,这是 platform-specific 并且可能会有所不同。许多系统要求值 aligned 为其大小的整数倍——也就是说,64 位指针必须从 8 字节的倍数的地址开始。编译器将在结构成员之间插入填充以满足对齐要求。
在我的 x86-64 Linux 系统上,您的结构大小为 16 个字节:int
4 个字节,4 个字节的填充以达到 8 个字节的边界,8 个指针的字节数。
What will be the size of pointer next?
尺码是sizeof(struct node*)
不应根据特定结果编写代码。
结果可能是 4 或 8,或 1 或 16 或其他。
编写可移植 C 代码依赖于不知道准确的答案,除了是一些理智的范围,如 1 到 64。
OP 没有提到需要知道 sizeof(ptr)
大小的原因,但答案只是指针的大小是 sizeof(ptr)
。代码应该使用 sizeof(ptr)
而不是像 4 或 8 这样的幻数。
要打印一个指针的大小,使用
some_type* ptr;
// printf("%d",sizeof(ptr));
printf("%zu",sizeof(ptr));
z Specifies that a following d, i, o, u, x, or X conversion specifier applies to a
size_t
C11dr §7.21.6.1 7
int *
、const int *
、void *
、int (*)()
等指针的大小可能不同。可移植代码不假定指向各种类型的所有指针都具有相同的大小。