Solved

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

Posted on 2004-04-08
7
355 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 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