• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception: STATUS_ACCESS_VIOLATION - pointer referencing

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am just starting up some C++ (that is after 10 years of JAVA!). I am following examples from Stroupstrup book.


I am put together the following code segments from his book.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;

map<string, int>histogram;
void record (const string &s)
{
histogram[s]++; //record frequency of "s"
cout<<"recorded:"<<s<<" occurence = "<<histogram[s]<<"\n";
}

void print (const pair<const string, int>& r)
{
cout<<r.first<<' '<<r.second<<'\n';
}

bool gt_42(const pair<const string, int>& r)
{
return r.second>42;
}

void f(map<string, int>& m)
{
typedef map<string, int>::const_iterator MI;
MI i = find_if(m.begin(), m.end(), gt_42);
cout<<i->first<<' '<<i->second; //this gives an error Exception: STATUS_ACCESS_VIOLATION
}

int main () {
istream_iterator<string> ii(cin);
istream_iterator<string> eos;
cout<<"input end\n";

for_each(ii, eos, record);

//typedef pair <string, int> String_Int_Pair;
//histogram.insert(String_Int_Pair("42", 1));
//histogram.insert(String_Int_Pair("44", 1));


//for_each(histogram.begin(), histogram.end(), print);
f(histogram);

}

I get an error - Exception: STATUS_ACCESS_VIOLATION, I think in the referencing i->first, i->second, I guess. Can someone help me out find out what the issue might be. Also if you can suggest some alternate C++ forums that will be helpful as well.

Regards
Mahesh


 
author
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahesh Chandran wrote:Hi all,
I am put together the following code segments from his book.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;

map<string, int>histogram;
void record (const string &s)
{
histogram[s]++; //record frequency of "s"
cout<<"recorded:"<<s<<" occurence = "<<histogram[s]<<"\n";
}

void print (const pair<const string, int>& r)
{
cout<<r.first<<' '<<r.second<<'\n';
}

bool gt_42(const pair<const string, int>& r)
{
return r.second>42;
}

void f(map<string, int>& m)
{
typedef map<string, int>::const_iterator MI;
MI i = find_if(m.begin(), m.end(), gt_42);
cout<<i->first<<' '<<i->second; //this gives an error Exception: STATUS_ACCESS_VIOLATION
}

int main () {
istream_iterator<string> ii(cin);
istream_iterator<string> eos;
cout<<"input end\n";

for_each(ii, eos, record);

//typedef pair <string, int> String_Int_Pair;
//histogram.insert(String_Int_Pair("42", 1));
//histogram.insert(String_Int_Pair("44", 1));


//for_each(histogram.begin(), histogram.end(), print);
f(histogram);

}

I get an error - Exception: STATUS_ACCESS_VIOLATION, I think in the referencing i->first, i->second, I guess. Can someone help me out find out what the issue might be. Also if you can suggest some alternate C++ forums that will be helpful as well.



It does indeed look likely that the problem is in the reference to i->first and i->second in f(). The problem is that if there is no matching element, find_if/[tt] returns the end iterator for the range passed to it, in this case [tt]m.end(). The end iterator for a container is not dereferencable --- it is "one past the end" rather than the last item, so that m.begin()==m.end() is an empty range. You therefore need to only dereference it if an element was found:

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahesh Chandran wrote: typedef map<string, int>::const_iterator MI;
MI i = find_if(m.begin(), m.end(), gt_42);
cout<<i->first<<' '<<i->second; //this gives an error Exception: STATUS_ACCESS_VIOLATION


If you don't have any entries with a second value that's larger than 42, find_if will return NULL. You would need to check if that's the case before printing anything.

Edit: well, so far for my C++ skills; apparently it returns m.end(). You should still check if that's the case though, as Anthony suggested.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mahesh -- please format your code so it is easy to read.

Reg. "alternate C++ forums" comp.lang.c++ is a good place to ask C++ questions (although some would say that usenet is dying).

 
Always look on the bright side of life. At least this ad is really tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic