返回   华枫论坛 > ◆ 工作学习◆ > IT交流



发表新主题 回复
 
只看楼主 主题工具
旧 Apr 14th, 2005, 21:45     #1
jhn
Senior Member
级别:4 | 在线时长:36小时 | 升级还需:9小时级别:4 | 在线时长:36小时 | 升级还需:9小时级别:4 | 在线时长:36小时 | 升级还需:9小时级别:4 | 在线时长:36小时 | 升级还需:9小时
 
注册日期: Jul 2004
帖子: 133
jhn is an unknown quantity at this point
默认 C问题请教:

2000: char * getbuf()
2001: {
2002: char buff[8];
2003: /* unspecified, buff defined here *./
2051: return (char *) buff;
2055: }

1. Is there an alternative, but equivalent, way to write line 2000? If so, what is it?
2. Is getbuf() a reasonable function?
3. Will getbuf() execute at all?
4. Please comment on line 2051.
5. Is getbuf() good practice, and why?
jhn 当前离线  
回复时引用此帖
旧 Apr 14th, 2005, 22:55   只看该作者   #2
ACDC
Senior Member
级别:1 | 在线时长:11小时 | 升级还需:1小时
 
注册日期: Sep 2004
帖子: 109
声望: 10
ACDC is on a distinguished road
默认

引用:
作者: jhn
2000: char * getbuf()
2001: {
2002: char buff[8];
2003: /* unspecified, buff defined here *./
2051: return (char *) buff;
2055: }

1. Is there an alternative, but equivalent, way to write line 2000? If so, what is it?
2. Is getbuf() a reasonable function?
3. Will getbuf() execute at all?
4. Please comment on line 2051.
5. Is getbuf() good practice, and why?
My 2 bits:
1. Not sure what you mean, it can be written as char* getbuf() ?
2. Not sure what you mean again. Depends on what you want to do with it.
3. It will execute but will cause a memory error.
4. It's returning a pointer to a mem location on stack. The mem allocated
on stack will be destroyed after line 2051 and hence the caller is now
having a pointer to an already 'destroyed' memory cells. The caller may
behave normally most of the time if the over all application is relatively
queisent. But at times it would crash 'mysteriously'.
5. getbuf() does not take any arguments. To return a pointer to the buffer,
it has to either use some global memory or it has to use malloc to get
mem on the heap. Using global is not desirable in most cases. Using malloc
in this context is slightly better if a corresponding function freebuf(char *)
is made available to the caller. My preference would be a function like
boolean getbuf(
char* pbuf, /* IN/OUT pointer to buffer */
int sizeOfBuf /* Max size to fill */
) {
....
// return whether it's success or not.
}
ACDC 当前离线  
回复时引用此帖
旧 Apr 14th, 2005, 22:59   只看该作者   #3
dongningprc
统一中国
级别:16 | 在线时长:337小时 | 升级还需:20小时级别:16 | 在线时长:337小时 | 升级还需:20小时级别:16 | 在线时长:337小时 | 升级还需:20小时级别:16 | 在线时长:337小时 | 升级还需:20小时
 
注册日期: Jul 2004
帖子: 4,243
积分:1
精华:1
声望: 22914575
dongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond reputedongningprc has a reputation beyond repute
默认

A better way to write it:
static char buff[8];
dongningprc 当前离线  
回复时引用此帖
旧 Apr 14th, 2005, 23:38   只看该作者   #4
ACDC
Senior Member
级别:1 | 在线时长:11小时 | 升级还需:1小时
 
注册日期: Sep 2004
帖子: 109
声望: 10
ACDC is on a distinguished road
默认

引用:
作者: dongningprc
A better way to write it:
static char buff[8];
I think the use of static would cause a re-entrant problem. i.e. If multiple
processes are using this function, there's a risk of caller A getting a value
that should belong to caller B.
ACDC 当前离线  
回复时引用此帖
旧 Apr 15th, 2005, 15:56   只看该作者   #5
beastmaster
Junior Member
级别:0 | 在线时长:1小时 | 升级还需:4小时
 
注册日期: Apr 2005
帖子: 27
声望: 2338
beastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond reputebeastmaster has a reputation beyond repute
默认 not thread safe

引用:
作者: ACDC
I think the use of static would cause a re-entrant problem. i.e. If multiple
processes are using this function, there's a risk of caller A getting a value
that should belong to caller B.
each process should have their own copy of this function, because each process has their own stack. and processes only can share something by IPC, but inside each process if there are multiple threads, then maybe will casue critical section problem, need lock to guarantee mutual exclusion. suppose all the codes are in user mode.
beastmaster 当前离线  
回复时引用此帖
发表新主题 回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码



所有时间均为格林尼治时间 -4。现在的时间是 12:29

请尊重文章原创者,转帖请注明来源及原作者。
凡是本站用户自行发布的任何信息,皆不代表本站的立场,
华枫网站不确保各类信息的正确性和可靠性,也不承担由此而导致的任何直接或间接损失以及任何法律责任。

Copyright © 1999-2024 Chinasmile