wood burning stoves 2.0*
The moose likes C / C++ and the fly likes Exception: STATUS_ACCESS_VIOLATION - pointer referencing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Exception: STATUS_ACCESS_VIOLATION - pointer referencing" Watch "Exception: STATUS_ACCESS_VIOLATION - pointer referencing" New topic
Author

Exception: STATUS_ACCESS_VIOLATION - pointer referencing

Mahesh Chandran
Greenhorn

Joined: Mar 23, 2011
Posts: 20
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


Anthony Aj Williams
author
Ranch Hand

Joined: Jun 10, 2011
Posts: 56
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:



Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

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).


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception: STATUS_ACCESS_VIOLATION - pointer referencing