Solved

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

Posted on 2004-04-08
7
345 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
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Comment Utility
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
Comment Utility
getfirst() and getsecond() is supposed to return the element  not to remove it
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Carl3003
Comment Utility
oh i got you....
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 250 total points
Comment Utility
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
Comment Utility
ok, i will try to do it right now..
0
 

Author Comment

by:Carl3003
Comment Utility
works, thanks a lot
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now