由买买提看人间百态

topics

全部话题 - 话题: fun1
1 (共1页)
p*********t
发帖数: 2690
1
fun1()在返回一个局部变量的地址,所以输出的是這個地址的值,因为fun1()的局部变
量在函数结束时消失,那个地址可能又被分配了新的值,所以是乱码。
要想输出abc,可以在fun1()的str设为static,这样即使fun1()函数结束,str[]还是在
内存存在。
char *fun1() {
static char str[]="ABC";
return (str);
}

有下面这段code, 为什么fun1()输出乱码, 而fun2()输出正确: ABC.
====== code ============
#include
int main() {
char *fun1(), *fun2();
printf("%s\n%s\n", fun1(), fun2());
return 0;
}
char *fun1() {
char str[]="ABC";
return (str);
}
char *fun2() {
char (*str)[]="ABC";
return (str);
... 阅读全帖
n*******r
发帖数: 22
2
class A
{
private:
int n;
public:
virtual void Fun1(int no=10)
{
n = no;
cout<<"A::Fun1"< }
};
class B : public A
{
private:
int m;
public:
virtual void Fun1(int no=20)
{
m = no;
cout<<"B::Fun1()"< }
}
int main()
{
B b;
A &a =b;
a.Fun1();
return 0;
}
这个程序的输出是 B::Func1()10
实在想不明白为什么输出10,而不是20.
n*******r
发帖数: 22
3
【 以下文字转载自 JobHunting 讨论区 】
发信人: newdriver (小小的光), 信区: JobHunting
标 题: 问一个C++问题:default parameter and overriding/inheritanc
发信站: BBS 未名空间站 (Tue Feb 22 01:33:31 2011, 美东)
class A
{
private:
int n;
public:
virtual void Fun1(int no=10)
{
n = no;
cout<<"A::Fun1"< }
};
class B : public A
{
private:
int m;
public:
virtual void Fun1(int no=20)
{
m = no;
cout<<"B::Fun1()"< }
}
int main()
{
B b;
A &a =b;
a.Fun1();
return 0;
}
这个程序的输出是 B::Func1()10
实在想不明白为什么输出... 阅读全帖
l*******h
发帖数: 3
4
来自主题: Programming版 - C/C++函数调用和栈内存
这几天学C语言学晕了,都因是看内存管理变量分配的问题.现在看RETURN语句都不顺眼.
比如下面第一个CODE,因为B指针指向栈内存,离开fun2()就失效,所以返回main()的结果
不正确.
int *fun2(void)
{
int b=2;
return &b;
}
main()
{
int *b1=fun2();
printf(“%\n”, *b1);
}
那下面的CODE就对了吗? 变量b在fun1()子程序里不也是在栈内存吗?它离开fun1()子程
序不也无效了吗?怎能指望返回MAIN正确的值呢? RETURN 这个命令是怎么和把什么返回
主函数的?
int fun1(void)
{
int b=2;
return b;
}
main()
{
int b1=fun1();
printf(“%\n”, b1);
}
m****s
发帖数: 1481
5
还是有点问题,大侠帮忙看下下面的代码,为什么在main里释放内存出错。
用pass by reference:
void fun1(int& *a, double& *b, char& *c){

a=new int[20];
b=new double[20];
c=new char[10];
for (int i=0;i<20;i++){
a[i]=i;
b[i]=sqrt((double)a[i]);
}
c="0123456789";
}
int _tmain(int argc, _TCHAR* argv[])
{
int *a;
double *b;
char *c;
fun1(a,b,c);
cout<<"a & b are:"< for(int i=0;i<20;i++)
cout< cout<<"c is: "< delete[] ... 阅读全帖
e*****e
发帖数: 1275
6
来自主题: Programming版 - 跪求大牛一个问题
如果有两个用户,分别有自己的code.
User A:
data...
code....
User B:
data...
code...
code 里面都有一个fun1()
如何让用户C可以同时用user A 和User B的那个fun1()
我初步的想法是user C, malloc 个大memory, 然后把user A, user B的code copy 过
去(假设user A, B,C的机器都是一样的)然后用个function pointer 指到user A的
fun1, 或者user B的fun1,可不可以呢?
因为code 需要change at run time,所以DLL就不用了。
还有什么其它的办法吗?
G****u
发帖数: 51
7
有下面这段code, 为什么fun1()输出乱码, 而fun2()输出正确: ABC.
====== code ============
#include
int main() {
char *fun1(), *fun2();
printf("%s\n%s\n", fun1(), fun2());
return 0;
}
char *fun1() {
char str[]="ABC";
return (str);
}
char *fun2() {
char (*str)[]="ABC";
return (str);
}
h**********c
发帖数: 4120
8
来自主题: Programming版 - ask Big Niu industrial code 都是怎麼寫的
These things are useful quite placebo.
Fundamental I believe is still design.
Let's talk about pass by ref in Java. You encapsulate something in a class.
This class should take care of this member variable. Ok, they just pass by
ref, get the ref, modify change them even new a new one in another class and
set it back.
Cross call function. lets say a.fun1() call b.fun2(), .... finally z.fun26()
call a.fun1(). They dont' even know what is called dependency graph.In fact
, they know, they just don't... 阅读全帖
b******s
发帖数: 345
9
运行出现的warning是"starting a module while inside s do group" "finish a
module while inside a do group", 该怎样改正这段出现问题的程序呢,谢谢!下面
是出现问题的部分程序。
do i = 1 to n;
if npf > 3 then do;
WFI = XF[i,3:npf-1];
end;
else WFI = 0;
if npu > 2 then do;
WUI = Z[i,3:npu];
end;
else WUI = 0;
lbyw = byw * WFI`;
lbuw = buw * WUI`;
mux1 = bu0 + bux + lbuw;
mux0 = bu0 + lbuw;
start fun1(u) global(by0, byx, lbyw, byu, mux1);
v = (1/(1+exp(-(by0 + byx + lbyw + byu*u ))))/sqrt(2*constant('pi'))
* exp((-1/2)*(... 阅读全帖
z***e
发帖数: 5393
10
之前我发过个帖子讨论科班和非科班的问题,这就是个例子。
不要被什么vtbl之类的束缚了,想一下如果你写一个compiler会怎么处理。
compiler读到这个a.Fun1(),首先去看A是否有matching function signature(注意,
compiler不会在B的function table里面找,因为a是type A),找到了,然后看用什么
参数,生成大概如下代码:
push 10
call A::Func1
这里default parameter只是帮助source code少打几个字而已。
G****u
发帖数: 51
11
如果你把两个fun()分开, 各写一个main(), 每个main里printf一个fun, 那么gcc里面
都能输出ABC, 但是同样的两个程序, 放到一些online c compiler里面, main+fun1 还
是输出garbage, main+fun2 输出ABC
G****u
发帖数: 51
12
问题是为啥fun2 总能输出ABC,而fun1时而能输出ABC(gcc), 时而输出garbage? array
在C里面到底是怎么给造出来的, 一下子可以等同与pointer, 一下子不能, 这背后的原
因是啥.

ANY
t****t
发帖数: 6806
13
fun2没有错, 所以总能输出ABC, 虽然你把好好的函数写成了一滩那什么.
fun1有错, 能不能输出ABC要看天意.
至于array和pointer的关系, 请仔细阅读C FAQ第6章.
G****u
发帖数: 51
14
如果fun1改成fun3
char *fun3() {
char str[]="ABC";
char *p = str;
return (p);
}

address
c*******y
发帖数: 1630
15
fun3=fun1
fun2=
char **str="abc";
return str;
G****u
发帖数: 51
16
I think it does matter what str points to. If it points to something in
function stack (destroyed after function exits), then return (str) is wrong,
just like what fun1 does. However, if it points to something that has a
storage duration throughout the lifetime of program, then it is correct,
just like char *str = "ABC".
This is why I cannot understand your explanation of why fun2 is correct. yes
,
the caller get the value (the address to something) of str, but if something
is stored in functi... 阅读全帖
r***t
发帖数: 21
17
in fun1, str is a local char array which has "ABC", the space is local stack.
in fun2, str is a poiner, "ABC" is treated as constant string, which will be
allocated from global space.
j*****I
发帖数: 2626
18
用gdb跟了一下,
(gdb) info locals
c1 = 0x7fff96489d80 "ABC"
c2 = 0x4005ff "ABC"
fun1()返回的str确实是stack的地址。如果在func1里面加个printf("%d",str);什么的
,好像这个stack里面的string就hold住了,main打印出来的就不是乱码了。

stack.
be
B********r
发帖数: 397
19

would
In fun1, str is array name of "ABC". When you return an array name whose
content was destroyed after returning, you can't get a right result.
In fun2, str is a pointer which points to an array "ABC". Since "ABC" is
constant and stays in the memory and what you return is the actual address,
you can find "ABC" in main(). (*str)[] just different from *str[] as a
pointer, instead of a pointer array.
B********r
发帖数: 397
20
但我试了一下,如果在fun1里面用 char *str="ABC"确实就没问题了,如果局部变量消
失的话,这个str pointer不是应该也没法指向变量了么?还是说“ABC“属于const
variable所以不会随着返回而消失?
a***d
发帖数: 336
21
来自主题: Statistics版 - R同名函数问题
this is what I usually do. there are probably much better ways of doing it.
your function is usually in .globalEnv unless you specify otherwise.
so to get your function, use:
fun1 <- get(funcNm, env=.GlobalEnv)
use search() to find out position of the package. if the package is the nth
package in the search path, use:
fun2 <- get(funcNm, pos=n)
1 (共1页)