The C++ projects I wrote and run without any problems on Ubuntu return the exception "vector subscript out of range" when I run them on windows. I use Windows 7 and Visual C++ 2008 Express.
I hope that makes sense to someone.
The version of the STL shipped by Microsoft comes with checked iterators, which when run in debug mode ensure that your vector indices are in range. No such checking is done in GCC by default.
Your code almost certainly contains undefined behavior. In such a case, an implementation is free to do almost anything it wants. It appears that gcc has basically ignored the problem, so it wasn't apparent. VC++ has built enough self-monitoring into your code to find the problem and tell you about it.
The next step is pretty much up to you: find the problem in your code and fix it. Unfortunately since you haven't posted any of the code, it's essentially impossible to give much more detailed advice about what you should do or how to do it. About the only hint I can think of is that the debugger in VC++ does have a nice stack trace feature, so if you run the code under the debugger and it fails, it's pretty easy to walk back up the stack to find the code that called the function (that called the function, etc.) where the problem was detected.
Related
I was writing some simple C++ code using DevC++ when this error came up:
I have no clue as to why I am getting this upon initialising a vector array (a graph adjacency list).
I couldn't co much to solve this problem since I am not an expert in c++ compilers. I tried reinstalling the program but that didn't help at all.
My compiler is TDM-GCC and in the compiler options I added "-std=c++11", which is executed when calling the compiler.
This line
std::vector<int> adj[NK];
defines an array of 100 million std::vector objects, along with a static initializer to create all of them.
Did you mean to create a single vector of size 100M?
std::vector<int> adj(NK);
Despite the fact that I made a mistake in the code, it still compiled successfully after a very simple fix. The compiler used to be a 64-bit Release version. I changed the field to the 32-bit Release and the problem disappeared, despite the ridiculous amount of memory my program had needed.
Please note that your mileage may vary and this solution might have some side effects I am not aware of. However, this worked just fine for me and it seems that all my other c++ files compile without any errors.
Ok, i am going to keep this simple and not include all the code (yet) if I don't need to. My inner senses tell me that there is probably a simple answer to this and I have overlooked it as usual. More inexperienced might say the compiler is the problem but I have never known that to be the case 99.999% of the time it has always been an error on my part in some way so... heres the question.
I have a program, this program is supposed to output two strings, nothing fancy
TCHAR* firststring = _T("FirstValue");
TCHAR* secondstring = _T("SecondValue");
_tprintf(_T("%s\n"),firststring);
_tprintf(_T("%s\n"),secondstring);
which outputs..
firststring
in the particular section of my code, this does not work (it works elsewhere) UNLESS i am step-over-debugging... then it is fine...
firststring
secondstring
I have even tried combining the operation into one by doing this:
_tprintf(_T("%s\n[%s]\n"),firststring,secondstring);
My result is, the first string, and the braces are there to try to point out that the string is trying to be print out and if it is empty, etc. I get this output:
firststring
]
and that's it.. again, UNLESS step-over debugging, then its all fine and dandy:
firststring
[secondstring]
has anyone ever heard of a problem like this? The last time I encountered weird problems like this was back in the ASM days with segment alignment but I doubt that is the case here, although I am not sure on _tprintf's inner workings... I'm going to try this again, if nobody knows, i'll assume its because of a error caused by the code earlier. The main question is, does this happen for any simple reason or does this just point to problems earlier in the program, that's all. Thanks for your reply.
PS: I realize that I didn't supply sufficient code to completely answer the problem, but the question is whether or not there is a known simple cause to this kind of behavior in the debugger (run vs. step-over). The breakpoint is set at the function level, I'm using Visual Studio 2008. Compiler Versions follow:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 80x86
Microsoft (R) Macro Assembler Version 9.00.21022.08
Microsoft (R) Incremental Linker Version 9.00.21022.08
Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
Microsoft Browse Information Maintenance Utility Version 9.00.21022
Microsoft (R) Manifest Tool version 5.2.3790.2075
I later resolved this issue by changing these strings from TCHAR* to const TCHAR*
when using read only strings it's supposedly undefined behavior to write a string array with a string literal. I'm not sure if it was because the document itself was being stored as unicode
or if that even had a part to play but I learned the hard way not to assign like that. And had I needed to write to those locations I should have used a separate constant string and copied it's contents with strcpy when i needed it in a read/write buffer. Apparently not doing that would cause security holes and other pitfalls to appear in the code even though it was simple.
(below is my reasoning on only partial code -- mouse over if interested)
As for the code sample not being complete, it was a very large project with thousands upon thousands of lines of code and I pretty much did not expect anyone on here to take THAT much time to go through it all. After all, its not like anyone here is paid to do so, and i really just needed to find out whether it was me or if this was a common thing and if it was a common thing, how to fix it. When we ask questions here, we aren't only asking a question, but we are also giving guidelines on how wide of a scope we expect the answer to be, and in no way do we ever expect anyone to be clarvoyant as some seem to suggest. If you don't want to answer a question or there isn't enough information for you to do so, then just don't its that simple (it's because we don't have all the information that we ask in the first place).
I have a Visual Studio 2008 solution which consists of some projects and uses dll's with theirs' headers. In the debug version it (the solution) works really well. And in release version it compiles successfully, but on executing some functions which are defined in a dll file it fails.
As I said, the solution works fine in debug mode, and options set properly. Tried turning off
optimization, turned on debugging information, it didn't help. What can be the cause of the problem?
I have seen this happen many times before. In almost every single case the problem was found to be an out-of-bounds error when writing to an array or other data structure. In other cases, an uninitialized variable was being used.
You have a bug in your code. That is certain. When you build under Debug settings, the compiler does a whole lot of stuff for you that masks certain types of problems. The compiler will write code that zero-initializes some stuff, masking uninitialized variable problems.
First thing I wold try is cranking up the warning levels to their highest settings. You should be doing this all the time anyways. This will very often reveal the problem. Just be sure to fix the problems the compiler tells you about. Don't mask them with #pragmas or chintzy casts. Next step through your code to isolate the problem. This can be difficult and time consuming, but there's a silver lining. Whatever the problem is, the likelyhood of you repeating that mistake is inversely proportional to how long and how hard it is to identify and fix the bug. :)
I'm facing a problem that is so mysterious, that I don't even know how to formulate this question... I cannot even post any piece of code.
I develop a big project on my own, started from scratch. It's nearly release time, but I can't get rid of some annoying error. My program writes an output file from time to time and during that I get either:
std::string out_of_range error
std::string length_error
just lots of nonsense on output
Worth noting that those errors appear very rarely and can never be reproduced, even with the same input. Memcheck shows no memory violation, even on runs where errors were previously noted. Cppcheck has no complains as well. I use STL and pthreads intensively, but without the latter one errors also happen.
I tried both newest g++ and icpc. I am running on some version of Ubuntu, but I don't believe that's the reason.
I would appreciate any help from you, guys, on how to tackle such problems.
Thanks in advance.
Enable coredumps (ulimit -c or setrlimit()), get a core and start gdb'ing. Or, if you can, make a setup where you always run under gdb, so that when the error eventually happen you have some information available.
The symptoms hint at a memory corruption.
If I had to guess, I'd say that something is corrupting the internal state of the std::string object that you're writing out. Does the string object live on the stack? Have you eliminated stack smashing as a possible cause (that wouldn't be detectable by valgrind)?
I would also suggest running your executable under a debugger, set up in such a way that it would trigger a breakpoint whenever the problem happens. This would allow you to examine the state of your process at that point, which might be helpful in figuring out what's going on.
gdb and valgrind are very useful tools for debugging errors like this. valgrind is especially powerful for identifying memory access problems and memory leaks.
I encountered strange optimization bugs in gcc (like a ++i being assembled to i++ in rare circumstances). You could try declaring some critical variables volatile but if valgrind doesn't find anything, chances are low. And of course it's like shooting in the dark...
If you can at least detect that something is wrong in a certain run from inside the program, like detecting nonsensical output, you could then call an empty "gotNonsense()" function that you can break into with gdb.
If you cannot determine where exactly in the code does your program crash, one way to find that place would be using a debug output. Debug output is good way of debugging bugs that cannot be reproduced, because you will get more information about the bug the next time it happens, without the need to actively reproduce it. I recommend using some logging lib for that, boost provides one, for example.
You are using STL intensively, so you can try to run your program with libstdc++ in debug mode. It will do extra checks on iterators, containers and algorithms. To use the libstdc++ debug mode, compile your application with the compiler flag -D_GLIBCXX_DEBUG
Using Microsoft Visual Studio 98, Microsoft Visual C++ 6.0 SP6
When running under the debugger, there's only one problem. If I pause the program and resume, everything's fine.
The problem? When I hit a breakpoint, my program stops. But not in a good way; execution halts, I'm thrown out of debug mode into edit mode. All's fine until a breakpoint is hit. And I know it's hitting the breakpoint - I see a flash of the little yellow arrow pointing at the right line of code, local variables in the inspect window and the call stack in that window. And then I'm staring at the editor.
This happens in all projects.
I've uninstalled and re-installed MSVC6. It didn't help.
I'm about to start over on a new PC; before I go that far, anyone know what I've done to this one?
Note: MSVC6 is not my choice, but there are reasons. It's the tool I work with. And, we get to target NT4, so given 2008 can't target NT4 and 2005 has issues with MFC and NT4, MSVC6 it is.
Stop beating on VC6. It's old. The STL was updated in 1996 from HP code written in 1994. C++ was ratified in 1998.
What is the code doing when you are breaking? Can you reduce the situation into a simple test. When I try that I usually find the cause. If you can do that so it still happens then I'll take a look at it for you. I too am unfortunate enough to use VC6 for my day to day work.
Visual C++ Express 2008 can't be used in certain situations.
The first thing I would check is if this project does the same thing on other machines. If not, it could be your box is heading south. If not it's the VC6 project itself.
Typically I get goofiness with the debugger when my program is doing something with the hardware, especially the video.
I would recommend turning off parts of your program until you figure out what part is causing this. If your program is small and not doing much it might be that the project is corrupted and needs to get rebuilt. Make a new project from scratch and put your files and settings back in by hand.
Is it specific to the app you're working on or do all breakpoints in any app break the debugger?
Is anything different if you attach the debugger manually after launching the app normally?
Is the device running out of memory and therefore gives up the ghost when it requires the additional memory to stop at the breakpoint?
Is the device running out of memory and therefore gives up the ghost when it requires the additional memory to stop at the breakpoint?
No, there's over a gig of RAM to go, and even more of virtual memory.
I haven't used MSVC6 in years, but I remember the debugger basically being a flaky piece of crap. Things like this would regularly happen for no apparent reason because it just didn't like your code that day.
In addition to the debugger being a flaky piece of crap, the rest of it was too.
It's STL implementation isn't threadsafe, it doesn't support very much of the C++ standard, and the IDE is sorely lacking in niceties.
I'm pretty sure you also just simply can't use any versions of the Platform SDK from the last 5 years because it's too old to grok them. Not a good thing.
You'd be mad to keep using it when there is Visual C++ Express 2008 which you can download for free.