Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:25:15+00:00 2026-06-14T02:25:15+00:00

This is a snippet of my implementation of the Cooley-Tukey algorithm in C. And

  • 0

This is a snippet of my implementation of the Cooley-Tukey algorithm in C. And yes, it’s college homework. But anyway… The algorithm works fine but I have to free ar1 and ar2 to get rid of immense memory leaks on huge input data but every time I try, I get invalid reads. In theory, ar1 and ar2 should only be used by the current instance of the function and they should be unique, since every instance mallocs its own output.

complex_exp * dft(complex_exp * from, int N, int s, int inverse) {

if(N == 1)
    return from;

int i;
complex_exp * transformed = (complex_exp *) malloc(N * sizeof(complex_exp));
complex_exp * ar1 = dft(from, N / 2, 2*s, inverse); //LINE 83
complex_exp * ar2 = dft(from + s, N / 2, 2*s, inverse); // LINE 84

for(i = 0; i < N/2; i++) {

    transformed[i] = ar1[i]; //LINE 88
}

for(i = N/2; i < N; i++) {
    transformed[i] = ar2[i - N/2];
}

//Do stuff with the transformed array - NO reference to ar1 or ar2.

free(ar1); //LINE 113
return transformed;
}

Valgrind says:

==69597== Invalid read of size 8
==69597==    at 0x100000EE6: dft (progtest05.c:88)
==69597==    by 0x100000EA2: dft (progtest05.c:84)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100001A0E: main (progtest05.c:233)
==69597==  Address 0x100007250 is 64 bytes inside a block of size 256 free'd
==69597==    at 0xDCB8: free (vg_replace_malloc.c:450)
==69597==    by 0x1000011E5: dft (progtest05.c:113)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100000E67: dft (progtest05.c:83)
==69597==    by 0x100001A0E: main (progtest05.c:233)

So it seems that a call to dft on line 83 frees the memory which then the call to dft on the next line tries to access. Any idea what’s actually going on and how I could get rid of the leaks?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:25:16+00:00Added an answer on June 14, 2026 at 2:25 am

    You say “every instance mallocs its own output”, however that’s not true in this statement:

    if(N == 1)
        return from;
    

    Perhaps when N==1 you should return a copy of from (i.e. malloc new memory, copy the contents of from into the new memory, and return the copy).

    how I could get rid of the leaks?

    I expect you must free ar1 and ar2 before returning transformed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This C snippet is part of a merge algorithm implementation: out[i++] = (in1[i1] <
In the tableView:cellForRowAtIndexPath: implementation of my UITableViewController subclass I'm using this code snippet: AlarmPicker
This snippet from official website works as expected: $treeObject = Doctrine::getTable('Category')->getTree(); $rootColumnName = $treeObject->getAttribute('rootColumnName');
I have this snippet of shell script: am start -n com.android.gallery3d/com.android.gallery3d.app.MovieActivity -d /sdcard/movie.mp4 sleep
I have this snippet of the code in my header : class A {
Here we have a snippet of an array implementation of a binary heap. I
I have code snippet, which is like this... #import <sqlite3.h> @interface DatabaseClass : NSObject
hello everyone I have this snippet of the code: void* Init(int N) { Hand
The following code snippet compiles just fine on Mac OS X with gcc, but
I have this code (snippet) from a .h file: #import <UIKit/UIKit.h> #import ILView.h /**

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.