sizeof void或者函数

本文详细介绍了GCC环境下sizeof函数在void和函数类型上的使用方式,特别指出其大小为1,并提供了相关代码示例进行验证。

sizeof的标准规定:

https://en.cppreference.com/w/cpp/language/sizeof

sizeof cannot be used with function types, incomplete types, or bit-field glvalues.

When applied to a reference type, the result is the size of the referenced type.

When applied to a class type, the result is the size of an object of that class plus any additional padding required to place such object in an array.

The result of sizeof is always nonzero, even if applied to an empty class type.

但是,对于gcc来说:

http://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html

6.24 Arithmetic on void- and Function-Pointers

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.

The option -Wpointer-arith requests a warning if these extensions are used.

因此,对于void和函数来说,其sizeof为1.

例子:

#include <stdio.h>

int test(int i)
{
    i++;
}

int main(void)
{
    int tmp_int = 1;
    int *int_ptr = &tmp_int;
    void *void_ptr = &tmp_int;

    printf("func name size is: %u\n", sizeof(test));

    printf("void size is: %u\n", sizeof(*void_ptr));

    printf("int ptr is [%p]\n", int_ptr);
    int_ptr++;
    printf("int ptr ++ is [%p]\n", int_ptr);

    printf("void ptr is [%p]\n", void_ptr);
    void_ptr++;
    printf("void ptr ++ is [%p]\n", void_ptr++);

    return 0;
}

输出:

func name size is: 1
void size is: 1
int ptr is [0x7fff97dec65c]
int ptr ++ is [0x7fff97dec660]
void ptr is [0x7fff97dec65c]
void ptr ++ is [0x7fff97dec65d]

 

在C语言中,`sizeof` 是一个非常常用的运算符,用于获取数据类型、变量或表达式在内存中所占的字节数。它并不是一个函数,而是一个单目操作符,其行为类似于其他C语言操作符如 `++` 或 `--` [^3]。 ### 作用 1. **返回对象或类型的字节大小**:`sizeof` 的主要用途是确定某个特定数据类型(如 `int`、`float`)或变量在当前系统架构下的内存占用大小(以字节为单位)。 2. **支持静态数组大小计算**:可以使用 `sizeof` 来获取静态数组的整体大小,这在进行内存拷贝或初始化时非常有用。 3. **可用于类型和表达式**:`sizeof` 的操作数既可以是一个类型名(例如 `sizeof(int)`),也可以是一个具体的变量或表达式(例如 `sizeof(x)` 或 `sizeof(x + y)`)[。 ### 使用方法 1. **基本语法形式**: - `sizeof(type)`:用于获取某种数据类型的大小。 - `sizeof(object)`:用于获取某个变量或表达式的大小。 - `sizeof object`:当操作数是变量时,括号可以省略。 示例: ```c int a; printf("Size of int: %zu\n", sizeof(int)); // 获取类型大小 printf("Size of variable a: %zu\n", sizeof(a)); // 获取变量大小 printf("Size of expression: %zu\n", sizeof(a + 0.5)); // 获取表达式结果类型大小 ``` 2. **指针与数组**: - 对于指针,在32位系统上,无论指向什么类型,`sizeof` 返回的值都是4字节;在64位系统上则是8字节。 - 当应用于静态数组时,`sizeof` 返回整个数组所占的总字节数,而不是仅仅指针大小。 示例: ```c int arr[10]; printf("Size of array: %zu\n", sizeof(arr)); // 输出 10 * sizeof(int) ``` 3. **结构体与联合体**: - `sizeof` 可以用来获取结构体 (`struct`) 和联合体 (`union`) 的大小,需要注意的是结构体内存对齐会影响最终的结果。 ### 注意事项 - `sizeof` 不能用于 `void` 类型,但可以用于 `void*` 指针。 - 在处理字符串时,要注意 `sizeof` 和 `strlen` 的区别:`sizeof` 计算包括终止符 `\0` 在内的所有字节,而 `strlen` 仅统计字符数直到遇到 `\0` 为止 [^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值