8 Replies - 510 Views - Last Post: 18 March 2012 - 01:35 PM Rate Topic: -----

#1 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 03-November 11

Access violation with vector

Posted 18 March 2012 - 07:23 AM

Hey!

I get a "Access violation reading location" at this line of code: if(!m_vpVaror->empty())

m_vpVaror is declared like this: std::vector<Vara> *m_vpVaror;
It is a private class member, and I check if it is empty in a class member function.
I have seriously no idea why this line of code would cause such an error, so please enlighten me.

This post has been edited by DoNotWant: 18 March 2012 - 07:23 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Access violation with vector

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Access violation with vector

Posted 18 March 2012 - 08:04 AM

Could your pointer be null or pointing to something that is not a vector?

Vectors are dynamic objects so why would you use pointers with them? That seems redundant and needlessly confusing.
Was This Post Helpful? 2
  • +
  • -

#3 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Access violation with vector

Posted 18 March 2012 - 09:14 AM

I agree with CTphpnwb. You should not use dynamic allocation unless you Have to.
Let's see an example of the problem (isolated):
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> *pv1 = new vector<int>, *pv2;
    //pv1 points to an allocated vector of int now
    //but pv2 is not initialized, so it points to disaster
    if(pv1->empty()) {
        cout<<"pv1 is empty"<<endl; //this case is true, this sentence gets printed!
    }
    //this one causes Segmentation Fault
    if(pv2->empty()) {
        cout<<"pv2 is empty"<<endl; 
    }
    delete pv1;
    return 0;
}


In order to check if a pointer is pointing to somewhere or not, you can simply check it as a conditional statement, like this:
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> *pv1 = new vector<int>, *pv2=0;
    //pv1 points to an allocated vector of int now
    //but pv2 is not initialized, so it points to disaster
    if(!pv1) {
        cout<<"pv1 is not allocated"<<endl; //pv1 is allocated so this one is not printed on screen
    }
    if(!pv2) {
        cout<<"pv2 is not allocated"<<endl; //but pv2 was not allocated so this one is printed on screen
    }
    delete pv1;
    return 0;
}


Do this kind of checking and log the result using a simple cout statement to see if the pointer you had is allocated or not.
Was This Post Helpful? 1
  • +
  • -

#4 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 03-November 11

Re: Access violation with vector

Posted 18 March 2012 - 09:24 AM

Thanks guys, for some reason my code wouldn't work at first, that's why I used a pointer.
I thought it was strange tho, and now everything works when I changed from dynamic allocation.
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: Access violation with vector

Posted 18 March 2012 - 10:12 AM

View PostAnarion, on 18 March 2012 - 12:14 PM, said:

Do this kind of checking and log the result using a simple cout statement to see if the pointer you had is allocated or not.

I would suggest to actually print any error messages to std::cerr and not std::cout, that's what it's there for.
Was This Post Helpful? 1
  • +
  • -

#6 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Access violation with vector

Posted 18 March 2012 - 11:12 AM

View Postvividexstance, on 18 March 2012 - 08:42 PM, said:

I would suggest to actually print any error messages to std::cerr and not std::cout, that's what it's there for.

Yes that would be better, thanks for pointing this out. Although for a one-time testing it doesn't make any difference.
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: Access violation with vector

Posted 18 March 2012 - 11:22 AM

It does make a difference because std::cout is buffered and std::cerr isn't. Also, when you run a program on the command-line, you can redirect standard output, standard input, and standard error. Sometimes people want std::cerr to output to a file instead of to the console so that error messages are not mixed in with regular program output. You can't do this if all you use to output to the console is std::cout.
Was This Post Helpful? 0
  • +
  • -

#8 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Access violation with vector

Posted 18 March 2012 - 12:09 PM

View Postvividexstance, on 18 March 2012 - 09:52 PM, said:

It does make a difference because std::cout is buffered and std::cerr isn't. Also, when you run a program on the command-line, you can redirect standard output, standard input, and standard error. Sometimes people want std::cerr to output to a file instead of to the console so that error messages are not mixed in with regular program output. You can't do this if all you use to output to the console is std::cout.

That's true, and informative. Nice ;)
But my point is that, when you have a small piece of code and you are just temporarily testing something, it doesn't matter. Because you will be removing it after the problem is found/solved (again, here, in a small code, not in a full-featured productive CLI code).

But hey, I don't want to argue as you are correct on that :)
Was This Post Helpful? 0
  • +
  • -

#9 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: Access violation with vector

Posted 18 March 2012 - 01:35 PM

I'm not trying to argue with anyone. I was just posting a fact.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1