__builtin_object_size

本文深入解析GCC内置函数__builtin_object_size的功能与用法,该函数用于返回指针指向对象的大小,尤其在-O2及以上优化级别下效果显著。文章详细介绍了函数原型、参数含义、返回值说明,并通过实例演示了不同情况下函数的行为表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:

https://www.ibm.com/support/knowledgecenter/en/SSXVZZ_16.1.0/com.ibm.xlcpp161.lelinux.doc/compiler_ref/bif_gcc_objectsize__builtin_object_size.html

 

Purpose

When used with -O2 or higher optimization, returns a constant number of bytes from the given pointer to the end of the object pointed to if the size of object is known at compile time.

Prototype

size_t __builtin_object_size (void *ptr, int type);

Parameters

ptr

The pointer of the object.

type

An integer constant that is in the range 0 - 3 inclusive. If the pointer points to multiple objects at compile time, type determines whether this function returns the maximum or minimum of the remaining byte counts in those objects. If the object that a pointer points to is enclosed in another object, type determines whether the whole variable or the closest surrounding subobject is considered to be the object that the pointer points to.

Return value

Table 1 describes the return values of this built-in function when both of the following conditions are met.

  • -O2 or higher optimization level is in effect.
  • The objects that ptr points to can be determined at compile time.

If any of these conditions are not met, this built-in function returns the values as described in Table 2.

Table 1. Return values when both conditions are met
typeReturn value
0The maximum of the sizes of all objects. The whole variable is considered to be the object that ptr points to.
1The maximum of the sizes of all objects. The closest surrounding variable is considered to be the object that ptr points to.
2The minimum of the sizes of all objects. The whole variable is considered to be the object that ptr points to.
3The minimum of the sizes of all objects. The closest surrounding variable is considered to be the object that ptr points to.
Table 2. Return values when any conditions are not met
typeReturn value
0(size_t) -1
1(size_t) -1
2(size_t) 0
3(size_t) 0

NoteIBM® XL C/C++ for Linux, V16.1 does not support the multiple targets and closest surrounding features. You can assign a value in the range 0 - 3 to type, but the compiler behavior is as if type were 0.

Examples

Consider the file myprogram.c:

#include "stdio.h"

int func(char *a){
  char b[10];
  char *p = &b[5];
  printf("__builtin_object_size(a,0):%ld\n",__builtin_object_size(a,0));
  printf("__builtin_object_size(b,0):%ld\n",__builtin_object_size(b,0));
  printf("__builtin_object_size(p,0):%ld\n",__builtin_object_size(p,0));
  return 0;
}

int main(){
  char a[10];
  func(a);
  return 0;
} 
  • If you compile myprogram.c with the -O option, you get the following output:
    __builtin_object_size(a,0):10
    __builtin_object_size(b,0):10
    __builtin_object_size(p,0):5
  • If you compile myprogram.c with the -O and -qnoinline options, you get the following output:
    __builtin_object_size(a,0):-1   
    /* The objects the pointer points to cannot be determined at compile time. */
    __builtin_object_size(b,0):10
    __builtin_object_size(p,0):5

Parent topic:

GCC object size checking built-in functions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值