开发者

Segfault on nth interation through std::map

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 开发者_StackOverflow中文版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!?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜