GCC关于内联的问题

1.没有内联的情况

#include <stdio.h>

void f() {
    printf("hello\n");
}

int main() {
    f();
//    void (*g)() = f;
//    g();
}
	.file	"inline.c"
	.section	.rodata
.LC0:
	.string	"hello"
	.text
.globl f
	.type	f, @function
f:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	movq	%rsp, %rbp
	.cfi_offset 6, -16
	.cfi_def_cfa_register 6
	movl	$.LC0, %edi
	call	puts
	leave
	ret
	.cfi_endproc
.LFE0:
	.size	f, .-f
.globl main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	movq	%rsp, %rbp
	.cfi_offset 6, -16
	.cfi_def_cfa_register 6
	movl	$0, %eax
	call	f
	leave
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
	.section	.note.GNU-stack,"",@progbits


惊奇的是不管我在f加上inline还是没有,结果都是一样的,google一下,有人说是默认情况是不会优化的,加上-O后编译器会自动优化!

 

让后我加上-O,结果

	.file	"inline.c"
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"hello\n"
	.text
.globl f
	.type	f, @function
f:
.LFB22:
	.cfi_startproc
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	movl	$.LC0, %esi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk
	addq	$8, %rsp
	ret
	.cfi_endproc
.LFE22:
	.size	f, .-f
.globl main
	.type	main, @function
main:
.LFB23:
	.cfi_startproc
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	movl	$.LC0, %esi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk
	addq	$8, %rsp
	ret
	.cfi_endproc
.LFE23:
	.size	main, .-main
	.ident	"GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
	.section	.note.GNU-stack,"",@progbits


结果确实会优化,其实我的目的是要看看inline传给一个指针函数事,是否被优化(inline 可以被优化,是不是inline没有地址?,其实inline是有地址的!看看上面第一个汇编就知道了!)

我们试一试(把所有的注释去掉)

	.file	"inline.c"
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"hello\n"
	.text
.globl f
	.type	f, @function
f:
.LFB22:
	.cfi_startproc
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	movl	$.LC0, %esi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk
	addq	$8, %rsp
	ret
	.cfi_endproc
.LFE22:
	.size	f, .-f
.globl main
	.type	main, @function
main:
.LFB23:
	.cfi_startproc
	subq	$8, %rsp
	.cfi_def_cfa_offset 16
	movl	$.LC0, %esi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk
	movl	$.LC0, %esi
	movl	$1, %edi
	movl	$0, %eax
	call	__printf_chk
	addq	$8, %rsp
	ret
	.cfi_endproc
.LFE23:
	.size	main, .-main
	.ident	"GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5"
	.section	.note.GNU-stack,"",@progbits

被优化了!

这里是由于我在编写C语言的排序代码时遇到的问题

 

 

#include <stdio.h>
#include <string.h>

//get the length of array
#define len(array) \
	(sizeof(array) / sizeof(array[0]))

//define a array and a size of its
typedef struct{
	int *array;
	int length;
}DArray;

//define a func pointer 
typedef void (* ArrSort)(DArray,int,int);
typedef void (*ArrPrintf)(int *,int);

//define a struct to contain functions which can sort array
typedef struct{
	ArrSort quick2Sort;
	ArrSort bubbleSort;
	ArrPrintf arrPrint;	
	DArray arr;	
}ArrFuc;

//void APrintf(int *arr,int count);
//define a quick sort 
void Quick2Sort(DArray arr,int left,int right)
{
//	printf("data is %d",arr.length);
//	APrintf(arr.array,arr.length);

	if(left >= right)return;//one sort end,and next
	int aleft 	= left;
	int aright	= right;
	int *arrData	= arr.array;
	int key		= arrData[aleft];//get a num to sort
	
	while(aleft < aright)
	{
		while(aleft < aright && arrData[aright] >= key)
		{
			--aright;
		}
		arrData[aleft] = arrData[aright];//move data to left
		 while(aleft < aright && arrData[aleft] <= key)
                {
                        ++aleft;
                }
		arrData[aright] = arrData[aleft];
	}
	arrData[aleft] = key;

//	APrintf(arrData,arr.length);
	Quick2Sort(arr,left,aleft-1);	
	Quick2Sort(arr,aleft+1,right);
}

void APrintf(int *arr,int count);
// bubble sort
void BubbleSort(DArray arr,int count,int none)
{
	int i=0,j=0,temp=0;
	for(;i < count - 1;i++)
	{
		for(j=0;j < (count - i - 1);j++)
		{
			if(arr.array[j] > arr.array[j+1])
			{
				temp = arr.array[j];
				arr.array[j] = arr.array[j+1];
				arr.array[j + 1] = temp;
				//APrintf(arr.array,10);
			}
		}
//				APrintf(arr.array,10);
	}
}

//print array
inline void APrintf(int *arr,int count)
{
	int i = 0;
	for(;i<count;i++)
	{
		printf("%d ",arr[i]);
	}
	printf("\n");
}

int main()
{
	ArrFuc func;
	DArray arrp;
	//init func var
	memset((void *)&func,0,sizeof(func));
	func.quick2Sort = &Quick2Sort;
	func.bubbleSort = &BubbleSort;
	func.arrPrint = &APrintf;

	int arr[10] = {2,11,22,34,5,56,3,2,1,62};
	arrp.array = arr;
	arrp.length=len(arr);
	func.arr = arrp;

	//call function in func and use array in func
//	func.quick2Sort(func.arr,0,9);
//	func.arrPrint(func.arr.array,func.arr.length);
	
#define NONE 0
	func.bubbleSort(func.arr,func.arr.length,NONE);
	func.arrPrint(func.arr.array,func.arr.length);

	return 0;
}


 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值