0

I'm trying to debug my mergesort program, which seems to be problematic somewhere inside the "merge" function when i try to use "push_back" to add values from either the vector "left" or "right" to "mergedList". The following is an excerpt from the gdb debugging session (followed by my complete program code below that)

This occurs at the first call to merge; I was able to access the values of "vector left" using "print left[0]" and i got a value I expected (all numbers in the vector "left" are between 1-50000), but after the executing the line of code when i = 0:

"mergedList.push_back(left[i]);" and then using the debugger to print mergedList[0], it appears that adding left[0] was unsuccessful.

Am I misunderstanding push_back? or vectors? Sorry if my question is unclear--please let me know how I could improve it!


GDB debugging session:


(gdb) print left[0]
$1 = (int &) @0x100104760: 14108
(gdb) print mergedList[0]
$2 = (int &) @0x7fff5fbfdbf0: 1066800
(gdb) 
(gdb) info locals
t = 0
i = 0
mergedList = {
  <std::_Vector_base<int,std::allocator<int> >> = {
    _M_impl = {
      <std::allocator<int>> = {
        <__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>},
       members of std::_Vector_base<int,std::allocator<int> >::_Vector_impl:
      _M_start = 0x7fff5fbfdbf0,
      _M_finish = 0x7fff5fbfdc68,
      _M_end_of_storage = 0x7fff5fbfdb90
    }
  }, <No data fields>}
j = 0
sizeOfLeft = 1
sizeOfRight = 1
next = 1
(gdb) 
-uuu:**-F1  *gud-p1*       Bot L28    (Debugger:run)-------------------------------------- 


    ------------------------------------------------------------------------------------------------------------------
    while (iss >> n)
    {
            v.push_back(n);
    }

    }

    return v;
}

vector<int> merge(vector<int> left, vector<int> right){

    int i = 0;
    int j = 0;
    int sizeOfLeft = left.size();
    int sizeOfRight = right.size();
    vector<int> mergedList;

    while (i < sizeOfLeft || j < sizeOfRight){
    if (i < sizeOfLeft && j < sizeOfRight){
            if (left[i] < right[j]) {
        mergedList.push_back(left[i]);
=>          i++;
            }else{
                mergedList.push_back(right[j]);
        j++;
-uu-:---F1  main.cpp       21% L47    (C++/l Abbrev)--------------------------------------------------------------------------------------------------------------------------------------------------------


    #COMPLETE MERGESORT PROGRAM



    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>

    using namespace std;

vector<int> getNums(){


    ifstream infile("/Users/christinedeist/Documents/Algorithms/Practice/TestProject/IntegerArray.txt");
    string line;
    vector<int> v;

    while (getline(infile, line))
    {
        istringstream iss(line);
        int n;

        while (iss >> n)
        {
            v.push_back(n);
        }

    }

    return v;
}

vector<int> merge(vector<int> left, vector<int> right){

    int i = 0;
    int j = 0;
    int sizeOfLeft = left.size();
    int sizeOfRight = right.size();
    vector<int> mergedList;

    while (i < sizeOfLeft || j < sizeOfRight){
        if (i < sizeOfLeft && j < sizeOfRight){
            if (left[i] < right[j]) {
                mergedList.push_back(left[i]);
                i++;
            }else{
                mergedList.push_back(right[j]);
                j++;
            }

        }else if (i < sizeOfLeft){
            mergedList.push_back(left[i]);
            i++;
        }else if (j < sizeOfRight){
            mergedList.push_back(right[j]);
            j++;
        }
    }


    return mergedList;

}

vector<int> sortVector(vector<int> nums){

  int sizeOfNums = nums.size();

    if (sizeOfNums == 1){
        return nums;
    }
    vector<int> left;
    vector<int> right;
    int midpoint = sizeOfNums/2;
    for (int i = 0; i < midpoint; i++){
        left.push_back(nums[i]);

    }
    for (int j = midpoint; j < sizeOfNums; j++){
        right.push_back(nums[j]);
    }
    left = sortVector(left);
    right = sortVector(right);
    return merge(left, right);

}

int main (int argc, char *argv[]) {

    vector<int> nums = getNums();
    vector<int> sorted = sortVector(nums);

    for(int i = 0; i < nums.size(); i++){
      cout << nums[i] <<endl;

    }

    return 0;
}
cdeist
  • 229
  • 2
  • 6
  • 2
    Try checking if you successfully opened the file. – Jesse Good Feb 10 '13 at 20:46
  • 1
    Can you verify that nothing was added to `mergedList` before appending `left[0]`? Ie, is there any chance that `right[j] > left[i]` in the first iteration? – chrisaycock Feb 10 '13 at 20:47
  • 1
    Could you minimize your code to highlight your problem? Too much irrelevance is not attractive to answerers. – xmllmx Feb 10 '13 at 20:49
  • any possibility that you are printing before `push_back` ? – ogzd Feb 10 '13 at 20:53
  • Thanks for this feedback--realized I did a very sloppy job of asking this question; reposted a narrowed down version http://stackoverflow.com/questions/14819183/why-cant-i-access-value-in-a-vector-using-print-in-gdb-debugger – cdeist Feb 13 '13 at 04:44

1 Answers1

1

This code lacks the error checking but that should still work. I think this logic should work fine. Did you try debugging this code as it was not producing the sorted out put. At the end when you sort the elements you are not printing the sorted elements but the ones which you read.

Because your gdb session has non-null values for pointers in vector i.e. _M_start = 0x7fff5fbfdbf0, _M_finish = 0x7fff5fbfdc68, _M_end_of_storage = 0x7fff5fbfdb90 This there are elements pushed into vector. Thus, file must be open and read successfully.

ddvVrdw
  • 39
  • 4
  • Thanks! I think the error was actually somewhere else in my code, push_back itself does seem to have worked, I think I just misunderstand how to read values in an array at certain points in the program. I tried to isolate the issue in a new post:http://stackoverflow.com/questions/14819183/why-cant-i-access-value-in-a-vector-using-print-in-gdb-debugger – cdeist Feb 11 '13 at 19:15