【在 h***o 的大作中提到】 : are you sure the crash is due to c++'s array?
x*u
10 楼
没错,同样的程序,我编了一个fortran的, program Array REAL A(2000,2000) INTEGER i, j do 100 i = 1,2000 DO 100 j = 1,2000 A(i,j) = 1. 100 continue end 用同样的机器,同样的编译器(gnu gcc's f77),没有一点问题。
Don't mislead others with totally wrong points. Historically, C++ program is much slower than Fortran program. One major reason is because of the usage of pointer. C/C++ compilers must assume that different pointers might refer to the same data, thus preventing some compiler optimizations. Fortran is dominant in the scientific calculations not only because of those old software packages, but also of its efficiency. At least for me. :)
能说一下为什么会这样吗?是不是因为当时CPU的寄存器数量太少? 至少我看现在的编译器根本没这个问题啊。 至少象他刚才那个程序 for (i=0;i<2000;i++) for (j=0;j<2000;j++) { ... data[i][j]=xx } 编译成汇编,循环体当中那句就是: st [data+i+j], xx 其中data通常是个寄存器,i+j用另一个寄存器。地址计算需要2次加法。 而如果用指针, pt=data[0]; for (i=0;i<2000;i++) for (j=0;j<2000;j++) { ... *(pt++)=xx; } 那么循环体中的那句就是: st [pt], xx inc pt 其中pt也是一个寄存器。这样地址运算不需要加法,只需额外的一个增量运算。
【在 f****r 的大作中提到】 : : Don't mislead others with totally wrong points. : Historically, C++ program is much slower than Fortran program. One major reason is : because of the usage of pointer. C/C++ compilers must assume that different : pointers might refer to the same data, thus preventing some compiler : optimizations. : Fortran is dominant in the scientific calculations not only because of those : old software packages, but also of its efficiency. At least for me. :)
I think this would be a good idea. However, could you give me a simple example, since I haven't used this function before. Thanks alot!
,告
是 有答 有。
【在 Ag 的大作中提到】 : allocate memory first, and it will be ok. : I don't know c++, only C. but same thing happened to me years ago. : use 'malloc()'
Ag
19 楼
double **a; a = (double **) malloc(200*sizeof(double *)); a[0] = (double *) malloc(200*sizeof(double)); for(i=0;i<200;i++){ a[i] = a[i-1]+200; } it's not elegant, but it should work.
【在 x*****u 的大作中提到】 : I think this would be a good idea. However, could you give me a simple : example, since I haven't used this function before. Thanks alot! : : ,告 : : : : : :
stack size not enough. try ulimit to increase it. If you dynamically alloate the memory space, there won't be such a problem. BTW, this problem also happens in fortran, if you don't use the heap. The limit could be compiler dependent.
【在 s**i 的大作中提到】 : stack size not enough. : try ulimit to increase it. : If you dynamically alloate the memory space, there won't be such a problem. : BTW, this problem also happens in fortran, if you don't use the heap. : The limit could be compiler dependent.