由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一道c/c++题
相关主题
c的问题请帮忙看看这个字符函数的错误在哪里
New C++ programmer, need to ask a I/O file read question请问strcpy()和memcpy()的写法问题  (转载)
a c++ question.C pass string 问题
c++ 中如何把str转换为float?帮忙看看这几段程序有问题吗?
帮忙找个错再问一个free()的问题
呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题C的问题,困惑中
谁帮我解释一下这个代码Array in C
printf("%s\n", NULL)的结果C++ Q110: Add without +
相关话题的讨论汇总
话题: char话题: str话题: return话题: strcpy话题: int
进入Programming版参与讨论
1 (共1页)
g*****1
发帖数: 998
1
char *m()
{
char str[50];
strcpy(str,"how are you");
return str;
}
int main()
{
char s[50];
strcpy(s,m());
printf("%s",s);
//cin.get();
return 0;
}
为什么结果可以正确输出呢?我知道return by pointer可以make copy,可是return之
后storage不是free了吗?
另外,为什么下面这个就只能由一部分正确输出?
char *m()
{
char str[20];
strcpy(str,"how are you");
return str;
}
int main()
{

printf("%s",m());
//cin.get();
return 0;
}
然后上面char str[20];改成比如char str[50],输出就完全是乱得了
X****r
发帖数: 3557
2
The behavior of such program is undefined, meaning that they could produce
any result, from crashing to the 'right' output. It is pointless to try to
understand when it will produce the 'right' result, for you can't rely on
that anyway. Just don't do it.

【在 g*****1 的大作中提到】
: char *m()
: {
: char str[50];
: strcpy(str,"how are you");
: return str;
: }
: int main()
: {
: char s[50];
: strcpy(s,m());

d****n
发帖数: 1637
3
this makes the array safe.
char *m()
{
Static char str[50];
strcpy(str,"how are you");
return str;
}
或者这样 thread safe
char * m(){
char *ret=(char *)malloc(sizeof(char)*20);
strcpy(str, "I am good");
return str;
}
main(){
char * str=m();

printf("%s\n",str);
free(str);
}
l********a
发帖数: 1154
4
汗...二把刀是不行啊,
我才发现我刚写完的一个东西就这样return pointer的
这样不好是吗?那要return pointer是不是必须改成
void foobar(type *temp)这种来修改temp?
问题是我有时候不知道这个数组多长,例如10个int,但是这个10写在文件中,我得读了才
能知道
我现在是 int *foobar(char *fname)这样进去读第一行读到一个整数10
然后才int *temp = new int[10];
最后return temp;
这种情况怎么办呢?
d****n
发帖数: 1637
5
没太明白。
你每行都有不同个数的 int?
怎么分隔的?
如果是text , 用tab 或 ,分隔的。就用getline 写个小东西。
得出多少个int。再用new 就搞订了。
但是最后要注意garbage collection

【在 l********a 的大作中提到】
: 汗...二把刀是不行啊,
: 我才发现我刚写完的一个东西就这样return pointer的
: 这样不好是吗?那要return pointer是不是必须改成
: void foobar(type *temp)这种来修改temp?
: 问题是我有时候不知道这个数组多长,例如10个int,但是这个10写在文件中,我得读了才
: 能知道
: 我现在是 int *foobar(char *fname)这样进去读第一行读到一个整数10
: 然后才int *temp = new int[10];
: 最后return temp;
: 这种情况怎么办呢?

l*********s
发帖数: 5409
6
lz is C and stack memory, and you are C++ and heap memory. So you are safe,
just don't forget to delete temp in the caller.

【在 l********a 的大作中提到】
: 汗...二把刀是不行啊,
: 我才发现我刚写完的一个东西就这样return pointer的
: 这样不好是吗?那要return pointer是不是必须改成
: void foobar(type *temp)这种来修改temp?
: 问题是我有时候不知道这个数组多长,例如10个int,但是这个10写在文件中,我得读了才
: 能知道
: 我现在是 int *foobar(char *fname)这样进去读第一行读到一个整数10
: 然后才int *temp = new int[10];
: 最后return temp;
: 这种情况怎么办呢?

n*******e
发帖数: 62
7

bad style, though valid, but still not array safe. It returns a "char*",
that means the caller can change the contents by using another strcpy, say:
char* bad = m();
strcpy(bad, "1234567890 1234567890 1234567890 1234567890 1234567890
1234567890 1234567890");
you know what will happen.
Don't see how this could make it thread safe? I'm not expert on multiple
threading, but first impression, it is not true.

【在 d****n 的大作中提到】
: this makes the array safe.
: char *m()
: {
: Static char str[50];
: strcpy(str,"how are you");
: return str;
: }
: 或者这样 thread safe
: char * m(){
: char *ret=(char *)malloc(sizeof(char)*20);

p*********t
发帖数: 2690
8
char *ret?

【在 d****n 的大作中提到】
: this makes the array safe.
: char *m()
: {
: Static char str[50];
: strcpy(str,"how are you");
: return str;
: }
: 或者这样 thread safe
: char * m(){
: char *ret=(char *)malloc(sizeof(char)*20);

p*********t
发帖数: 2690
9
2网友的意见不同,哪个是对的?
newpolice -- function char *m() {} is valid
Xentar -- it's undefined.

char *m()
{
char str[50];
strcpy(str,"how are you");
return str;
}
int main()
{
char s[50];
strcpy(s,m());
printf("%s",s);
//cin.get();
return 0;
}
为什么结果可以正确输出呢?我知道return by pointer可以make copy,可是return之
后storage不是free了吗?
另外,为什么下面这个就只能由一部分正确输出?
char *m()
{
char str[20];
strcpy(str,"how are you");
return str;
}
int main()
{

printf("%s",m());
//cin.get();
return 0;
}
然后上面char str[20];改成比如char str[50],输出就完全是乱得了

【在 g*****1 的大作中提到】
: char *m()
: {
: char str[50];
: strcpy(str,"how are you");
: return str;
: }
: int main()
: {
: char s[50];
: strcpy(s,m());

t****t
发帖数: 6806
10
你看这ID, 你觉得应该信谁?

【在 p*********t 的大作中提到】
: 2网友的意见不同,哪个是对的?
: newpolice -- function char *m() {} is valid
: Xentar -- it's undefined.
:
: char *m()
: {
: char str[50];
: strcpy(str,"how are you");
: return str;
: }

相关主题
呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题请帮忙看看这个字符函数的错误在哪里
谁帮我解释一下这个代码请问strcpy()和memcpy()的写法问题  (转载)
printf("%s\n", NULL)的结果C pass string 问题
进入Programming版参与讨论
p*********t
发帖数: 2690
11
Xentar当然是对的,局部变量和全局变量的区别,出了作用范围就会被释放。

【在 t****t 的大作中提到】
: 你看这ID, 你觉得应该信谁?
a****l
发帖数: 8211
12
hahaha, didn't notice that...

【在 t****t 的大作中提到】
: 你看这ID, 你觉得应该信谁?
n*******e
发帖数: 62
13
请大家眼睛睁大一点.我说的是下面的:

>>>>: Static char str[50];
而不是:

【在 d****n 的大作中提到】
: this makes the array safe.
: char *m()
: {
: Static char str[50];
: strcpy(str,"how are you");
: return str;
: }
: 或者这样 thread safe
: char * m(){
: char *ret=(char *)malloc(sizeof(char)*20);

d****n
发帖数: 1637
14
好吧,非要较真。
static in function .那块地方总是你的。
在你把临时内容取走之前,总是安全的。(前提是只有一个thread。多个thread就会有
麻烦。)
用多大,是你自己的事情。
你非要定义长度50, 拷贝100个字符,我也没办法。
用这个定义的最大的好处是--再你知道不会越界的前提下--每次不用分配内存空间。
你不会不知到malloc的时间要所消耗吧?
感兴趣的花自己作个benchmark.看看那个快。
char *func(){ static char * mychar[MAXCHAR]; ;;;; return mychar ;}
char *mallocFun(){chat *ret=(char *)malloc(sizeof(char)*MAXCHAR); return ret
;}
in main
main(){
{
int i;
for(i=0;i<100000;++i)
func();

}
{
int i;
for (i=0;i<100000;++i){
char *tmp=mallocFun();
free(tmp);
}
}
}
还有一点typo抓住不放,有意思么?

【在 n*******e 的大作中提到】
: 请大家眼睛睁大一点.我说的是下面的:
:
: >>>>: Static char str[50];
: 而不是:

X****r
发帖数: 3557
15
The standard way to do it is to let caller provide space along
with its size. This is both safer and more flexible, at the cost
of slightly more inconvenient to use.

【在 d****n 的大作中提到】
: 好吧,非要较真。
: static in function .那块地方总是你的。
: 在你把临时内容取走之前,总是安全的。(前提是只有一个thread。多个thread就会有
: 麻烦。)
: 用多大,是你自己的事情。
: 你非要定义长度50, 拷贝100个字符,我也没办法。
: 用这个定义的最大的好处是--再你知道不会越界的前提下--每次不用分配内存空间。
: 你不会不知到malloc的时间要所消耗吧?
: 感兴趣的花自己作个benchmark.看看那个快。
: char *func(){ static char * mychar[MAXCHAR]; ;;;; return mychar ;}

l********a
发帖数: 1154
16
谢谢回复.我的问题是这样的,例如一个文件写了
10 5表示是10*5的矩阵
下面是50个float
读取的时候先getline得到10和5,然后调用我的函数
float **initMat(int m, int n)生成10*5的二维矩阵,返回指针**
这个函数内部大概是:
float **initMat(int m, int n)
{
float **mat = new float*[m];
for (int i=0;i {
m[i] = new float[n];
for (int j=0;j m[i][n] = 0.0;
}

return mat;
}
然后这个返回的数组我整个程序都有用,在程序结束有个专门的free函数
void freeMat(float **mat, int m)
{
for (int i=0;i if(mat!=NULL) delete mat;
mat = NULL;
}
这样有问题吗?多谢了

【在 d****n 的大作中提到】
: 没太明白。
: 你每行都有不同个数的 int?
: 怎么分隔的?
: 如果是text , 用tab 或 ,分隔的。就用getline 写个小东西。
: 得出多少个int。再用new 就搞订了。
: 但是最后要注意garbage collection

d****n
发帖数: 1637
17
I agree. Glibc and linux source code 都是这么规范的。
但是情况不同,也不要刻舟求剑。
譬如,你有100million 个相同长度51个char。
你会考虑用那个方法呢?
100million个malloc?
还是保证知道最大长度情况下,用static var?
知道别人怎么用的是好的,但是你要考虑自己实际情况。

【在 X****r 的大作中提到】
: The standard way to do it is to let caller provide space along
: with its size. This is both safer and more flexible, at the cost
: of slightly more inconvenient to use.

d****n
发帖数: 1637
18
没问题,既然你都知道是10 by 5 的matrix.
不过你也可一这么写。
//creation
float *myarr= new float *[m*n];
//deletion
if (myarr!=NULL ) delete [] myarr;
//iteration
int r, c;
for(r=0;r for(c=0;c cout< //transpose 2D array to 1 dimension
茴字有5种写法,呵呵。见笑了。

【在 l********a 的大作中提到】
: 谢谢回复.我的问题是这样的,例如一个文件写了
: 10 5表示是10*5的矩阵
: 下面是50个float
: 读取的时候先getline得到10和5,然后调用我的函数
: float **initMat(int m, int n)生成10*5的二维矩阵,返回指针**
: 这个函数内部大概是:
: float **initMat(int m, int n)
: {
: float **mat = new float*[m];
: for (int i=0;i
1 (共1页)
进入Programming版参与讨论
相关主题
C++ Q110: Add without +帮忙找个错
10个包子请教一个简单的编程问题呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题
weird output谁帮我解释一下这个代码
[合集] &x[1]和x+1是一回事吧?不管c还是c++?printf("%s\n", NULL)的结果
c的问题请帮忙看看这个字符函数的错误在哪里
New C++ programmer, need to ask a I/O file read question请问strcpy()和memcpy()的写法问题  (转载)
a c++ question.C pass string 问题
c++ 中如何把str转换为float?帮忙看看这几段程序有问题吗?
相关话题的讨论汇总
话题: char话题: str话题: return话题: strcpy话题: int