-2

I write this code:

On running, an error occurs in the 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 full code:

#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---------------------------
3
  • s[len] = 0; equals s[8] = 0;, and s is declared as 8-sized but you try to access the 9th element. Also, you could use the following iterator declaration : MAP::const_iterator it2;
    – azf
    Nov 10, 2011 at 20:45
  • 1
    This is awkwardly written, non-standard (void main() is not allowed in a C++ program), and in general difficult to read. Could you try to condense this down into a smaller program that still fails? Can VS 2008 Express tell you which line this is on? Nov 10, 2011 at 20:45
  • 5
    Beginner's C++ Tip: "If you're using pointers, You're Doing It Wrong."
    – Kerrek SB
    Nov 10, 2011 at 20:46

2 Answers 2

5

s[len] = 0; p[len] = 0;

You are trying to assign values to s[8] and p[8] which are outside the array bounds. They are declared as char[8] so they can only be indexed from 0 to 7.

1
  • 2
    @vahid: That is some information that you want to include in the original question. You are nevertheless causing undefined behavior, which could be the actual source of your problem. Though I doubt it, triggering undefined behavior makes the entire program be undefined. So start by fixing those invalid memory accesses.
    – K-ballo
    Nov 10, 2011 at 20:51
2

The line:

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

Should not end in a semicolon. The loop runs, but does not go into the block of code following it. When the loop completes, it2 is equal to map2.end() which is why you get an access violation when you try to use it.

This problem could have been avoided if you used a smaller scoped variable. For example, this would get you a compiler error (assuming it2 was no longer a global):

for(boost::unordered_map<int,int>::iterator it2 = map2.begin(); it2 != map2.end(); ++it2);
{
    cout<<it2->first;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.