Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write this code.in run time one error is occur in this line"cout<<it2->first;":"Unhandled exception at 0x00411edd in test3.exe: 0xC0000005: Access violation reading location 0x00000004."
i have visual studio express 2008 and boost 1_47_0;
this is my code.plz help me!
C++
    #include "stdafx.h"
    #include <iostream>
    #include <boost/unordered_map.hpp>

using namespace std;

typedef boost::unordered_map<int,int > MAP;
        MAP map2;
        boost::unordered_map<int,int>::iterator it2;

void gen_random(char *s  ,char *p,int*  r,const int len);
void inline insert2(int i_key,int i_value);
void print();
//-----------main------------------------------------
void main()
{
    char* s_key=new char[8];
    char* s_value=new char[8];
    int i_value=0, size_random=8;
    for(int i=0;i<12;i++)
    {
        gen_random(s_key,s_value,&i_value,size_random);
         insert2(i_value,i_value);
    }

        print();
    int a;
    std::cin>>a;
}
//-------------end main--------------------------------

//--------my function ---------------------------
//-------random--------------
void gen_random(char* s,char* p,int *r, const int len) {


    static const char alphanum[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
     (*r)=rand();

    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
         p[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    s[len] = 0;
    p[len] = 0;
}
//-----end random------------

    void inline insert2(int i_key,int i_value)
    {
        map2.insert(MAP::value_type(i_key,i_value));
    }
    void print()
    {

    for(it2=map2.begin();it2 != map2.end();++it2 );
     {


         cout<<it2->first;

     }

    }
//--------end function---------------------------
Posted
Updated 10-Nov-11 12:17pm
v2
Comments
Paul Heil 10-Nov-11 16:10pm    
Under Debug->Exceptions check the "Thrown" box for "Win32 Exceptions"
Run the program in debug mode.
What line of code is the debugger pointing to when the program throws the exception?
vahid851041002 10-Nov-11 16:21pm    
cout<<it2->first;
Paul Heil 10-Nov-11 16:55pm    
If you set a breakpoint there and hold your mouse over "first" what value does it show?

Here's one problem...
You're writing past the end of these strings.

C++
s[len] = 0;
p[len] = 0;
 
Share this answer
 
Comments
Paul Heil 10-Nov-11 16:56pm    
cheater! I was going to show him how to get there. :-P
ah, well.
JackDingler 10-Nov-11 17:00pm    
Ooops, sorry.

These can be a tough one for beginners (and at times seasoned programmers) to find, because they don't always throw errors at the point of execution.
i forgot remove semicolon in the end of for.when i remove that.the problem is gon
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900