On Mon, 02 Mar 2009 07:01:52 +0000, LL wrote:
#include <stdio.h>
float* dup(float* farr[], int n) {
float* fdarr;
for (int i=0; i<n; i++) {
fdarr[i]=*farr[i];
}
return fdarr;
}
main() {
float *f1=new float(1.0);
float *f2=new float(2.0);
float *f3=new float(3.0);
float* farr_s[]={f1,f2,f3};
float* fdarr_s;
fdarr_s=dup(farr_s,3);
for (int i; i<3; i++) {
printf("%f ", fdarr_s[i]); // Segmentation fault
}
}
I fixed it.
#include <stdio.h>
float* dup(float* farr[], int n) {
float* fdarr=new float[n];
for (int i=0; i<n; i++) {
fdarr[i]=*(farr[i]);
}
return fdarr;
}
main() {
float *f1=new float(1.0);
float *f2=new float(2.0);
float *f3=new float(3.0);
float* farr_s[]={f1,f2,f3};
float* fdarr_s=dup(farr_s,3);
for (int i=0; i<3; i++) {
printf("%f ", fdarr_s[i]); // 1.0 2.0 3.0
}
}
This still has no "int main()" and produces memory leaks. I hope this is
and ours time.