返回值问题及goto问题
2014-06-06 01:59
225 查看
应用调用驱动时返回时,int do_sys_poll()返回类型是int ,其返回值fdcount应该为1,但是并没有return语句,所以做这个测试(对do_sys_poll中的goto理解有误)
gcc版本:gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)
ubuntu版本:ubuntu 9.10
printchar返回类型为int,但是函数没有return语句
hello.c:
增加printchar中的a数目,然后编译执行,其打印如下:
hello.c
运行结果:
由程序及运行结果可知,goto out语句没有执行,循环完了后,执行a=3, printf("a = %d", a); return 5;所以说out这段代码goto out可以用,goto out不执行(数组中没有大于0的数)时即数组查询完毕后执行执行a=3和out代码printf("a = %d", a); return 5;
do_sys_poll函数实现源码,此与forth_drv实验poll机制有关:
若do_poll调用完后返回值为1即fdcount =1,若goto out_fds的while,if条件满足,就直接跳到out_fds标号处,最后return err相当于 return -EFAULT,若while if的条件不满足不执行goto out_fds,则err = fdcount =1,由于fdcount
=1,则if条件为if(0)
poll机制留待下回分析
gcc版本:gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)
ubuntu版本:ubuntu 9.10
printchar返回类型为int,但是函数没有return语句
hello.c:
#include<stdio.h> int printchar(void) { printf("aa\n"); } int main() { int ret=0; ret = printchar(); printf("ret = %d\n", ret); return 0; }
cc hello.c root@book-desktop:/home/book# ./a.out aa ret = 3
增加printchar中的a数目,然后编译执行,其打印如下:
root@book-desktop:/home/book# cc hello.c root@book-desktop:/home/book# ./a.out aaaaaaaaaa ret = 11 root@book-desktop:/home/book# vim hello.c root@book-desktop:/home/book# cc hello.c root@book-desktop:/home/book# ./a.out aaaaaaaaaaa ret = 12
hello.c
#include<stdio.h> int main() { int ret[3]={0,0,0}, i; int a=0; for(i=0; i<3; i++) if(ret[i]>0) goto out; a= 3; out: printf("a = %d\n", a); return 5; return a; }
运行结果:
root@book-desktop:/home/book# cc hello.c root@book-desktop:/home/book# ./a.out a = 3 root@book-desktop:/home/book# echo $? 5
由程序及运行结果可知,goto out语句没有执行,循环完了后,执行a=3, printf("a = %d", a); return 5;所以说out这段代码goto out可以用,goto out不执行(数组中没有大于0的数)时即数组查询完毕后执行执行a=3和out代码printf("a = %d", a); return 5;
do_sys_poll函数实现源码,此与forth_drv实验poll机制有关:
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout) { struct poll_wqueues table; int fdcount, err; unsigned int i; struct poll_list *head; struct poll_list *walk; /* Allocate small arguments on the stack to save memory and be faster - use long to make sure the buffer is aligned properly on 64 bit archs to avoid unaligned access */ long stack_pps[POLL_STACK_ALLOC/sizeof(long)]; struct poll_list *stack_pp = NULL; /* Do a sanity check on nfds ... */ if (nfds > current->signal->rlim[RLIMIT_NOFILE].rlim_cur) return -EINVAL; poll_initwait(&table); head = NULL; walk = NULL; i = nfds; err = -ENOMEM; while(i!=0) { struct poll_list *pp; int num, size; if (stack_pp == NULL) num = N_STACK_PPS; else num = POLLFD_PER_PAGE; if (num > i) num = i; size = sizeof(struct poll_list) + sizeof(struct pollfd)*num; if (!stack_pp) stack_pp = pp = (struct poll_list *)stack_pps; else { pp = kmalloc(size, GFP_KERNEL); if (!pp) goto out_fds; } pp->next=NULL; pp->len = num; if (head == NULL) head = pp; else walk->next = pp; walk = pp; if (copy_from_user(pp->entries, ufds + nfds-i, sizeof(struct pollfd)*num)) { err = -EFAULT; goto out_fds; } i -= pp->len; } fdcount = do_poll(nfds, head, &table, timeout); /* OK, now copy the revents fields back to user space. */ walk = head; err = -EFAULT; while(walk != NULL) { struct pollfd *fds = walk->entries; int j; for (j=0; j < walk->len; j++, ufds++) { if(__put_user(fds[j].revents, &ufds->revents)) goto out_fds; } walk = walk->next; } err = fdcount; if (!fdcount && signal_pending(current)) err = -EINTR; out_fds: walk = head; while(walk!=NULL) { struct poll_list *pp = walk->next; if (walk != stack_pp) kfree(walk); walk = pp; } poll_freewait(&table); return err; }
若do_poll调用完后返回值为1即fdcount =1,若goto out_fds的while,if条件满足,就直接跳到out_fds标号处,最后return err相当于 return -EFAULT,若while if的条件不满足不执行goto out_fds,则err = fdcount =1,由于fdcount
=1,则if条件为if(0)
if (!fdcount && signal_pending(current)) err = -EINTR;然后执行out_fds段代码,此时return err相当于return 1即do_sys_poll的返回值为1
poll机制留待下回分析
相关文章推荐
- 数据访问类用静态方法。。。当多个人调用时返回值会返回别人的?。。。特别奇怪的问题???
- 使用SqlParameter参数返回值时遇到的问题
- 返回值问题:使用了未赋值的局部变量
- 关于goto语句能不能从复杂的嵌套循环判断中跳出去的问题讨论
- 关于goto语句能不能从复杂的嵌套循环判断中跳出去的问题讨论
- 数据导入问题参考http://www.itpub.net/redirect.php?tid=888849&goto=lastpost
- ExecuteNonQuery()返回值问题
- 注意 ExecuteNonQuery() 返回值 问题
- 编程规范,try catch 和返回值的问题
- 使用VC6调用DLL出现的问题(使用LoadLibrary返回值为0)
- 一点点小问题,关于用传址返回值类型API的返回值处理问题
- JS正则替换问题 http://bbs.51js.com/redirect.php?fid=1&tid=67418&goto=nextnewset
- 存储过程中,goto语句和if else语句注意,标签的位置问题导致条件语句失效
- 一个C++返回值的问题
- 返回值为静态存储区的库函数的多线程问题
- 存储过程返回值问题
- 我学员的一个问题及其我对之的解答,关于lr返回值问题
- 完成端口GetQueuedCompletionStatus返回值的问题
- 关于goto语句能不能从复杂的嵌套循环判断中跳出去的问题讨论
- 关于goto语句能不能从复杂的嵌套循环判断中跳出去的问题讨论