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

  • SEARCH
  • Home
  • 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 8549499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:43:43+00:00 2026-06-11T13:43:43+00:00

I am having difficulty finding this memory leak. I believe it has something to

  • 0

I am having difficulty finding this memory leak.

I believe it has something to do with basin

int numbasinsx(int size, double *f)
{
    int maxBasin, maxRelabel, bcnt;
    unsigned int newgene;
    int *basin, *relabel;
    int relabelcnt;

    //
    // find the basins
    //
    maxBasin = 1<<size;
    basin = new int [maxBasin];
    for (int gene=0; gene<maxBasin; gene++) basin[gene] = 0;

    bool change = true;
    bcnt = 0;
    while (change) {
        change = false;

        for (int gene=0; gene<maxBasin; gene++) {

            bool dominated = false;
            for (int i=0; i<size; i++) {               // for all neighbors
                newgene = gene ^ (1<<i);               // newgene is a neighbor 1 bit away

                if (f[newgene] > f[gene]) dominated = true;  // if neighbor is better mark it

                if (basin[newgene]!=0) {                                        // something to copy
                    if (f[newgene] > f[gene]) {                                // gene is dominated
                        if (basin[gene]==0 || basin[gene]<basin[newgene]) {    // if not labeled or not lowest number
                            basin[gene] = basin[newgene];                      // then label or relabel
                            change = true;
                        }
                    }
                    else if (f[newgene] == f[gene]) {
                        if (basin[gene]<basin[newgene]) {
                            basin[gene] = basin[newgene];
                            change = true;
                        }
                    }
                }
            }

            if (! dominated && basin[gene]==0) {
//                printf("max: %d %lg\n", gene, f[gene]);
                basin[gene] = ++bcnt;
                change = true;
            }
        }
    }


    //
    // relabel the basins
    //
    maxRelabel = maxBasin/2+1;
    relabel = new int [maxRelabel];
    for (int gene=0; gene<maxRelabel; gene++) relabel[gene] = 0;

    relabelcnt = 1;
    for (int gene=0; gene<maxBasin; gene++) {
        if (basin[gene]>=maxRelabel) printf("ERROR: trying to index %d into relabel array with max index of %d\n", basin[gene], maxRelabel-1);

        if (relabel[basin[gene]]==0) {
            relabel[basin[gene]] = relabelcnt++;
        }
//        printf("%d %lg %d\n", gene, f[gene], relabel[basin[gene]]);
    }
//    printf("%d\n", relabelcnt-1);

    delete basin;
    delete relabel;

    return relabelcnt-1;
}

I will leave the above up as to help others, but the new code with vectors is now at http://codepad.org/KiV85Brw. The vector run time is much slower than the delete[] version, and it is even segfaulting for large inputs. I am doing something wrong

  • 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-11T13:43:44+00:00Added an answer on June 11, 2026 at 1:43 pm

    If you use operator new[] to allocate a bunch of bytes, then you must use operator delete[] to destroy them:

    basin = new int [maxBasin];
    //.....
    delete[] basin;
    

    Note that you’re doing the same thing with relabel.

    Anyway, for this kind of tasks, you should use std::vector:

    std::vector<int> basin(maxBasin), relabel(maxRelabel); // We haz no leaks
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My apologies if this has already been answered - I'm having difficulty finding a
I'm looking to crowd-source this, as I'm having bit of difficulty finding an industry
I'm having difficulty finding support on this topic, because I know how to pass
Having difficulty finding relevant search results... Given this model: public abstract class A {
This seems simple enough, but I'm having difficulty finding an answer. I have a
I am having difficulty finding the one best way to do something. I have
I am having difficulty finding space and time complexity for this code that i
I watched Railscast 328 this morning and I am having difficulty finding docs for
This is probably an extremely basic question but I'm having difficulty finding an answer.
There is probably a really straightforward answer to this but I'm having difficulty finding

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.