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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:39:52+00:00 2026-05-27T05:39:52+00:00

My program crashes on this line but only in debug build, it works fine

  • 0

My program crashes on this line but only in debug build, it works fine in release build.

m_lstIds.insert(m_lstIds.begin() + indexInsert, ID);

‘m_lstIds’ is a std::vector of int, ‘ID’ is an int. When the program crashed, m_lstIds had 3 items (1, 2, 3). indexInsert was ‘0’ and ID was ‘0’.

The error message says:

Expression: vector iterator + offset out of range

I am running visual studio 2010; I am guessing it has something to do with bad project settings which conflicted with STL optimization.

Edit:
When I said: “works on release” I meant if I do std::cout<<m_lstIds[i] for i = 0..3, I will actually get 0,1,2,3 printed out. In debug build it just crashes when I try to insert.

Edit2:
I found the answer! Thanks everyone for the help.

Here is the shortest repro. The problem is the memset function I call at the constructor. Because the constructor of m_lstItem was called before the memset, it will erase whatever data in the vector that allowed insert to work properly.

What’s really interesting is how this worked in release but not in debug. Would be great if someone can explain that part.

struct SimpleList
{
  SimpleList()
  {
    memset(this, 0, sizeof(SimpleList));
    m_lstItem.push_back(0);
    m_lstItem.push_back(1);
    m_lstItem.push_back(2);
  }

  void Crash()
  {
    m_lstItem.insert(m_lstItem.begin() + 0, 3);
  }

  std::vector<int>m_lstItem;
};

int main(int argc, char** argv[])
{ 
  SimpleList sl;
  sl.Crash();
  return 0;
}
  • 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-27T05:39:52+00:00Added an answer on May 27, 2026 at 5:39 am

    memset(this, 0, sizeof(SimpleList)); is unsafe when your struct is not a POD.

    Due to the member std::vector<int>m_lstItem;, your struct is not a POD.

    Therefor, using memset is unsafe in this case, and leads to undefined behaviour. In other words: anything is allowed to happen! And anything includes working as expected…

    My advice: Do not use memset in C++ unless you totally know what you are doing.

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

Sidebar

Related Questions

I'm trying to get this simple program to work on windows, but it crashes:
Sometimes my c++ program crashes in debug mode, and what I got is a
The following C++ program crashes on my Windows XP machine with a message Abnormal
I'm experiencing a strange issue where my Visual Studio 2005 C++ program crashes the
I am working on Linux with the GCC compiler. When my C++ program crashes
Sometimes whenever I write a program in Linux and it crashes due to a
So I am trying to follow this tutorial to debug my code. I tell
Storyboard.SetTargetProperty sets an animations target property, but a Storyboard.GetTargetProperty on the following line will
My program has a trouble: When GC perform garbage collection,my program always crashes at
I have a small command line program that uses the Team System API. When

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.