2

Everything is working until the compiler tries to perform the push_back operation. in the if condition proper values are being returned.
I have declared items as:

vector<int> items; // inside the header file.

//inside the .cpp file

void MsPs::findnSort()
{
 for(int i = 1; i<50 ; i++)
 {
 string temp = static_cast<ostringstream*>( &(ostringstream() << i) )->str(); // TO convert int i to a string temp
 if(findSupport(temp) >= MIS[i])
 {
 items.push_back(i);
 }
 }
}

the following error pops up:

Unhandled exception at 0x5052ad4a (msvcp100d.dll) in PrefixScan.exe: 0xC0000005: Access violation reading location 0x3d4cccd1.

PS: I have one more function using the push_back operation and there it's working fine.

Can anyone help me with this?

Even this gives the same error:

void MsPs::findnSort()
{
 for(int i = 1; i<50 ; i++)
 {
 items.push_back(i);
 }
}
asked Feb 16, 2013 at 0:25
17
  • what's the size of MIS? Commented Feb 16, 2013 at 0:35
  • Its size is 50, but while debugging, the code isn't going through even the first pass. Commented Feb 16, 2013 at 0:37
  • Memory is corrupted somewhere else, use debugger to see the call stack, should find something wrong Commented Feb 16, 2013 at 0:47
  • 1
    Why do you declare vector<int> items in the header file? Is it a shared header? Where is the vector actually instanced? Commented Feb 16, 2013 at 0:50
  • 1
    @ChrisHayden. No, it will not be a static instance - it will be an exported symbol in every translation unit that includes the header. Commented Feb 16, 2013 at 0:56

1 Answer 1

2

I think the issue is that the ostringstream is destructed when the static cast returns. Thus your pointer is dangling when str() is called. Try this instead:

void MsPs::findnSort()
{
 for(int i = 1; i<50 ; i++)
 {
 ostringstream blah;
 string temp = (blah << i).str();
 if(findSupport(temp) >= MIS[i])
 {
 items.push_back(i);
 }
 }
}
answered Feb 16, 2013 at 0:32
Sign up to request clarification or add additional context in comments.

2 Comments

Eventhis is giving the same error: void MsPs::findnSort() { for(int i = 1; i<50 ; i++) { items.push_back(i); } }
@AdRoiT : Sounds to me like you're calling findnSort on a null or otherwise invalid pointer to an MsPs object.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.