Thread: std::vector problem

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    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 );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    what's the exception error say? is _blocks defined somewhere else in the code or is that supposed to be _numBlocks?

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>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).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.
    Last edited by grib; 12-07-2003 at 01:38 PM.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I even removed every underscore, and it still doesn't work!
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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)

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I opened up a new copy of Dev-C++ and did a full recompile, and now it works fine! Wierd.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed