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 6170069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:58:02+00:00 2026-05-23T22:58:02+00:00

Solution: See Bo Persson’s post and my comment below. I am getting a segmentation

  • 0

Solution: See Bo Persson’s post and my comment below.

I am getting a segmentation fault with my map. What confuses me is that the n-1 iterations over the keys works but then seg faults on the nth iteration. To add to the confusion, the key value that the iteration seg faults on is found (with an iterator) in the pervious iteration and even earlier in the code.

I tried to profile the seg fault with valgrind, however i get an infinite loop of the message, “signal 11 being dropped from thread 0”. Hence, valgrind is not very useful.

The map that is seg faulting is called site_depths. Here is how the values are inserted:

map<string,unsigned short*> site_depths;
map<string,unsigned int>::iterator it;
map<string,unsigned short*>::iterator insert_it;
unsigned int size = 0;
string key = "";

// go through each key pair of CHROM_SIZES to build the site_depth map
for (it=CHROM_SIZES.begin(); it != CHROM_SIZES.end(); it++) {
    key = it->first;
    size = it->second;

    unsigned short *array = new unsigned short[size];

    insert_it = site_depths.end();
    site_depths.insert(insert_it, pair<string,unsigned short*>(key,array));
}

I’ve checked to be sure that all values added work. Both the key and size print to console.

Immediately after, i test to see if find() and [] access work on the key value that seg faults (this also works):

cout << "schill found: " << site_depths.find("lcl_NM_000999")->first << endl;
unsigned short* test_array = site_depths["lcl_NM_000999"];

Then when i parse the text file, it will seg fault on find() or if i commend it out, on the [] access:

            string line;
            string chromosome;
            unsigned int start;
            unsigned int end;
            unsigned int i;
            char* values[3];
            unsigned short* sites;
            map<string,unsigned short*>::iterator iter_end = site_depths.end();

        while (getline(in,line)) {
            //use C strtok to tokenize the line
            char cstr[line.size()+1];
            strcpy(cstr,line.c_str());

            char *pch = strtok(cstr, "  ");

            // tokenize three columns
            for (i=0; i<3 || pch != NULL; i++) {
                values[i] = pch;
                pch = strtok(NULL, "    ");
            }

            chromosome = values[0];
            start = atoi(values[1])-1;  //must subtract 1 to correspond to 0 index
            end = atoi(values[2])-1;

            // get appropriate array pointer
            if (site_depths.find(chromosome) == iter_end) {
                cerr << "WARNING: Chromosome name in Input file does not match .len file." << endl;
                cerr << " Exiting script." << endl;
                exit(EXIT_FAILURE);
            }
            sites = site_depths[chromosome];

            // increment over range
            for (i=start; i<end; i++) {
                sites[i]++;
            }
        }

The case where it segfaults is on key “lcl_NM_000998”, trying to find key “lcl_NM_000999”. This doesn’t make sense as the previous getline() iteration finds the key value “lcl_NM_000998”. I’ve checked to be sure this is the case by manually iterating through the map.

I’ve checked to be sure that my code before wasn’t seg faulting in previous code, but the tokenizing looks fine. My code always segfaults at this location in my test case. Does anyone have ideas!?

  • 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-05-23T22:58:03+00:00Added an answer on May 23, 2026 at 10:58 pm

    This isn’t just a test for presence

    unsigned short* test_array = site_depths["lcl_NM_000999"];
    

    but also inserts a node into site_depths, but with a null pointer in the second member.

    The code also trusts that start and end are always within range of the array size given by size. Wouldn’t hurt to validate that!

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

Sidebar

Related Questions

I've seen many similar errors, but I can't see a solution that applies to
Whenever I see a problem that would be shared by others, with a solution
Just posting the solution I worked out today. See my answer below. If you
Intro: EDIT: See solution at the bottom of this question (c++) I have a
I tried searching here for a similar solution but didn't see one so I
My current solution will suck sometimes EDIT For those who don't understand,see this example:
I came up with a solution, but I wanted to see if anyone had
In Visual Studio 2008, I can see all my project files in my Solution
I have been tasked with developing a solution that tracks changes to a database.
Edit: I decided to take the LINQ to XML approach (see the answer below)

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.