c - segmentation fault / malloc error? -


edit: problem solved, all

so i'm writing code final piece of c coursework. i've written function fastdfs carries out fourier transform. following code supposed time process different input sizes.

#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; <= 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); } 

the code has been simplified original, contains same error. @ stage either error "timings.out(49621,0x7fff7e684300) malloc: *** error object 0x7f81ca80c208: incorrect checksum freed object - object modified after being freed." returned or segmentation fault:11. program crashes varies on different runs, never gets part n = 8192.

i've written main function test code , don't encounter these errors. main uses same functions (make_complex_vector etc). difference can see don't free pointers in main test 1 value of n @ time, don't see how cause crash? can run code greater values of n in main.

can see wrong here? many thanks

you iterating 1 -> n , assigning y. in c, arrays 0-indexed. example, if create array of size 5, elements numbered so:

[a[0], a[1], a[2], a[3], a[4]] (5 elements, numbered 0-4)

so instead of

for(i = 1; <= n; i++) 

what want is

for(i = 0; < n; i++) 

otherwise, going write past end of allocated memory. if want first element of vector 1, write this:

for(i = 0; < n; i++) {     y[i] = + 1; } 

(i assuming make_complex_vector(n) allocates array of size n).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -