Click here to Skip to main content
12,753,788 members (35,910 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ STL

I have declared a vector as a member variable in a class in my program. I am not even using it but still it gives access violation error at runtime.

Why is it so ??

vector <int> m_iNumbers;
Posted 6-Jul-11 0:05am
Updated 6-Jul-11 0:08am
ThatsAlok 6-Jul-11 5:22am
i doubt it...could you let us know what the actual error!
PrafullaVedante 6-Jul-11 6:06am
Error comes at runtime .... Program shows error about memory corruption at the exit.
SAKryukov 6-Jul-11 11:14am
There is nothing wrong in the code line you show. The problem is elsewhere. You do not provide sufficient information.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

The other cause could be that a memory overwrite exists in the previously existing code. It now overwrites the new member variable and thus corrupts it.

You can check for this by adding a buffer before the new variable, init to 0 in the constructor and check the content at various points to see if/when its being corrupted.
CPallini 6-Jul-11 9:02am
Good point, indeed.
Niklas Lindquist 6-Jul-11 15:22pm
You can even set a break point in VS to trigger when that variable changes, to quickly find the offending module. 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I think just inclusion wont give any error.If you are using Microsoft visual studio, Just start debugger and when the error came click on retry button which takes you to the error code and check from where the error is coming.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I just added that line to my program and it has no effect, you must be doing something else to cause the crash.
Niklas Lindquist 6-Jul-11 7:16am
Did you actually add it to your own code to verify that? Something tells me you're not being completely honest here :)
Richard MacCutchan 6-Jul-11 7:29am
Yes, I did add it to my own code. I do not see why you need to imply that I'm being dishonest!
Niklas Lindquist 6-Jul-11 9:39am
Simply because your're an excellent C++ programmer. I imply that you knew before testing it that the inclusion of the member variable wasn't the real problem, and that you also knew your own code would work with or without it. If you could not add a vector<int> without having your code break, not much code would work.
It was meant as a compliment rather than an accusation, even if it was somewhat disguised.
Richard MacCutchan 6-Jul-11 10:49am
My mistake; I guess I misinterpreted your message, and also did not quite notice the smiley.
Oh, BTW, I am far from being an excellent C++ programmer; but I am willing to learn.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

The line itself does almost nothing but making your class a little wider.
If you are working with multiple files, check the inclusion dependencies (it seems you class is destroyed in a file whose obj file doesn't know about the change you made: probably a full rebuild will eliminated the problem).

Or... you used the vector using the [] operator (that doesn't check form bounds ...) outside a proper range (causing corruption in a memory guard, that is revealed during the system cleanup)

Or ... many other (bad) things

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 6 Jul 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100