【发布时间】:2015-04-29 17:20:54
【问题描述】:
编辑:问题现已解决,谢谢大家
所以我正在为我最后的 C 课程作业编写代码。我编写了一个执行傅立叶变换的函数 FastDFS。下面的代码应该为不同的输入大小计时这个过程。
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
#include <time.h>
void FastDFS(complex double *, complex double *, complex double *, complex double *, int, int);
void print_complex_vector(complex double *, int);
void free_complex_vector(complex double *);
complex double *make_complex_vector(int);
complex double *MakeWpowers(int);
int main(void)
{
int i,N, operations_required;
int index_counter = 1;
double initial_time, final_time, time_taken, mega_flops_rate;
complex double *mega_flops_vector = make_complex_vector(21);
for(N = 2; N <= pow(2,20); N = N * 2)
{
complex double *Wp = MakeWpowers(N);
complex double *y = make_complex_vector(N);
complex double *x = make_complex_vector(N);
complex double *w = make_complex_vector(N);
printf("N = %d\n",N);
operations_required = 1024;
for(i = 1; i <= N; i++)
{
y[i] = i;
}
initial_time = clock();
FastDFS(x,y,w,Wp,N,1);
final_time = clock();
time_taken = (final_time - initial_time) / CLOCKS_PER_SEC;
mega_flops_rate = operations_required / (time_taken * pow(10,6));
free(Wp);
free(y);
free(x);
free(w);
}
return(0);
}
代码与原始代码相比略有简化,但包含相同的错误。在某些阶段,错误“timings.out(49621,0x7fff7e684300) malloc: *** 对象 0x7f81ca80c208 错误:已释放对象的校验和不正确 - 对象可能在被释放后被修改。”返回或分段错误:11。程序崩溃的地方在不同的运行中有所不同,但永远不会得到部分 N = 8192。
我编写了一个 main 函数来测试代码并且没有遇到这些错误。主要使用相同的功能(make_complex_vector 等)。我可以看到的一个区别是我没有释放 main 中的任何指针,因为我一次只测试一个 N 值,但我不明白这会如何导致它崩溃?我可以在主程序中运行代码以获得比这更大的 N 值。
任何人都可以在这里看到明显错误的地方吗?非常感谢
【问题讨论】:
-
错误的行号是多少?
标签: c segmentation-fault malloc