Solved

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

Posted on 2004-04-08
7
353 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
Technology Partners: 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 250 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

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!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

730 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