?
Solved

probably a bug in MS VSC++ 6.0???

Posted on 2004-04-08
7
Medium Priority
?
362 Views
Last Modified: 2010-04-01
I have this simple program , i dont know why but i get same refference memory error" The instruction at '0x004022ce' referenced memory at '0xddddddddd'. The memory could not read".. Here is the sample. Can anyone help me to solve that? Thanks
#include <list>
#include <iostream>
#include <string>

using namespace std ;

class Apple;
class Basket;
typedef list<Apple> LISTApple;


class Apple
{
public:
      Apple(){second=0;first=0;};
      void setDATA(int, int);
    int getfirst();
      int getsecond();

private:
      int first;
      int second;

};
class Basket
{
public:
      void addNewApple(Apple);
      Apple getFirstApple();
      void showList();
private:

            LISTApple listApple;
};


void main()
{
      Apple newApple;
      Basket myReadyQ;

      newApple.setDATA(6, 3);
      myReadyQ.addNewApple(newApple);
      cout<<"second = "<<myReadyQ.getFirstApple().getsecond()<<endl;
      cout<<"first= "<<myReadyQ.getFirstApple().getfirst()<<endl;  // it works fine without to cout this statement
}

void Apple::setDATA(int First, int Second)
{
      first=First;
      second=Second;

}

int Apple::getfirst()
{
      return first;
}

int Apple::getsecond()
{
      return second;
}

void Basket::addNewApple(Apple newApple)
{

      listApple.push_front(newApple);            

}


Apple Basket::getFirstApple()
{
      LISTApple::iterator i;
      Apple selectedApple;

      i= listApple.begin();
      selectedApple=(*i);

      listApple.pop_back();
      return selectedApple;
}

void Basket::showList()
{
      LISTApple::iterator i;


      for (i = listApple.begin(); i != listApple.end(); ++i)
                cout <<"First = "<<(*i).getfirst()<<", second = "<<(*i).getsecond()<<endl;
}
0
Comment
Question by:Carl3003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10789359
The  problem is with this line

 listApple.pop_back();
in getFirstApple()

When u first call
cout<<"second = "<<myReadyQ.getFirstApple().getsecond()<<endl;

It executes the code in getFirstApple() and thus removes the only elemnet that is present in your list
Then again when u call
cout<<"first= "<<myReadyQ.getFirstApple().getfirst()<<endl;

It again tries to access the element (apple) which is not present

Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10789367
You should either NOT remove the elemnt from from list i.e
listApple.pop_back(); should not be there in your getFirstApple() function since u need to reference the list element again

OR
u should check for the size of list and if it is no zero, then reference the iterator
somethinglike
if ( listApple.size() != 0 ) {
            i= listApple.begin();
        selectedApple=(*i);
        listApple.pop_back();
}

OR
check for listApple.begin() != listApple..end() in the above if condition

Amit
0
 

Author Comment

by:Carl3003
ID: 10789368
getfirst() and getsecond() is supposed to return the element  not to remove it
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Carl3003
ID: 10789374
oh i got you....
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 1000 total points
ID: 10789377
Apple Basket::getFirstApple()
{
     LISTApple::iterator i;
     Apple selectedApple;

     i= listApple.begin();
     selectedApple=(*i);

     listApple.pop_back();
     return selectedApple;
}


In the above function, u have used
     listApple.pop_back();
pop_back() removes the last element of the list.
Since your list contains only one element, when u call the function for the first time, the list becomes empty after exceuting the above code
Now, when u call the function the second time, ur list is empty BUT u are referecing the return value of
listApple.begin();
and then assign
selectedApple=(*i);

And then u again execute listApple.pop_back();
it has no elements and hence the error

Amit

0
 

Author Comment

by:Carl3003
ID: 10789378
ok, i will try to do it right now..
0
 

Author Comment

by:Carl3003
ID: 10789388
works, thanks a lot
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question