f



hash_map creates access violation errors

hi,

I use a hash_map for storing 4x4 DCT-Blocks. Therefore I convert each
block into a string looking like:
":-88:-3:-9:-91:-44:1:-7:-93:88:-1:9:42:-2:-2:-6:". Then I use the
following code to identify blocks that have been seen before.


-- code -------------
std::hash_map<std::string, int> blockMap; //initialize hash_map
....

// add blockString to hashmap
if (blockMap[blockString] == NULL) {
   blockMap[blockString] = ++blockIDcounter;
   blockID = blockIDcounter;
} else {
   blockID = blockMap[blockString];
}

-- end of code -------

The blockIDcounter counts the number of different blocks.

during calculation of the first image frame, there is always a runtime
error (access violation reading/writing to a certain address) when
reaching a blockIDcounter around 1800.

So, what could be the reason for this problem? I use Visual Studio C++
..NET 2003.


best regards,
  Rebecca Hoffmann

-- 
rebecca.hoffmann@tut.fi

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Rebecca
6/7/2005 1:14:46 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

3 Replies
828 Views

Similar Articles

[PageSpeed] 50

Rebecca.Hoffmann@gmail.com (Rebecca Hoffmann) writes:

> I use a hash_map for storing 4x4 DCT-Blocks. Therefore I convert
> each block into a string looking like:
> ":-88:-3:-9:-91:-44:1:-7:-93:88:-1:9:42:-2:-2:-6:". Then I use the
> following code to identify blocks that have been seen before.
>
>
> -- code -------------
> std::hash_map<std::string, int> blockMap; //initialize hash_map
> ...
>
> // add blockString to hashmap
> if (blockMap[blockString] == NULL) {

Why NULL? The value type of blockMap is int, so why don't you write 0?


>    blockMap[blockString] = ++blockIDcounter;
>    blockID = blockIDcounter;
> } else {
>    blockID = blockMap[blockString];
> }
>
> -- end of code -------
>
> The blockIDcounter counts the number of different blocks.
>
> during calculation of the first image frame, there is always a
> runtime error (access violation reading/writing to a certain
> address) when reaching a blockIDcounter around 1800.

Please post the minimal (<50 lines) complete program that produces
this access violation so that your audience can see what you are
seeing.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Thomas
6/7/2005 10:49:22 PM
Rebecca Hoffmann wrote:
> hi,
>
> -- code -------------
> std::hash_map<std::string, int> blockMap; //initialize hash_map
> ...
>
>
> during calculation of the first image frame, there is always a runtime
> error (access violation reading/writing to a certain address) when
> reaching a blockIDcounter around 1800.
>
> So, what could be the reason for this problem? I use Visual Studio C++
> .NET 2003.

First try with std::map, not hash_map. If map works and hash_map don't,
  then default hash function for string probably doesn't work,
so you must provide your own hashing function (read the docs I guess).

Greetings, Bane.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Branimir
6/9/2005 3:44:49 PM

Branimir Maksimovic wrote:
> Rebecca Hoffmann wrote:
> > hi,
> >
> > -- code -------------
> > std::hash_map<std::string, int> blockMap; //initialize hash_map
> > ...
> >
> >
> > during calculation of the first image frame, there is always a runtime
> > error (access violation reading/writing to a certain address) when
> > reaching a blockIDcounter around 1800.
> >
> > So, what could be the reason for this problem? I use Visual Studio C++
> > .NET 2003.
>
> First try with std::map, not hash_map. If map works and hash_map don't,
>   then default hash function for string probably doesn't work,
> so you must provide your own hashing function (read the docs I guess).

That seems unlikely.  If the hash function is poor (eg "hash() { return
0; }" ) then the performance of the hash_map will be appalling, but the
behaviour should be the same.

It looks to me like there is some memory corruption going on somewhere,
or indirection through an invalidated iterator, or something similar.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Martin
6/10/2005 4:30:22 PM
Reply: