Problem

This is a discussion on Problem within the C++ Programming forums, part of the General Programming Boards category; I'm still quite a newbie to C++, but definately not to C. I'm working on my first large C++ project, ...

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    Problem

    I'm still quite a newbie to C++, but definately not to C. I'm working on my first large C++ project, and I've made a lot of headway with it, but I'm encountering an error I just cannot figure out. It compiles fine, but I'm getting an invalid page fault error in the output of my "term" class. Judging from the error and past experience, I'd say I messed up with new and delete allocation. I'm basing all my work off of a pretty shabby C++ tutorial, though. I'm trying to find a new one. Anyway, my two source files are attached. I've marked the point I narrowed down the error to, but I've come to find that that sometimes doesn't mean squat, as I'm sure many of you know.
    Attached Files Attached Files

  2. #2
    Deo
    Deo is offline
    Registered User
    Join Date
    May 2005
    Posts
    73
    Ugg.. its very late here.. i'm half asleep.. plus rarely use double pointers **.
    nvalid page fault error Would this occur if you were trying to access memory that your program doesn't have access to?

    Anyways from what I see the number variables aren't being initiazed correctly in your term class.

    Code:
    numer = new number*[1];
    denom = new number*[1];
    Code:
    number** temp;
    temp = new number*[numercount];
    Shouldn't temp,numer,denom after this statement point to a number with initial values set to BASIC_ARGUMENTS? I'm getting bumbkiss.

    This doesn't seem to initialize anything correctly. Thus preventing the following section of code from working
    Code:
       term operator *= (number& numptr)
         {
          ++numercount;
          number** temp;
          temp = new number*[numercount];
          for(int i; i < numercount - 1; ++ i)
            temp[i] = numer[i];
          delete [] numer;
          numer = new number*[numercount];
          for(int x; x < numercount - 1; ++x)
            numer[x] = temp[x];
          numer[numercount - 1] = &numptr;
          delete [] temp;
          return *this;
         }
    Honestly I'm completely confused.. why would you initialize numer[0] = NULL then try and copy it over to a temp structure?

    As for the line in red don't you need to set int i = 0? Otherwise i will equal like -89328409823.

    As for the next block of code:
    Code:
    ostream& operator << (ostream& out, term &t)
     {
      cout << endl;
    //  cout << "Checkpoint 2" << endl;
      for(int i = 0; i < t.numercount; ++i)
       {
        cout << "Checkpoint 3" << endl;
        cout << "  Numerator " << (i + 1) << " address = " << int(t.numer[i]);
        cout << endl;
       }
      cout << endl;
      for(int x = 0; x < t.denomcount; ++x)
       {
        cout << "Checkpoint 4" << endl;
        cout << "  Denominator " << (x + 1) << " address = " << int(t.denom[x]);
        cout << endl;
       }
    //  cout << "Checkpoint 5" << endl;
    Shouldn't you put:
    Code:
    return cout;
    or something like that as the last line.. i think the function wants to return a stream.

    Oh yeah i made it through here was my final output.

    Code:
    num address = 4695928
    num2 address = 4695952
    Checkpoint 1
    
    Checkpoint 3
      Numerator 1 address = -572662307
    
    Checkpoint 5
    
    Checkpoint 6
    
    Press any key to continue...
    Code:
    ~term()
    {
      delete [] numer;
      delete [] denom;
    }
    Seems rather hostile as well
    Therefore
    Code:
    ostream& operator << (ostream& out, term t)
    When this function ends t will call the destructor. According to your code t points to num which points to a number. So T is destroyed which eliminates NUM? I THINK.. so after main() when T is destroyed again KABOOM!

    Lastly:
    Code:
    t *= num;
    After this line T is automatically destroyed.. thus wasted all of the effort made to multiply it! why? cause T isn't being passed in by reference.
    Code:
    BEFORE
    term operator *= (number& numptr)
    
    AFTER
    term& operator *= (number& numptr)
    FINAL OUTPUT

    Code:
    num address = 4695928
    num2 address = 4695952
    Checkpoint 1
    
    Checkpoint 2
    Checkpoint 3
      Numerator 1 address = 4695928
    
    Checkpoint 5
    
    Checkpoint 6
    
    Press any key to continue...
    Anyways like I said I don't use ** very much ... so i'm probably just giving you completely wrong info.. but oh well...can't say i didn't try
    Last edited by Deo; 06-05-2005 at 01:21 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Post your code between code tags if you want more people to look at it.

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    *sigh* I always make stupid maistakes like the uninitializing of int i, forgetting to return cout, and the missing & indentifier. Again, I'm still a newb, so these things aren't habit, yet. Sorry for making you go through all that.

    I think you were confused about the initialization thing. I realize my programming is uite difficult to understand. I very rarely need to put the entire thing up, so others being able to understand it isn't an issue. I'll have to work on that. The basic idea is that a term will be produced when two numbers are put together through multiplication or division, and the numebers can't go together. Usually, this would mean that the exponents are different. Thus, when a term is created, it doesn't know which numbers it should point to. It's basic algebra, when you think about it, except with no variables. I initialize them to NULL, simply to make sure that they don't point to anything. If you think that the *= operator is copying the NULL over, that's becuase I haven't written the part that will actually change that NULL. But in any case, I'll go back over my thinking on that part.

    I use double pointers, by the way, simply so I don't have to create a whole new number variable for each term. I may end up rethinking that.

    Anyway, thanks a whole lot for your input. Sorry you had to go through all this, especially at night. I know what it's like dealing with code at three in the morning. :P I'll bet that uninitialized i was causing me to access the wrong memory. Thanks again.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    For your overloaded << operator, you should be outputting to out (the stream passed as an argument) and then return that as well.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21