-
std::vector problem
The following code throws an unhandled exception. Any ideas?
Code:
typedef unsigned long DWORD;
std::vector< DWORD > _blocks;
//...
// str = "abc"
DWORD __numBlocks = ( ( str.length( ) + 8 ) >> 6 ) + 1;
_blocks.erase( _blocks.begin( ), _blocks.end( ) );
for( DWORD __i = 0; __i < __numBlocks * 16; __i++ )
_blocks.push_back( 0 );
-
what's the exception error say? is _blocks defined somewhere else in the code or is that supposed to be _numBlocks?
-
>>what's the exception error say?
It's an access violation. That's all I know, and it's happening on the push_back( ) line.
>> is _blocks defined somewhere else in the code or is that supposed to be _numBlocks?
It's defined elsewhere, but I put the definition at the top of my code.
Also, I'm compiling using MingW/GCC v3.2, and Bloodshed Dev-C++ v4.9.8.5 (latest).
-
Doesnt seem to crash for me (VC++)
Can you try it with vector::clear instead of vector::erase
Also, avoid the underscore before your variables.....it's not legal.
-
You might be having a collision with a library variable, normally users shouldn't have leading underscores. The only exception that push_back() should throw is bad_alloc.
Your erase() is actually passing erase(_block.end(),_block.end()) as the vector is empty in your example. I beleve this may be undefined though as a quality of implementation issue it should always no-op. Your sample code works fine for me here (gcc 3.2) with a string of "abc". See if you can produce a small sample of code that reproduces the exception.
This might be something the stlPort folks should see.
As an asside the snazzy way of doing this is simply
Code:
blocks.clear();
blocks.resize((((str.length()+8)/64)+1)*16,0);
// Or, cooler and beter
std::vector< DWORD >((((str.length()+8)/64)+1)*16,0).swap(_blocks);
edit: Hi Fordy gotta learn to actually read the previews.
-
Hmm... copied the entire code over to VC++6 and it worked fine. I just switched over to using new and delete[], and it works fine.
>>You might be having a collision with a library variable
I doubt that, because I'm referencing everything with the std:: prefix, I'm not 'using namespace std;'.
>>Can you try it with vector::clear instead of vector::erase
Same error.
-
Leading underscores are not requred to be in the std namespace, particularly if you are mixing in any of the old C headers. They can even be lowercase macros, this rule predates std:: by a good twenty years.
-
I even removed every underscore, and it still doesn't work!
-
very strange, I can't duplicate the behavior here. Does it happen with a seperate program containing just what you posted? If you aren't using underscores and are using clear() then push_back can run out of heap, but cannot create an access violation on it's own without either a bug in the library or messing with vectors pointers directly (via buffer overflow somewhere or casting evilness)
-
I opened up a new copy of Dev-C++ and did a full recompile, and now it works fine! Wierd.