Vector push_back() giving SIGSEGV error

Discussion in 'C++' started by sravanreddy001, Sep 22, 2011.

  1. Hi,
    I've the below piece of code. I'm collecting all the links in text_files.

    This code worked fine for the first 26 files and giving this error for 27th
    When I skip 27th file using continue, its giving error at 89, and also at..
    121 122 131.

    For all the other files also. this piece of code is being executed.

    When i traced it using (eclipse) i think this is because, the memory is not being alloted to this vector.

    Additional Information:
    I'm using around 6-7 other vector<string> objects to store other information.
    And all these will have a max of 1 MB data all of them combined. and these vectors are recreated in the loop.

    Could this be because, i'm not erasing the contents of vector. I don't think this is the reason.

    Any inputs will be very helpful..


    while(begin != string::npos && end != string::npos){
    string link = line.substr(begin+2,end-begin-2);
    try{
    Links.push_back(link);
    }
    catch(...){
    printf("%d - ",Links.size());
    }
    File_Content.push_back("L: "+link);
    //printf("%s\n", line.substr(begin+2,end-begin-2).c_str());
    begin = line.find("[[",end);
    end = line.find("]]",begin+2);
    //begin = end+1; // can be end+2, but end+1 for safety
    }

    Thanks,
    Sravan.
    sravanreddy001, Sep 22, 2011
    #1
    1. Advertising

  2. On 9/22/2011 3:48 PM, sravanreddy001 wrote:
    > Hi,
    > I've the below piece of code. I'm collecting all the links in text_files.


    What's "text_files"? Is that the name of the variable?

    > This code worked fine for the first 26 files and giving this error for 27th
    > When I skip 27th file using continue, its giving error at 89, and also at..
    > 121 122 131.
    >
    > For all the other files also. this piece of code is being executed.
    >
    > When i traced it using (eclipse) i think this is because, the memory is not being alloted to this vector.


    Huh?

    >
    > Additional Information:
    > I'm using around 6-7 other vector<string> objects to store other information.
    > And all these will have a max of 1 MB data all of them combined. and these vectors are recreated in the loop.
    >
    > Could this be because, i'm not erasing the contents of vector. I don't think this is the reason.
    >
    > Any inputs will be very helpful..
    >
    >
    > while(begin != string::npos&& end != string::npos){
    > string link = line.substr(begin+2,end-begin-2);
    > try{
    > Links.push_back(link);
    > }
    > catch(...){
    > printf("%d - ",Links.size());
    > }
    > File_Content.push_back("L: "+link);
    > //printf("%s\n", line.substr(begin+2,end-begin-2).c_str());
    > begin = line.find("[[",end);
    > end = line.find("]]",begin+2);
    > //begin = end+1; // can be end+2, but end+1 for safety
    > }


    Not enough information. For instance, how is 'File_Content' declared?
    How is 'Links' declared?

    Create a test setup in which program would take your 27th file only and
    parse it (parsing is what you're doing, yes?) See if it fails then.

    IOW, try to debug your program. Oh, and read the FAQ 5.8.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 22, 2011
    #2
    1. Advertising

  3. Hi Paavo,

    <My apologies, I overlooked my question and missed the core part>

    Please let me know if you need more info.
    {I'm not a newbie to C++ (may be Intermediate), I checked if its refereing any out-of-bound reference etc.}

    The "Links" is a vector<string> object.

    The problem is:
    The size of link has reached its capacity for a size of 4. (during one of the(26th) iterations of this code.)

    When I'm trying to add the fifth element to the above vector, it is supposed to increase the size and add this element. But instead it is giving a SIGSEGV error.

    True. There are no obvious errors.

    Simply put.
    I've a vector, and I was able to add 4 objects to it, and I checked that its capacity is 4. and max_size() is over a 100,000. When I'm trying to add 5th element its returning the Termination Interrupt
    sravanreddy001, Sep 23, 2011
    #3
  4. My data is fetched from text files *.txt

    It was running properly for upto 26 iterations(files)

    The vector is not cumulative. (Its created for each iteration and result used elsewhere)

    There is lot of memory on my machine.

    (The total amount of RAM that my application at any point wont be more that 5 MB)
    sravanreddy001, Sep 23, 2011
    #4
  5. sravanreddy001

    Ian Collins Guest

    On 09/23/11 12:17 PM, sravanreddy001 wrote:

    Please keep the context of the message you are replying to, otherwise
    your posts don't make sense.

    > Hi Paavo,
    >
    > <My apologies, I overlooked my question and missed the core part>
    >
    > Please let me know if you need more info.
    > {I'm not a newbie to C++ (may be Intermediate), I checked if its refereing any out-of-bound reference etc.}
    >
    > The "Links" is a vector<string> object.
    >
    > The problem is:
    > The size of link has reached its capacity for a size of 4. (during one of the(26th) iterations of this code.)
    >
    > When I'm trying to add the fifth element to the above vector, it is supposed to increase the size and add this element. But instead it is giving a SIGSEGV error.
    >
    > True. There are no obvious errors.
    >
    > Simply put.
    > I've a vector, and I was able to add 4 objects to it, and I checked that its capacity is 4. and max_size() is over a 100,000. When I'm trying to add 5th element its returning the Termination Interrupt


    You really should do what Paavo and Victor suggested and build a
    complete, self-contained example demonstrating your problem that you can
    post. Odds are that if you can't, your problem lies elsewhere.

    --
    Ian Collins
    Ian Collins, Sep 23, 2011
    #5
  6. sravanreddy001

    Jorgen Grahn Guest

    On Fri, 2011-09-23, sravanreddy001 wrote:

    > When I'm trying to add the fifth element to the above vector, it is
    > supposed to increase the size and add this element. But instead it is
    > giving a SIGSEGV error.


    Then debug the core dump to see where the crash happens. If it is in
    new/malloc(), you have corrupted the heap memory somewhere else in
    your code. Also, run valgrind if it exists for your platform.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Sep 23, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ganesh Gella
    Replies:
    5
    Views:
    1,140
    John Harrison
    Jul 14, 2003
  2. Hitesh Bhatiya
    Replies:
    4
    Views:
    4,510
    John Harrison
    Aug 13, 2003
  3. Avi Bercovich
    Replies:
    5
    Views:
    23,600
    Evan Carew
    Jan 14, 2004
  4. Replies:
    8
    Views:
    1,896
    Csaba
    Feb 18, 2006
  5. Bin Chen
    Replies:
    4
    Views:
    413
    Nate Barney
    Jan 19, 2007
Loading...

Share This Page